C# 解释器执行的最佳方式

发布于 2024-12-08 01:50:13 字数 474 浏览 0 评论 0原文

我正在编写一种脚本语言,我已经完成了词法分析器和解析器,我想在内存中动态执行。

可以说我有类似的东西,

function Hello(World)
{
    Print(world);
}
var world = "World";
var boolean = true;
if(boolean == true)
{
    Print("True is True");
}
else
{
    Print("False is True");
}
Hello(world);

什么是执行我尝试过的这个代码片段的最佳方式

1)OpCode Il Generation(我无法让 if 语句工作或除打印功能之外的任何东西) 2)RunSharp,我无法执行这些功能,因为我不知道如何执行该功能。

如果有人能指出我正确的方向!

小代码会有所帮助 链接到资源(不是像 IronPython 这样的东西)也很好

I am writing a scripting language, i've done the lexer and parser and i want to dynamically execute in the memory.

Lets say i have something like

function Hello(World)
{
    Print(world);
}
var world = "World";
var boolean = true;
if(boolean == true)
{
    Print("True is True");
}
else
{
    Print("False is True");
}
Hello(world);

what would be the best way to execute this snippet i'ved tried

1) OpCode Il Generation (i couldn't get if statement to work or anything other than Print function)
2) RunSharp, i cannot do the functions cause the way i can do it i have no idea how.

If anyone could point me to the right direction!

Alittle Code will help
Link to resource (not something like IronPython) would be also good

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

能怎样 2024-12-15 01:50:13

你的脚本语言,比如 JavaScript,如果它在内存中动态编译。

例子:

//csc sample.cs -r:Microsoft.JScript.dll
using System;
using System.CodeDom.Compiler;
using Microsoft.JScript;

class Sample {
    static public void Main(){
        string[] source = new string[] {
@"import System;
class JsProgram {
    function Print(mes){
        Console.WriteLine(mes);
    }
    function Hello(world){
        Print(world);
    }
    function proc(){
        var world = ""World"";
        var bool = true;
        if(bool == true){
            Print(""True is True"");
        }
        else{
            Print(""False is True"");
        }
        Hello(world);
    }
}"
        };
        var compiler = new JScriptCodeProvider();
        var opt      = new CompilerParameters();
        opt.ReferencedAssemblies.Add("System.dll");
        opt.GenerateExecutable = false;
        opt.GenerateInMemory = true;
        var result = compiler.CompileAssemblyFromSource(opt, source);
        if(result.Errors.Count > 0){
            Console.WriteLine("Compile Error");
            return;
        }
        var js = result.CompiledAssembly;
        dynamic jsProg = js.CreateInstance("JsProgram");
        jsProg.proc();
/*
True is True
World
*/
    }
}

your script language like JavaScript,if it dynamic compile in memory.

example:

//csc sample.cs -r:Microsoft.JScript.dll
using System;
using System.CodeDom.Compiler;
using Microsoft.JScript;

class Sample {
    static public void Main(){
        string[] source = new string[] {
@"import System;
class JsProgram {
    function Print(mes){
        Console.WriteLine(mes);
    }
    function Hello(world){
        Print(world);
    }
    function proc(){
        var world = ""World"";
        var bool = true;
        if(bool == true){
            Print(""True is True"");
        }
        else{
            Print(""False is True"");
        }
        Hello(world);
    }
}"
        };
        var compiler = new JScriptCodeProvider();
        var opt      = new CompilerParameters();
        opt.ReferencedAssemblies.Add("System.dll");
        opt.GenerateExecutable = false;
        opt.GenerateInMemory = true;
        var result = compiler.CompileAssemblyFromSource(opt, source);
        if(result.Errors.Count > 0){
            Console.WriteLine("Compile Error");
            return;
        }
        var js = result.CompiledAssembly;
        dynamic jsProg = js.CreateInstance("JsProgram");
        jsProg.proc();
/*
True is True
World
*/
    }
}
决绝 2024-12-15 01:50:13

生成 C# 源并编译它:-)
http://support.microsoft.com/kb/304655

generate c# source and compile it :-)
http://support.microsoft.com/kb/304655

春花秋月 2024-12-15 01:50:13

如果我理解正确的话,您想在运行时编译并运行代码吗?那么你可以尝试 http://www.csscript.net/index.html ,它是一个例子在链接的同一页面上。

If I understand you right, you want to compile and run the code on runtime? then you could try http://www.csscript.net/index.html , its an example on the same page linked.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文