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 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 CodeObject Compile(AstNode syntaxNode, CodeProgram prog) { var syntaxAssignExpr = (ScriptAssignExpr)syntaxNode; if (syntaxAssignExpr.LeftExpression.IsVariable) { if (syntaxAssignExpr.Symbol == "++") { var binary = new CodeBinaryOperator { Left = new CodeVariableReference(syntaxAssignExpr.LeftExpression.Identifier), Right = new CodeValueReference(1), Type = OperatorType.Plus }; var codeAssign = new CodeAssignExpression( syntaxAssignExpr.LeftExpression.Identifier, binary) { SourceSpan = syntaxNode.Span }; return(codeAssign); } else if (syntaxAssignExpr.Symbol == "--") { var binary = new CodeBinaryOperator { Left = new CodeVariableReference(syntaxAssignExpr.LeftExpression.Identifier), Right = new CodeValueReference(1), Type = OperatorType.Minus }; var codeAssign = new CodeAssignExpression( syntaxAssignExpr.LeftExpression.Identifier, binary) { SourceSpan = syntaxNode.Span }; return(codeAssign); } else { var codeAssign = new CodeAssignExpression( syntaxAssignExpr.LeftExpression.Identifier, (CodeExpression)AstDomCompiler.Compile(syntaxAssignExpr.RightExpression, prog)) { SourceSpan = syntaxNode.Span }; return(codeAssign); } } return(null); }
public CodeObject Compile(AstNode syntaxNode, CodeProgram prog) { var syntaxAssignExpr = (ScriptAssignExpr)syntaxNode; if (syntaxAssignExpr.LeftExpression.IsVariable) { if (syntaxAssignExpr.Symbol == "++") { var binary = new CodeBinaryOperator { Left = new CodeVariableReference(syntaxAssignExpr.LeftExpression.Identifier), Right = new CodeValueReference(1), Type = OperatorType.Plus }; var codeAssign = new CodeAssignExpression( syntaxAssignExpr.LeftExpression.Identifier, binary) {SourceSpan = syntaxNode.Span}; return codeAssign; } else if (syntaxAssignExpr.Symbol == "--") { var binary = new CodeBinaryOperator { Left = new CodeVariableReference(syntaxAssignExpr.LeftExpression.Identifier), Right = new CodeValueReference(1), Type = OperatorType.Minus }; var codeAssign = new CodeAssignExpression( syntaxAssignExpr.LeftExpression.Identifier, binary) {SourceSpan = syntaxNode.Span}; return codeAssign; } else { var codeAssign = new CodeAssignExpression( syntaxAssignExpr.LeftExpression.Identifier, (CodeExpression)AstDomCompiler.Compile(syntaxAssignExpr.RightExpression, prog)) {SourceSpan = syntaxNode.Span}; return codeAssign; } } return null; }
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; }