1

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

2
  • 1)OpCode Generation:Convert the expression tree by Your Parser to parse your language. Expression Object is msdn.microsoft.com/library/bb356138.aspx. Expression Object Tree Compile :msdn.microsoft.com/library/bb345362.aspx. You made YourLanguegeProvider by it. Commented Oct 4, 2011 at 10:42
  • thxs i will check it out Edit: Thanks! that looks like it will fit my needs, just too save time of browsing, is it possible to do the functions and then enable them to be called, i couldn't figure how with opcodes or runsharp Commented Oct 4, 2011 at 14:00

3 Answers 3

3

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
*/
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

If i cannot do it like the pro's do i may have to do it this way :P
i would do it this way, you benefit from all compiler optimisations and you can debug the generated c# code instead of il asm ;-)
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.