public ExecutableMachine Compile(CodeObject code, ExecutableMachine machine) { // CCCC(indexA)SSSSSS(indexB)EEEEEE(indexC) var ifStatement = (CodeIfStatement)code; CodeDomCompiler.Compile(ifStatement.Condition, machine); //Put AX (result of condition evaluation) to BBX var exch = machine.CreateOperation<RegisterOperation>(); exch.Source = MachineRegisters.AX; exch.Destination = MachineRegisters.BBX; //Jmp To Else var jmpToElse = machine.CreateOperation<JmpIfOperation>(); int eCount = machine.CommandCount; //Compile Statement CodeDomCompiler.Compile(ifStatement.Statement, machine); jmpToElse.Offset = machine.CommandCount - eCount + 1; //Compile Else if Any if (ifStatement.ElseStatement != null) { //Jmp To Next var jmpToNext = machine.CreateOperation<JmpOperation>(); //Modify jmpToElse jmpToElse.Offset++; int sCount = machine.CommandCount; CodeDomCompiler.Compile(ifStatement.ElseStatement, machine); jmpToNext.Offset = machine.CommandCount - sCount + 1; } return machine; }
public ExecutableMachine Compile(CodeObject code, ExecutableMachine machine) { // CCCC(indexA)SSSSSS(indexC) var whileStatement = (CodeWhileStatement)code; int cCount = machine.CommandCount; CodeDomCompiler.Compile(whileStatement.Condition, machine); //Put AX (result of condition evaluation) to BBX var exch = machine.CreateOperation <RegisterOperation>(); exch.Source = MachineRegisters.AX; exch.Destination = MachineRegisters.BBX; //Jmp To Else var jmpToNext = machine.CreateOperation <JmpIfOperation>(); int eCount = machine.CommandCount; //Compile Statement CodeDomCompiler.Compile(whileStatement.Statement, machine); var jmpToCondition = machine.CreateOperation <JmpIfOperation>(); jmpToCondition.Offset = cCount - machine.CommandCount + 1; jmpToNext.Offset = machine.CommandCount - eCount + 1; return(machine); }
public ExecutableMachine Compile(CodeObject code, ExecutableMachine machine) { var codeExpression = (CodeObjectReference)code; if (codeExpression.Modifiers.Count == 0) { var op = machine.CreateOperation<GetValueOperation>(); op.Id = codeExpression.Id; } else { foreach (var modifier in codeExpression.Modifiers) { var functionCall = modifier as CodeObjectFunctionCall; if (functionCall == null) continue; CodeDomCompiler.Compile(modifier, machine); var callOp = machine.CreateOperation<ObjectMemberOperation>(); callOp.MemberName = codeExpression.Id; } } if (codeExpression.Next != null) { machine.CreateOperation<PushOperation>(); CodeDomCompiler.Compile(codeExpression.Next, machine); } return machine; }
public ExecutableMachine Compile(CodeObject code, ExecutableMachine machine) { var codeSwitch = (CodeSwitchStatement)code; //switch(a) { case c1 : s1; case c2 : s2; default : s3 } //~ //switch_expr = a; //if (a == c1) s1 //else if (a == c2) s2 //else s3 _sid++; var block = new CodeBlockStatement { SourceSpan = codeSwitch.SourceSpan }; var switchExpr = new CodeAssignExpression( "#switch_" + _sid, codeSwitch.Expression); var ifStatement = BuildNextIf(codeSwitch.Cases, 0); block.Statements.Add(new CodeExpressionStatement(switchExpr)); block.Statements.Add(ifStatement); CodeDomCompiler.Compile(block, machine); return(machine); }
public ExecutableMachine Compile(CodeObject code, ExecutableMachine machine) { // CCCC(indexA)SSSSSS(indexC) var whileStatement = (CodeWhileStatement)code; int cCount = machine.CommandCount; CodeDomCompiler.Compile(whileStatement.Condition, machine); //Put AX (result of condition evaluation) to BBX var exch = machine.CreateOperation<RegisterOperation>(); exch.Source = MachineRegisters.AX; exch.Destination = MachineRegisters.BBX; //Jmp To Else var jmpToNext = machine.CreateOperation<JmpIfOperation>(); int eCount = machine.CommandCount; //Compile Statement CodeDomCompiler.Compile(whileStatement.Statement, machine); var jmpToCondition = machine.CreateOperation<JmpIfOperation>(); jmpToCondition.Offset = cCount - machine.CommandCount + 1; jmpToNext.Offset = machine.CommandCount - eCount + 1; return machine; }
public ExecutableMachine Compile(CodeObject code, ExecutableMachine machine) { // CCCC(indexA)SSSSSS(indexB)EEEEEE(indexC) var ifStatement = (CodeIfStatement)code; CodeDomCompiler.Compile(ifStatement.Condition, machine); //Put AX (result of condition evaluation) to BBX var exch = machine.CreateOperation <RegisterOperation>(); exch.Source = MachineRegisters.AX; exch.Destination = MachineRegisters.BBX; //Jmp To Else var jmpToElse = machine.CreateOperation <JmpIfOperation>(); int eCount = machine.CommandCount; //Compile Statement CodeDomCompiler.Compile(ifStatement.Statement, machine); jmpToElse.Offset = machine.CommandCount - eCount + 1; //Compile Else if Any if (ifStatement.ElseStatement != null) { //Jmp To Next var jmpToNext = machine.CreateOperation <JmpOperation>(); //Modify jmpToElse jmpToElse.Offset++; int sCount = machine.CommandCount; CodeDomCompiler.Compile(ifStatement.ElseStatement, machine); jmpToNext.Offset = machine.CommandCount - sCount + 1; } return(machine); }
public ExecutableMachine Compile(CodeObject code, ExecutableMachine machine) { var codeExpression = (CodeObjectReference)code; if (codeExpression.Modifiers.Count == 0) { var op = machine.CreateOperation <GetValueOperation>(); op.Id = codeExpression.Id; } else { foreach (var modifier in codeExpression.Modifiers) { var functionCall = modifier as CodeObjectFunctionCall; if (functionCall == null) { continue; } CodeDomCompiler.Compile(modifier, machine); var callOp = machine.CreateOperation <ObjectMemberOperation>(); callOp.MemberName = codeExpression.Id; } } if (codeExpression.Next != null) { machine.CreateOperation <PushOperation>(); CodeDomCompiler.Compile(codeExpression.Next, machine); } return(machine); }
public ExecutableMachine Compile(CodeObject code, ExecutableMachine machine) { var codeSwitch = (CodeSwitchStatement)code; //switch(a) { case c1 : s1; case c2 : s2; default : s3 } //~ //switch_expr = a; //if (a == c1) s1 //else if (a == c2) s2 //else s3 _sid++; var block = new CodeBlockStatement { SourceSpan = codeSwitch.SourceSpan }; var switchExpr = new CodeAssignExpression( "#switch_"+_sid, codeSwitch.Expression); var ifStatement = BuildNextIf(codeSwitch.Cases, 0); block.Statements.Add(new CodeExpressionStatement(switchExpr)); block.Statements.Add(ifStatement); CodeDomCompiler.Compile(block, machine); return machine; }
public ExecutableMachine Compile(CodeObject code, ExecutableMachine machine) { var op = machine.CreateOperation<ValueOperation>(); op.Value = ((CodeValueReference)code).Value; op.SourceObject = code; return machine; }
public ExecutableMachine Compile(CodeObject code, ExecutableMachine machine) { var ret = (CodeReturnStatement)code; CodeDomCompiler.Compile(ret.Expression, machine); machine.CreateOperation <RetOperation>(); return(machine); }
public ExecutableMachine Compile(CodeObject code, ExecutableMachine machine) { var ret = (CodeReturnStatement)code; CodeDomCompiler.Compile(ret.Expression, machine); machine.CreateOperation<RetOperation>(); return machine; }
public ExecutableMachine Compile(CodeObject code, ExecutableMachine machine) { var op = machine.CreateOperation <ValueOperation>(); op.Value = ((CodeValueReference)code).Value; op.SourceObject = code; return(machine); }
public ExecutableMachine Compile(CodeObject code, ExecutableMachine machine) { var codeExpression = (CodeVariableReference)code; var op = machine.CreateOperation<GetValueOperation>(); op.Id = codeExpression.Id; op.SourceObject = codeExpression; return machine; }
public ExecutableMachine Compile(CodeObject code, ExecutableMachine machine) { var codeExpression = (CodeVariableReference)code; var op = machine.CreateOperation <GetValueOperation>(); op.Id = codeExpression.Id; op.SourceObject = codeExpression; return(machine); }
public ExecutableMachine Compile(CodeObject code, ExecutableMachine machine) { var blockStatement = (CodeBlockStatement)code; foreach (var statement in blockStatement.Statements) { CodeDomCompiler.Compile(statement, machine); } return machine; }
public ExecutableMachine Compile(CodeObject code, ExecutableMachine machine) { var blockStatement = (CodeBlockStatement)code; foreach (var statement in blockStatement.Statements) { CodeDomCompiler.Compile(statement, machine); } return(machine); }
public ExecutableMachine Compile(CodeObject code, ExecutableMachine machine) { var codeExpression = (CodeAssignExpression)code; CodeDomCompiler.Compile(codeExpression.RightExpression, machine); var op = machine.CreateOperation<SetValueOperation>(); op.Id = codeExpression.Id; op.SourceObject = code; return machine; }
public void AstDomCompiler_ForEach() { IScriptContext context = new ScriptContext(); context.SetItem("a", new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }); CodeProgram domTree = AstDomCompiler.Compile(Script.Compile("sum=0; foreach(i in a) sum = sum + i;", null, false).Ast); ExecutableMachine vm = CodeDomCompiler.Compile(domTree); vm.Execute(context); Assert.Equal(45, context.GetItem("sum", true)); }
public ExecutableMachine Compile(CodeObject code, ExecutableMachine machine) { var codeExpression = (CodeAssignExpression)code; CodeDomCompiler.Compile(codeExpression.RightExpression, machine); var op = machine.CreateOperation <SetValueOperation>(); op.Id = codeExpression.Id; op.SourceObject = code; return(machine); }
public ExecutableMachine Compile(CodeObject code, ExecutableMachine machine) { var codeExpression = (CodeBinaryOperator)code; CodeDomCompiler.Compile(codeExpression.Right, machine); var op = machine.CreateOperation <PushOperation>(); op.SourceObject = codeExpression.Right; CodeDomCompiler.Compile(codeExpression.Left, machine); op = machine.CreateOperation <PushOperation>(); op.SourceObject = codeExpression.Left; Operation sop; switch (codeExpression.Type) { case OperatorType.Plus: sop = machine.CreateOperation <AddOperation>(); break; case OperatorType.Minus: sop = machine.CreateOperation <SubOperation>(); break; case OperatorType.Mul: sop = machine.CreateOperation <MulOperation>(); break; case OperatorType.Mod: sop = machine.CreateOperation <ModOperation>(); break; case OperatorType.Div: sop = machine.CreateOperation <DivOperation>(); break; default: var gop = machine.CreateOperation <GenericOperation>(); gop.Symbol = Mapping[codeExpression.Type]; sop = gop; break; } sop.SourceObject = codeExpression; var pop = machine.CreateOperation <PopOperation>(); pop.SourceObject = codeExpression; return(machine); }
public ExecutableMachine Compile(CodeObject code, ExecutableMachine machine) { var program = (CodeProgram)code; foreach (var statement in program.Statements) CodeDomCompiler.Compile(statement, machine); machine.CreateOperation<RetOperation>(); //TODO: Functions //foreach (CodeObject function in program.Functions) // Compile(function, machine); return machine; }
public void AstDomCompiler_InvokeMember3() { VM_Test1 vt1 = new VM_Test1(); IScriptContext context = new ScriptContext(); context.SetItem("v", vt1); CodeProgram domTree = AstDomCompiler.Compile(Script.Compile("return v.Next.Next;", null, false).Ast); ExecutableMachine vm = CodeDomCompiler.Compile(domTree); vm.Execute(context); Assert.Equal(2, ((VM_Test1)context.Result).Level); }
public void AstDomCompiler_InvokeMember() { int[] a = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int[] b = new int[9]; IScriptContext context = new ScriptContext(); context.SetItem("a", a); context.SetItem("b", b); CodeProgram domTree = AstDomCompiler.Compile(Script.Compile("return a.CopyTo(b, 0);", null, false).Ast); ExecutableMachine vm = CodeDomCompiler.Compile(domTree); vm.Execute(context); Assert.Equal(a[4], b[4]); }
public ExecutableMachine Compile(CodeObject code, ExecutableMachine machine) { var program = (CodeProgram)code; foreach (var statement in program.Statements) { CodeDomCompiler.Compile(statement, machine); } machine.CreateOperation <RetOperation>(); //TODO: Functions //foreach (CodeObject function in program.Functions) // Compile(function, machine); return(machine); }
public ExecutableMachine Compile(CodeObject code, ExecutableMachine machine) { var forStatement = (CodeForEachStatement)code; //foreach (i in c) statement ~ // //f_ = c.GetEnumerator(); //while(f_.Next()) {i = f_.Current; statement; } _sid++; string fName = "#ForEach_" + _sid; var fblock = new CodeBlockStatement(); var f = new CodeAssignExpression(fName, forStatement.Container); var f1 = new CodeAssignExpression(fName, new CodeObjectReference(fName, new CodeObjectReference("GetEnumerator", null, new CodeObject[] { new CodeObjectFunctionCall(new CodeExpression[0]) }), new CodeObject[0])); var fwhileBlock = new CodeBlockStatement(); var i = new CodeAssignExpression(forStatement.Id.Id, new CodeObjectReference(fName, new CodeObjectReference("get_Current", null, new CodeObject[] { new CodeObjectFunctionCall(new CodeExpression[0]) }), new CodeObject[0])); fwhileBlock.Statements.Add(new CodeExpressionStatement(i)); fwhileBlock.Statements.Add(forStatement.Statement); var fwhile = new CodeWhileStatement( new CodeObjectReference(fName, new CodeObjectReference("MoveNext", null, new CodeObject[] { new CodeObjectFunctionCall(new CodeExpression[0]) }), new CodeObject[0]), fwhileBlock); fblock.Statements.Add(new CodeExpressionStatement(f)); fblock.Statements.Add(new CodeExpressionStatement(f1)); fblock.Statements.Add(fwhile); CodeDomCompiler.Compile(fblock, machine); return(machine); }
public ExecutableMachine Compile(CodeObject code, ExecutableMachine machine) { var forStatement = (CodeForEachStatement)code; //foreach (i in c) statement ~ // //f_ = c.GetEnumerator(); //while(f_.Next()) {i = f_.Current; statement; } _sid++; string fName = "#ForEach_" + _sid; var fblock = new CodeBlockStatement(); var f = new CodeAssignExpression(fName, forStatement.Container); var f1 = new CodeAssignExpression(fName, new CodeObjectReference(fName, new CodeObjectReference("GetEnumerator", null, new CodeObject[] { new CodeObjectFunctionCall(new CodeExpression[0]) }), new CodeObject[0])); var fwhileBlock = new CodeBlockStatement(); var i = new CodeAssignExpression(forStatement.Id.Id, new CodeObjectReference(fName, new CodeObjectReference("get_Current", null, new CodeObject[] { new CodeObjectFunctionCall(new CodeExpression[0]) }), new CodeObject[0])); fwhileBlock.Statements.Add(new CodeExpressionStatement(i)); fwhileBlock.Statements.Add(forStatement.Statement); var fwhile = new CodeWhileStatement( new CodeObjectReference(fName, new CodeObjectReference("MoveNext", null, new CodeObject[] { new CodeObjectFunctionCall(new CodeExpression[0]) }), new CodeObject[0]), fwhileBlock); fblock.Statements.Add(new CodeExpressionStatement(f)); fblock.Statements.Add(new CodeExpressionStatement(f1)); fblock.Statements.Add(fwhile); CodeDomCompiler.Compile(fblock, machine); return machine; }
public void MachineExection() { IScriptContext context = new ScriptContext(); //Example 0: Machine ExecutableMachine machine = ExecutableMachine.Create(); SetValueOperation sv = machine.CreateOperation <SetValueOperation>(); sv.Id = "a"; machine.AX = "Hello World"; machine.CreateOperation <RetOperation>(); machine.Execute(context); object rez = context.GetItem("a", true); Assert.Equal("Hello World", rez); }
public ExecutableMachine Compile(CodeObject code, ExecutableMachine machine) { var codeExpression = (CodeBinaryOperator)code; CodeDomCompiler.Compile(codeExpression.Right, machine); var op = machine.CreateOperation<PushOperation>(); op.SourceObject = codeExpression.Right; CodeDomCompiler.Compile(codeExpression.Left, machine); op = machine.CreateOperation<PushOperation>(); op.SourceObject = codeExpression.Left; Operation sop; switch (codeExpression.Type) { case OperatorType.Plus: sop = machine.CreateOperation<AddOperation>(); break; case OperatorType.Minus: sop = machine.CreateOperation<SubOperation>(); break; case OperatorType.Mul: sop = machine.CreateOperation<MulOperation>(); break; case OperatorType.Mod: sop = machine.CreateOperation<ModOperation>(); break; case OperatorType.Div: sop = machine.CreateOperation<DivOperation>(); break; default: var gop = machine.CreateOperation<GenericOperation>(); gop.Symbol = Mapping[codeExpression.Type]; sop = gop; break; } sop.SourceObject = codeExpression; var pop = machine.CreateOperation<PopOperation>(); pop.SourceObject = codeExpression; return machine; }
public ExecutableMachine Compile(CodeObject code, ExecutableMachine machine) { var forStatement = (CodeForStatement)code; //for (init; cond; next) statement ~ // //init; //while(cond) { statement; next;} CodeDomCompiler.Compile(forStatement.Init, machine); var body = new CodeBlockStatement(); body.Statements.Add(forStatement.Statement); body.Statements.Add(new CodeExpressionStatement(forStatement.Next)); var newWhile = new CodeWhileStatement(forStatement.Condition, body); CodeDomCompiler.Compile(newWhile, machine); return machine; }
public ExecutableMachine Compile(CodeObject code, ExecutableMachine machine) { var forStatement = (CodeForStatement)code; //for (init; cond; next) statement ~ // //init; //while(cond) { statement; next;} CodeDomCompiler.Compile(forStatement.Init, machine); var body = new CodeBlockStatement(); body.Statements.Add(forStatement.Statement); body.Statements.Add(new CodeExpressionStatement(forStatement.Next)); var newWhile = new CodeWhileStatement(forStatement.Condition, body); CodeDomCompiler.Compile(newWhile, machine); return(machine); }
public ExecutableMachine Compile(CodeObject code, ExecutableMachine machine) { var call = (CodeObjectFunctionCall)code; // AX = function name // BX = parameter count // stack is full of parameters foreach (var codeParameter in call.Parameters) { CodeDomCompiler.Compile(codeParameter, machine); var op = machine.CreateOperation<PushOperation>(); } var countOp = machine.CreateOperation<RegisterOperation>(); countOp.Destination = MachineRegisters.BX; countOp.Value = call.Parameters.Count; return machine; }
public ExecutableMachine Compile(CodeObject code, ExecutableMachine machine) { var call = (CodeObjectFunctionCall)code; // AX = function name // BX = parameter count // stack is full of parameters foreach (var codeParameter in call.Parameters) { CodeDomCompiler.Compile(codeParameter, machine); var op = machine.CreateOperation <PushOperation>(); } var countOp = machine.CreateOperation <RegisterOperation>(); countOp.Destination = MachineRegisters.BX; countOp.Value = call.Parameters.Count; return(machine); }
public ExecutableMachine Compile(CodeObject code, ExecutableMachine machine) { return(CodeDomCompiler.Compile(((CodeExpressionStatement)code).Expression, machine)); }
public ExecutableMachine Compile(CodeObject code, ExecutableMachine machine) { return CodeDomCompiler.Compile(((CodeExpressionStatement)code).Expression, machine); }
public void Benchmark() { IScriptContext context = new ScriptContext(); //Example 0: Machine ExecutableMachine machine = ExecutableMachine.Create(); int iterations = 2;// 10000000; //loops = 10000000 ValueOperation op0 = machine.CreateOperation <ValueOperation>(); op0.Value = iterations; SetValueOperation op1 = machine.CreateOperation <SetValueOperation>(); op1.Id = "loops"; // counter = 0 ValueOperation op2 = machine.CreateOperation <ValueOperation>(); op2.Value = 0; SetValueOperation op3 = machine.CreateOperation <SetValueOperation>(); op3.Id = "counter"; //while (loops > 0) RegisterOperation op4 = machine.CreateOperation <RegisterOperation>(); op4.Destination = MachineRegisters.BX; op4.Value = 0; GetValueOperation op5 = machine.CreateOperation <GetValueOperation>(); op5.Id = "loops"; CmpOperation op6 = machine.CreateOperation <CmpOperation>(); JmpIfOperation op7 = machine.CreateOperation <JmpIfOperation>(); op7.Offset = 8; //loops = loops -1; GetValueOperation op8 = machine.CreateOperation <GetValueOperation>(); op8.Id = "loops"; DecOperation op9 = machine.CreateOperation <DecOperation>(); SetValueOperation op10 = machine.CreateOperation <SetValueOperation>(); op10.Id = "loops"; //counter = counter + 1; GetValueOperation op11 = machine.CreateOperation <GetValueOperation>(); op11.Id = "counter"; IncOperation op12 = machine.CreateOperation <IncOperation>(); SetValueOperation op13 = machine.CreateOperation <SetValueOperation>(); op13.Id = "counter"; JmpOperation op14 = machine.CreateOperation <JmpOperation>(); op14.Offset = -10; machine.CreateOperation <RetOperation>(); machine.Execute(context); object rez = context.GetItem("counter", true); Assert.Equal(iterations, rez); }