原文:CSREPL - REPL for C#
在ChrisAn忙于将Python寄宿于AvPad之时,我在思考为什么他不把C#也同时考虑进去呢?
作为一项生存能力的测试,我在10分钟内,编写了一个简单的REPL程序,它支持C#表达式和语句的命令行解析。
下面是它的基本使用方法:
>1+2+3
6
>DateTime.Now.ToString("T");
4:12:36 PM
为了支持交叉表达式变量,我定义了两个内置的函数,Set 和 Get:
>Set("X",32)
32
>Get("X")
32
为了支持调用任意的代码块,我定义了一个能够解析void(void) 代理的 Invoke方法:
> Invoke(delegate { for (int i = 0; i < 6; i++) Console.WriteLine(i); })
0
1
2
3
4
5
下面是原代码,在Whidbey Beta2下,将其编译为Console程序。
using
System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.CodeDom;
using System.CodeDom.Compiler;
namespace csrepl {
class Program {
static string funcprefix = "using System; "
+ "public delegate void Proc(); "
+ "public class Wrapper { "
+ " public static object Set(string name, object value) { "
+ " AppDomain.CurrentDomain.SetData(name, value); "
+ " return value; "
+ " } "
+ " public static object Get(string name) { "
+ " return AppDomain.CurrentDomain.GetData(name); "
+ " } "
+ " public static object Invoke(Proc proc) { "
+ " proc(); "
+ " return null; "
+ " } "
+ " public static object Eval() { return ";
static string funcsuffix = "; } }";
static string StringEval(string expr) {
string program = funcprefix + expr + funcsuffix;
ICodeCompiler compiler = CodeDomProvider.CreateProvider("C#").CreateCompiler();
CompilerParameters cp = new CompilerParameters();
cp.GenerateExecutable = false;
cp.GenerateInMemory = true;
CompilerResults results = compiler.CompileAssemblyFromSource(cp, program);
if (results.Errors.HasErrors) {
if (results.Errors[0].ErrorNumber == "CS0029")
return StringEval("Invoke(delegate { " + expr + "; })");
return results.Errors[0].ErrorText;
}
else {
Assembly assm = results.CompiledAssembly;
Type target = assm.GetType("Wrapper");
MethodInfo method = target.GetMethod("Eval");
object result = method.Invoke(null, null);
return result == null ? null : result.ToString();
}
}
static void Main(string[] args) {
while (true ) {
Console.Write("> ");
Console.Out.Flush();
string expr = Console.ReadLine();
if (expr == null)
break;
try {
string result = StringEval(expr);
Console.WriteLine(result);
}
catch (TargetInvocationException ex) {
Console.WriteLine(ex.InnerException.GetType().Name + ": " + ex.InnerException.Message);
}
catch (Exception ex) {
Console.WriteLine(ex.GetType().Name + ": " + ex.Message);
}
}
}
}
}
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.CodeDom;
using System.CodeDom.Compiler;
namespace csrepl {
class Program {
static string funcprefix = "using System; "
+ "public delegate void Proc(); "
+ "public class Wrapper { "
+ " public static object Set(string name, object value) { "
+ " AppDomain.CurrentDomain.SetData(name, value); "
+ " return value; "
+ " } "
+ " public static object Get(string name) { "
+ " return AppDomain.CurrentDomain.GetData(name); "
+ " } "
+ " public static object Invoke(Proc proc) { "
+ " proc(); "
+ " return null; "
+ " } "
+ " public static object Eval() { return ";
static string funcsuffix = "; } }";
static string StringEval(string expr) {
string program = funcprefix + expr + funcsuffix;
ICodeCompiler compiler = CodeDomProvider.CreateProvider("C#").CreateCompiler();
CompilerParameters cp = new CompilerParameters();
cp.GenerateExecutable = false;
cp.GenerateInMemory = true;
CompilerResults results = compiler.CompileAssemblyFromSource(cp, program);
if (results.Errors.HasErrors) {
if (results.Errors[0].ErrorNumber == "CS0029")
return StringEval("Invoke(delegate { " + expr + "; })");
return results.Errors[0].ErrorText;
}
else {
Assembly assm = results.CompiledAssembly;
Type target = assm.GetType("Wrapper");
MethodInfo method = target.GetMethod("Eval");
object result = method.Invoke(null, null);
return result == null ? null : result.ToString();
}
}
static void Main(string[] args) {
while (true ) {
Console.Write("> ");
Console.Out.Flush();
string expr = Console.ReadLine();
if (expr == null)
break;
try {
string result = StringEval(expr);
Console.WriteLine(result);
}
catch (TargetInvocationException ex) {
Console.WriteLine(ex.InnerException.GetType().Name + ": " + ex.InnerException.Message);
}
catch (Exception ex) {
Console.WriteLine(ex.GetType().Name + ": " + ex.Message);
}
}
}
}
}
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1726369