public void TestRoundTrip_Assign02() { //================================= // 1. Build AST // 2. Execute AST and verify // 3. Convert AST to source // 4. Execute source and verify //================================= int result1 = 30; ExecutionMirror mirror = null; // 1. Build the AST tree ProtoCore.AST.AssociativeAST.BinaryExpressionNode assign = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("a"), new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IntNode(10), new ProtoCore.AST.AssociativeAST.IntNode(20), ProtoCore.DSASM.Operator.add), ProtoCore.DSASM.Operator.assign); List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>(); astList.Add(assign); // 2. Execute AST and verify mirror = thisTest.RunASTSource(astList); Assert.IsTrue((Int64)mirror.GetValue("a").Payload == result1); // 3. Convert AST to source ProtoCore.CodeGenDS codegenDS = new ProtoCore.CodeGenDS(astList); string code = codegenDS.GenerateCode(); // 4. Execute source and verify mirror = thisTest.RunScriptSource(code); Assert.IsTrue((Int64)mirror.GetValue("a").Payload == result1); }
protected virtual void EmitFunctionDefNode(ref ProtoCore.AST.AssociativeAST.FunctionDefinitionNode funcDefNode) { if (funcDefNode.ReturnType.UID != ProtoCore.DSASM.Constants.kInvalidIndex) { } if (funcDefNode.Singnature != null) { } else { } if (null != funcDefNode.FunctionBody) { List <ProtoCore.AST.AssociativeAST.AssociativeNode> funcBody = funcDefNode.FunctionBody.Body; //EmitCode("{\n"); foreach (ProtoCore.AST.AssociativeAST.AssociativeNode bodyNode in funcBody) { if (bodyNode is ProtoCore.AST.AssociativeAST.BinaryExpressionNode) { ProtoCore.AST.AssociativeAST.BinaryExpressionNode binaryEpr = bodyNode as ProtoCore.AST.AssociativeAST.BinaryExpressionNode; EmitBinaryNode(ref binaryEpr); } if (bodyNode is ProtoCore.AST.AssociativeAST.ReturnNode) { ProtoCore.AST.AssociativeAST.ReturnNode returnNode = bodyNode as ProtoCore.AST.AssociativeAST.ReturnNode; EmitReturnNode(ref returnNode); } } } }
public void TestRoundTrip_Assign01() { //================================= // 1. Build AST // 2. Execute AST and verify // 3. Convert AST to source // 4. Execute source and verify //================================= int result1 = 10; ExecutionMirror mirror = null; // 1. Build AST ProtoCore.AST.AssociativeAST.BinaryExpressionNode assign = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("a"), new ProtoCore.AST.AssociativeAST.IntNode(10), ProtoCore.DSASM.Operator.assign); List <ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List <ProtoCore.AST.AssociativeAST.AssociativeNode>(); astList.Add(assign); // 2. Execute AST and verify mirror = thisTest.RunASTSource(astList); Assert.IsTrue((Int64)mirror.GetValue("a").Payload == result1); // 3. Convert AST to source ProtoCore.CodeGenDS codegenDS = new ProtoCore.CodeGenDS(astList); string code = codegenDS.GenerateCode(); // 4. Execute source and verify mirror = thisTest.RunScriptSource(code); Assert.IsTrue((Int64)mirror.GetValue("a").Payload == result1); }
protected virtual void EmitBinaryNode(ProtoCore.AST.AssociativeAST.BinaryExpressionNode binaryExprNode) { Validity.Assert(null != binaryExprNode); DFSTraverse(binaryExprNode.LeftNode); EmitCode(ProtoCore.Utils.CoreUtils.GetOperatorString(binaryExprNode.Optr)); DFSTraverse(binaryExprNode.RightNode); }
public static void UpdateBinaryExpressionLocation(ProtoCore.AST.AssociativeAST.BinaryExpressionNode node) { if (null == node || (null == node.LeftNode) || (null == node.RightNode)) { return; } SetNodeLocation(node, node.LeftNode, node.RightNode); }
protected virtual void EmitBinaryNode(ref ProtoCore.AST.AssociativeAST.BinaryExpressionNode binaryExprNode) { Validity.Assert(null != binaryExprNode); ProtoCore.AST.AssociativeAST.AssociativeNode leftNode = binaryExprNode.LeftNode; DFSTraverse(ref leftNode); ProtoCore.AST.AssociativeAST.AssociativeNode rightNode = binaryExprNode.RightNode; DFSTraverse(ref rightNode); }
private string GetVarNameFromCode(int lineNo, string code) { string varName = null; // Search the code using the input line no. /*string[] lines = code.Split('\n'); * string stmt = ""; * for (int i = lineNo - 1; i < lines.Length; ++i) * { * stmt += lines[i]; * } * * List<ProtoCore.AST.Node> nodes = GraphUtilities.ParseCodeBlock(stmt); * * // The first node must be a binary expressions * ProtoCore.AST.AssociativeAST.BinaryExpressionNode ben = nodes[0] as ProtoCore.AST.AssociativeAST.BinaryExpressionNode;*/ // Search for the binary expression in the input code which contains the warning ProtoCore.AST.AssociativeAST.BinaryExpressionNode ben = null; Validity.Assert(runner.runnerCore.AstNodeList != null); foreach (var node in runner.runnerCore.AstNodeList) { if (node is ProtoCore.AST.AssociativeAST.BinaryExpressionNode) { if (lineNo >= node.line && lineNo <= node.endLine) { ben = node as ProtoCore.AST.AssociativeAST.BinaryExpressionNode; break; } } } if (ben != null) { ProtoCore.AST.AssociativeAST.IdentifierNode lhs = ben.LeftNode as ProtoCore.AST.AssociativeAST.IdentifierNode; //Validity.Assert(lhs != null); if (lhs != null) { varName = lhs.Name; } else // lhs could be an IdentifierListNode in a CodeBlock { ProtoCore.AST.AssociativeAST.IdentifierListNode lhsIdent = ben.LeftNode as ProtoCore.AST.AssociativeAST.IdentifierListNode; Validity.Assert(lhsIdent != null); // Extract line of code corresponding to this Ast node and get its LHS string string identstmt = ProtoCore.Utils.ParserUtils.ExtractStatementFromCode(code, lhsIdent); int equalIndex = identstmt.IndexOf('='); varName = ProtoCore.Utils.ParserUtils.GetLHSatAssignment(identstmt, equalIndex)[0]; } } return(varName); }
public void TestProtoASTExecute_Assign01() { // Build the AST trees ProtoCore.AST.AssociativeAST.BinaryExpressionNode assign = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("a"), new ProtoCore.AST.AssociativeAST.IntNode(10), ProtoCore.DSASM.Operator.assign); List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>(); astList.Add(assign); // Verify the results ExecutionMirror mirror = thisTest.RunASTSource(astList); thisTest.Verify("a", 10); }
public void TestProtoASTExecute_Assign03() { GraphToDSCompiler.GraphCompiler gc = GraphToDSCompiler.GraphCompiler.CreateInstance(); /*b = 20;*/ ProtoCore.AST.AssociativeAST.BinaryExpressionNode assign2 = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("b"), new ProtoCore.AST.AssociativeAST.IntNode(20), ProtoCore.DSASM.Operator.assign); /*a = (b + 50)*(b + 20)*/ ProtoCore.AST.AssociativeAST.BinaryExpressionNode assign1 = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("a"), new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("b"), new ProtoCore.AST.AssociativeAST.IntNode(50), ProtoCore.DSASM.Operator.add), new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("b"), new ProtoCore.AST.AssociativeAST.IntNode(20), ProtoCore.DSASM.Operator.add), ProtoCore.DSASM.Operator.mul), ProtoCore.DSASM.Operator.assign); /*c = a - 200*/ ProtoCore.AST.AssociativeAST.BinaryExpressionNode assign3 = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("c"), new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("a"), new ProtoCore.AST.AssociativeAST.IntNode(200), ProtoCore.DSASM.Operator.sub), ProtoCore.DSASM.Operator.assign); /*d = b*/ ProtoCore.AST.AssociativeAST.BinaryExpressionNode assign4 = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("d"), new ProtoCore.AST.AssociativeAST.IdentifierNode("b"), ProtoCore.DSASM.Operator.assign); List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>(); astList.Add(assign2); astList.Add(assign1); astList.Add(assign3); astList.Add(assign4); ExecutionMirror mirror = thisTest.RunASTSource(astList); //a = 2800, c = 2600, d = b = 20 Obj o = mirror.GetValue("a"); Assert.IsTrue((Int64)o.Payload == 2800); o = mirror.GetValue("c"); Assert.IsTrue((Int64)o.Payload == 2600); Obj p = mirror.GetValue("b"); Assert.IsTrue((Int64)p.Payload == 20); o = mirror.GetValue("d"); Assert.IsTrue((Int64)o.Payload == 20); }
public void TestProtoASTExecute_Assign03() { /*b = 20;*/ ProtoCore.AST.AssociativeAST.BinaryExpressionNode assign2 = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("b"), new ProtoCore.AST.AssociativeAST.IntNode(20), ProtoCore.DSASM.Operator.assign); /*a = (b + 50)*(b + 20)*/ ProtoCore.AST.AssociativeAST.BinaryExpressionNode assign1 = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("a"), new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("b"), new ProtoCore.AST.AssociativeAST.IntNode(50), ProtoCore.DSASM.Operator.add), new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("b"), new ProtoCore.AST.AssociativeAST.IntNode(20), ProtoCore.DSASM.Operator.add), ProtoCore.DSASM.Operator.mul), ProtoCore.DSASM.Operator.assign); /*c = a - 200*/ ProtoCore.AST.AssociativeAST.BinaryExpressionNode assign3 = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("c"), new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("a"), new ProtoCore.AST.AssociativeAST.IntNode(200), ProtoCore.DSASM.Operator.sub), ProtoCore.DSASM.Operator.assign); /*d = b*/ ProtoCore.AST.AssociativeAST.BinaryExpressionNode assign4 = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("d"), new ProtoCore.AST.AssociativeAST.IdentifierNode("b"), ProtoCore.DSASM.Operator.assign); List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>(); astList.Add(assign2); astList.Add(assign1); astList.Add(assign3); astList.Add(assign4); ExecutionMirror mirror = thisTest.RunASTSource(astList); //a = 2800, c = 2600, d = b = 20 thisTest.Verify("a", 2800); thisTest.Verify("c", 2600); thisTest.Verify("b", 20); thisTest.Verify("d", 20); }
public static bool IsReturnExpressionNode(ProtoCore.AST.AssociativeAST.AssociativeNode node) { ProtoCore.AST.AssociativeAST.BinaryExpressionNode binaryNode = node as ProtoCore.AST.AssociativeAST.BinaryExpressionNode; if (null == binaryNode) { return(false); } ProtoCore.AST.AssociativeAST.IdentifierNode retNode = binaryNode.LeftNode as ProtoCore.AST.AssociativeAST.IdentifierNode; if (null == retNode) { return(false); } return(retNode.Value == ProtoCore.DSDefinitions.Keyword.Return); }
public void TestProtoASTExecute_Assign02() { // Build the AST tree ProtoCore.AST.AssociativeAST.BinaryExpressionNode assign = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("a"), new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IntNode(10), new ProtoCore.AST.AssociativeAST.IntNode(20), ProtoCore.DSASM.Operator.add), ProtoCore.DSASM.Operator.assign); List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>(); astList.Add(assign); // Verify the results ExecutionMirror mirror = thisTest.RunASTSource(astList); Obj o = mirror.GetValue("a"); Assert.IsTrue((Int64)o.Payload == 30); }
public AstCodeBlockTraverse(ProtoCore.AST.AssociativeAST.BinaryExpressionNode bNode) { ChildTree = bNode; }
public void TestProtoASTExecute_ArrayIndex_RHS_Assign04() { List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>(); // a = {1, 2, 3, 4}; int[] input = { 1, 2, 3, 4 }; ProtoCore.AST.AssociativeAST.BinaryExpressionNode declareNodeA = CreateDeclareArrayNode("a", input); astList.Add(declareNodeA); // b = 4; ProtoCore.AST.AssociativeAST.BinaryExpressionNode declareNodeB = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("b"), new ProtoCore.AST.AssociativeAST.IntNode(5), ProtoCore.DSASM.Operator.assign); astList.Add(declareNodeB); // def foo(){ // return = -4; // } ProtoCore.AST.AssociativeAST.CodeBlockNode cbn = new ProtoCore.AST.AssociativeAST.CodeBlockNode(); ProtoCore.AST.AssociativeAST.BinaryExpressionNode returnNode = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode(ProtoCore.DSDefinitions.Keyword.Return), new ProtoCore.AST.AssociativeAST.IntNode(-4), ProtoCore.DSASM.Operator.assign); cbn.Body.Add(returnNode); // Build the function definition foo const string functionName = "foo"; ProtoCore.AST.AssociativeAST.FunctionDefinitionNode funcDefNode = new ProtoCore.AST.AssociativeAST.FunctionDefinitionNode() { Name = functionName, FunctionBody = cbn }; // Function Return Type ProtoCore.Type returnType = new ProtoCore.Type(); returnType.Initialize(); returnType.UID = (int)ProtoCore.PrimitiveType.Var; returnType.Name = ProtoCore.DSDefinitions.Keyword.Var; funcDefNode.ReturnType = returnType; astList.Add(funcDefNode); // c = a[b + foo()]; ProtoCore.AST.AssociativeAST.FunctionCallNode functionCall = new ProtoCore.AST.AssociativeAST.FunctionCallNode(); functionCall.Function = new ProtoCore.AST.AssociativeAST.IdentifierNode(functionName); ProtoCore.AST.AssociativeAST.BinaryExpressionNode operation1 = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("b"), functionCall, ProtoCore.DSASM.Operator.add); ProtoCore.AST.AssociativeAST.IdentifierNode nodeALHS = new ProtoCore.AST.AssociativeAST.IdentifierNode("a"); nodeALHS.ArrayDimensions = new ProtoCore.AST.AssociativeAST.ArrayNode { Expr = operation1 }; ProtoCore.AST.AssociativeAST.BinaryExpressionNode nodeALHSAssignment = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("c"), nodeALHS, ProtoCore.DSASM.Operator.assign); astList.Add(nodeALHSAssignment); // Verify the results ExecutionMirror mirror = thisTest.RunASTSource(astList); Obj o = mirror.GetValue("c"); Console.WriteLine(o.Payload); // expected: c = 2 Assert.AreEqual(2, Convert.ToInt32(o.Payload)); }
public void TestProtoASTExecute_ArrayIndex_LHS_Assign05() { List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>(); // a = { { 0, 1 }, { 3, 4, 5 } }; int[] input1 = { 0, 1 }; int[] input2 = { 3, 4, 5 }; List<ProtoCore.AST.AssociativeAST.AssociativeNode> arrayList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>(); arrayList.Add(CreateExprListNodeFromArray(input1)); arrayList.Add(CreateExprListNodeFromArray(input2)); ProtoCore.AST.AssociativeAST.ExprListNode arrayExpr = new ProtoCore.AST.AssociativeAST.ExprListNode { Exprs = arrayList }; ProtoCore.AST.AssociativeAST.BinaryExpressionNode declareNodeA = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("a"), arrayExpr, ProtoCore.DSASM.Operator.assign); astList.Add(declareNodeA); // b = 2; ProtoCore.AST.AssociativeAST.BinaryExpressionNode declareNodeB = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("b"), new ProtoCore.AST.AssociativeAST.IntNode(2), ProtoCore.DSASM.Operator.assign); astList.Add(declareNodeB); // a[0][b] = b; ProtoCore.AST.AssociativeAST.IdentifierNode nodeALHS = new ProtoCore.AST.AssociativeAST.IdentifierNode("a"); nodeALHS.ArrayDimensions = new ProtoCore.AST.AssociativeAST.ArrayNode { Expr = new ProtoCore.AST.AssociativeAST.IntNode(0), Type = new ProtoCore.AST.AssociativeAST.ArrayNode { Expr = new ProtoCore.AST.AssociativeAST.IdentifierNode("b") } }; ProtoCore.AST.AssociativeAST.BinaryExpressionNode nodeALHSAssignment = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( nodeALHS, new ProtoCore.AST.AssociativeAST.IdentifierNode("b"), ProtoCore.DSASM.Operator.assign); astList.Add(nodeALHSAssignment); // Verify the results ExecutionMirror mirror = thisTest.RunASTSource(astList); Obj o = mirror.GetValue("a"); Console.WriteLine(o.Payload); int[] output1 = { 0, 1, 2 }; int[] output2 = { 3, 4, 5 }; // Result should be = { { 0, 1, 2 }, { 3, 4, 5 } }; ProtoCore.DSASM.Mirror.DsasmArray result = o.Payload as ProtoCore.DSASM.Mirror.DsasmArray; Assert.IsNotNull( result ); // First row of array = { 0, 1, 2 } ProtoCore.DSASM.Mirror.DsasmArray array1 = result.members[0].Payload as ProtoCore.DSASM.Mirror.DsasmArray; Assert.IsNotNull( array1 ); for (int i = 0; i < output1.Length; i++) Assert.AreEqual(output1[i], Convert.ToInt32(array1.members[i].Payload)); // Second row of array = { 3, 4, 5 } ProtoCore.DSASM.Mirror.DsasmArray array2 = (ProtoCore.DSASM.Mirror.DsasmArray)result.members[1].Payload; Assert.IsNotNull( array2 ); for (int i = 0; i < output2.Length; i++) Assert.AreEqual(output2[i], Convert.ToInt32(array2.members[i].Payload)); }
public void TestProtoASTExecute_ArrayIndex_LHS_Assign03() { List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>(); // a = {1, 2, 3, 4}; int[] input = { 1, 2, 3, 4 }; ProtoCore.AST.AssociativeAST.BinaryExpressionNode declareNodeA = CreateDeclareArrayNode("a", input); astList.Add(declareNodeA); // b = 3; ProtoCore.AST.AssociativeAST.BinaryExpressionNode declareNodeB = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("b"), new ProtoCore.AST.AssociativeAST.IntNode(3), ProtoCore.DSASM.Operator.assign); astList.Add(declareNodeB); // a[b - 3] = 0; ProtoCore.AST.AssociativeAST.BinaryExpressionNode operation1 = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("b"), new ProtoCore.AST.AssociativeAST.IntNode(3), ProtoCore.DSASM.Operator.sub); ProtoCore.AST.AssociativeAST.IdentifierNode nodeALHS = new ProtoCore.AST.AssociativeAST.IdentifierNode("a"); nodeALHS.ArrayDimensions = new ProtoCore.AST.AssociativeAST.ArrayNode() { Expr = operation1 }; ProtoCore.AST.AssociativeAST.BinaryExpressionNode nodeALHSAssignment = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( nodeALHS, new ProtoCore.AST.AssociativeAST.IdentifierNode("0"), ProtoCore.DSASM.Operator.assign); astList.Add(nodeALHSAssignment); // Verify the results ExecutionMirror mirror = thisTest.RunASTSource(astList); Obj o = mirror.GetValue("a"); Console.WriteLine(o.Payload); // expected: a = { 0, 2, 3, 4 }; int[] expected = { 0, 2, 3, 4 }; ProtoCore.DSASM.Mirror.DsasmArray result = (ProtoCore.DSASM.Mirror.DsasmArray)o.Payload; for (int i = 0; i < expected.Length; i++) Assert.AreEqual(expected[i], Convert.ToInt32(result.members[i].Payload)); }
List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>(); astList.Add(assign); // Verify the results ExecutionMirror mirror = thisTest.RunASTSource(astList); Obj o = mirror.GetValue("a"); Assert.IsTrue((Int64)o.Payload == 11); } [Test] public void TestProtoASTExecute_Imperative_IfStatement02() { // // a = [Imperative] // { // b = 10; // if (b > 10) // { // b = 11; // } // else // { // b = 12 // } // return = b; // } // List<ProtoCore.AST.ImperativeAST.ImperativeNode> imperativeList = new List<ProtoCore.AST.ImperativeAST.ImperativeNode>(); // b = 10 ProtoCore.AST.ImperativeAST.BinaryExpressionNode imperativeAssign = new ProtoCore.AST.ImperativeAST.BinaryExpressionNode( new ProtoCore.AST.ImperativeAST.IdentifierNode("b"), new ProtoCore.AST.ImperativeAST.IntNode(10), ProtoCore.DSASM.Operator.assign); imperativeList.Add(imperativeAssign); // if (b > 10) ProtoCore.AST.ImperativeAST.BinaryExpressionNode equality = new ProtoCore.AST.ImperativeAST.BinaryExpressionNode( new ProtoCore.AST.ImperativeAST.IdentifierNode("b"), new ProtoCore.AST.ImperativeAST.IntNode(10), ProtoCore.DSASM.Operator.gt); ProtoCore.AST.ImperativeAST.IfStmtNode ifNode = new ProtoCore.AST.ImperativeAST.IfStmtNode(); ifNode.IfExprNode = equality; // if body // b = 11 ProtoCore.AST.ImperativeAST.BinaryExpressionNode ifCodeBlockStmt = new ProtoCore.AST.ImperativeAST.BinaryExpressionNode( new ProtoCore.AST.ImperativeAST.IdentifierNode("b"), new ProtoCore.AST.ImperativeAST.IntNode(11), ProtoCore.DSASM.Operator.assign); List<ProtoCore.AST.ImperativeAST.ImperativeNode> ifCodeBlock = new List<ProtoCore.AST.ImperativeAST.ImperativeNode>(); ifCodeBlock.Add(ifCodeBlockStmt); ifNode.IfBody = ifCodeBlock; // else body // b = 12 ProtoCore.AST.ImperativeAST.BinaryExpressionNode elseCodeBlockStmt = new ProtoCore.AST.ImperativeAST.BinaryExpressionNode( new ProtoCore.AST.ImperativeAST.IdentifierNode("b"), new ProtoCore.AST.ImperativeAST.IntNode(12), ProtoCore.DSASM.Operator.assign); List<ProtoCore.AST.ImperativeAST.ImperativeNode> elseCodeBlock = new List<ProtoCore.AST.ImperativeAST.ImperativeNode>(); elseCodeBlock.Add(elseCodeBlockStmt); ifNode.ElseBody = elseCodeBlock; imperativeList.Add(ifNode); // return = b ProtoCore.AST.ImperativeAST.BinaryExpressionNode returnStmt = new ProtoCore.AST.ImperativeAST.BinaryExpressionNode( new ProtoCore.AST.ImperativeAST.IdentifierNode("return"), new ProtoCore.AST.ImperativeAST.IdentifierNode("b"), ProtoCore.DSASM.Operator.assign); imperativeList.Add(returnStmt); // Build the language block ProtoCore.AST.ImperativeAST.CodeBlockNode imperativeCodeBlock = new ProtoCore.AST.ImperativeAST.CodeBlockNode(); imperativeCodeBlock.Body = imperativeList; ProtoCore.AST.AssociativeAST.LanguageBlockNode langblock = new ProtoCore.AST.AssociativeAST.LanguageBlockNode(); langblock.codeblock = new ProtoCore.LanguageCodeBlock(ProtoCore.Language.Imperative); langblock.CodeBlockNode = imperativeCodeBlock; // Build an assignment where the rhs is the imperative block ProtoCore.AST.AssociativeAST.BinaryExpressionNode assign = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("a"), langblock, ProtoCore.DSASM.Operator.assign);
// Generate the script ProtoCore.CodeGenDS codegen = new ProtoCore.CodeGenDS(astList); string code = codegen.GenerateCode(); ExecutionMirror mirror = thisTest.RunScriptSource(code); Assert.IsTrue((Int64)mirror.GetValue("a").Payload == 10); } [Test] public void TestCodegenDS_Imperative_IfStatement01() { // // a = [Imperative] // { // b = 10; // if (b == 10) // { // b = 11; // } // return = b; // } // List<ProtoCore.AST.ImperativeAST.ImperativeNode> imperativeList = new List<ProtoCore.AST.ImperativeAST.ImperativeNode>(); // b = 10 ProtoCore.AST.ImperativeAST.BinaryExpressionNode imperativeAssign = new ProtoCore.AST.ImperativeAST.BinaryExpressionNode( new ProtoCore.AST.ImperativeAST.IdentifierNode("b"), new ProtoCore.AST.ImperativeAST.IntNode(10), ProtoCore.DSASM.Operator.assign); imperativeList.Add(imperativeAssign); // if (b == 10) ProtoCore.AST.ImperativeAST.BinaryExpressionNode equality = new ProtoCore.AST.ImperativeAST.BinaryExpressionNode( new ProtoCore.AST.ImperativeAST.IdentifierNode("b"), new ProtoCore.AST.ImperativeAST.IntNode(10), ProtoCore.DSASM.Operator.eq); ProtoCore.AST.ImperativeAST.IfStmtNode ifNode = new ProtoCore.AST.ImperativeAST.IfStmtNode(); ifNode.IfExprNode = equality; // b = 11 ProtoCore.AST.ImperativeAST.BinaryExpressionNode ifCodeBlockStmt = new ProtoCore.AST.ImperativeAST.BinaryExpressionNode( new ProtoCore.AST.ImperativeAST.IdentifierNode("b"), new ProtoCore.AST.ImperativeAST.IntNode(11), ProtoCore.DSASM.Operator.assign); List<ProtoCore.AST.ImperativeAST.ImperativeNode> ifCodeBlock = new List<ProtoCore.AST.ImperativeAST.ImperativeNode>(); ifCodeBlock.Add(ifCodeBlockStmt); ifNode.IfBody = ifCodeBlock; imperativeList.Add(ifNode); // return = b ProtoCore.AST.ImperativeAST.BinaryExpressionNode returnStmt = new ProtoCore.AST.ImperativeAST.BinaryExpressionNode( new ProtoCore.AST.ImperativeAST.IdentifierNode("return"), new ProtoCore.AST.ImperativeAST.IdentifierNode("b"), ProtoCore.DSASM.Operator.assign); imperativeList.Add(returnStmt); // Build the language block ProtoCore.AST.ImperativeAST.CodeBlockNode imperativeCodeBlock = new ProtoCore.AST.ImperativeAST.CodeBlockNode(); imperativeCodeBlock.Body = imperativeList; ProtoCore.AST.AssociativeAST.LanguageBlockNode langblock = new ProtoCore.AST.AssociativeAST.LanguageBlockNode(); langblock.codeblock = new ProtoCore.LanguageCodeBlock(ProtoCore.Language.Imperative); langblock.CodeBlockNode = imperativeCodeBlock; // Build an assignment where the rhs is the imperative block ProtoCore.AST.AssociativeAST.BinaryExpressionNode assign = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("a"), langblock, ProtoCore.DSASM.Operator.assign); List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>();
ProtoCore.CodeGenDS codegen = new ProtoCore.CodeGenDS(astList); string code = codegen.GenerateCode(); ExecutionMirror mirror = thisTest.RunScriptSource(code); Assert.IsTrue((Int64)mirror.GetValue("a").Payload == 10); } [Test] [Ignore][Category("DSDefinedClass_Ignored_DSDefinedClassSemantics")] public void TestCodeGenDS_ClassDecl_MemFunctionCall_01() { // class bar // { // f : var // def foo (b:int) // { // b = 10; // return = b + 10; // } // } // // p = bar.bar(); // a = p.foo(); ProtoCore.AST.AssociativeAST.CodeBlockNode cbn = new ProtoCore.AST.AssociativeAST.CodeBlockNode(); // Build the function body ProtoCore.AST.AssociativeAST.BinaryExpressionNode assignment1 = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("b"), new ProtoCore.AST.AssociativeAST.IntNode(10), ProtoCore.DSASM.Operator.assign); ProtoCore.AST.AssociativeAST.BinaryExpressionNode returnExpr = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("b"), new ProtoCore.AST.AssociativeAST.IntNode(10), ProtoCore.DSASM.Operator.add); ProtoCore.AST.AssociativeAST.BinaryExpressionNode returnNode = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode(ProtoCore.DSDefinitions.Keyword.Return), returnExpr, ProtoCore.DSASM.Operator.assign); cbn.Body.Add(assignment1); cbn.Body.Add(returnNode); // Build the function definition foo const string functionName = "foo"; ProtoCore.AST.AssociativeAST.FunctionDefinitionNode funcDefNode = new ProtoCore.AST.AssociativeAST.FunctionDefinitionNode(); funcDefNode.Name = functionName; funcDefNode.FunctionBody = cbn; // Function Return type ProtoCore.Type returnType = new ProtoCore.Type(); returnType.Initialize(); returnType.UID = (int)ProtoCore.PrimitiveType.Var; returnType.Name = ProtoCore.DSDefinitions.Keyword.Var; funcDefNode.ReturnType = returnType; // Create the class node AST ProtoCore.AST.AssociativeAST.ClassDeclNode classDefNode = new ProtoCore.AST.AssociativeAST.ClassDeclNode(); classDefNode.ClassName = "bar"; // Add the member function 'foo' classDefNode.Procedures.Add(funcDefNode); // Create the property AST ProtoCore.AST.AssociativeAST.VarDeclNode varDeclNode = new ProtoCore.AST.AssociativeAST.VarDeclNode(); varDeclNode.Name = "f"; varDeclNode.NameNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("f"); varDeclNode.ArgumentType = new ProtoCore.Type() { Name = "int", rank = 0, UID = (int)ProtoCore.PrimitiveType.Integer }; classDefNode.Variables.Add(varDeclNode); // Add the constructed class AST List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>(); astList.Add(classDefNode); // p = bar.bar(); ProtoCore.AST.AssociativeAST.FunctionCallNode constructorCall = new ProtoCore.AST.AssociativeAST.FunctionCallNode(); constructorCall.Function = new ProtoCore.AST.AssociativeAST.IdentifierNode("bar"); ProtoCore.AST.AssociativeAST.IdentifierListNode identListConstrcctorCall = new ProtoCore.AST.AssociativeAST.IdentifierListNode(); identListConstrcctorCall.LeftNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("bar"); identListConstrcctorCall.RightNode = constructorCall; ProtoCore.AST.AssociativeAST.BinaryExpressionNode stmtInitClass = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("p"), identListConstrcctorCall, ProtoCore.DSASM.Operator.assign); astList.Add(stmtInitClass); // a = p.f; ProtoCore.AST.AssociativeAST.FunctionCallNode functionCall = new ProtoCore.AST.AssociativeAST.FunctionCallNode(); functionCall.Function = new ProtoCore.AST.AssociativeAST.IdentifierNode("foo"); ProtoCore.AST.AssociativeAST.IdentifierListNode identListFunctionCall = new ProtoCore.AST.AssociativeAST.IdentifierListNode(); identListFunctionCall.LeftNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("p"); identListFunctionCall.RightNode = functionCall; ProtoCore.AST.AssociativeAST.BinaryExpressionNode stmtPropertyAccess = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("a"), identListFunctionCall, ProtoCore.DSASM.Operator.assign);
public void TestRoundTrip_FunctionDefAndCall_02() { //================================= // 1. Build AST // 2. Execute AST and verify // 3. Convert AST to source // 4. Execute source and verify //================================= int result1 = 11; ExecutionMirror mirror = null; // 1. Build the AST tree // def foo(a : int) // { // b = 10; // return = b + a; // } // // x = foo(1); ProtoCore.AST.AssociativeAST.CodeBlockNode cbn = new ProtoCore.AST.AssociativeAST.CodeBlockNode(); // Build the function body ProtoCore.AST.AssociativeAST.BinaryExpressionNode assignment1 = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("b"), new ProtoCore.AST.AssociativeAST.IntNode(10), ProtoCore.DSASM.Operator.assign); ProtoCore.AST.AssociativeAST.BinaryExpressionNode returnExpr = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("b"), new ProtoCore.AST.AssociativeAST.IdentifierNode("a"), ProtoCore.DSASM.Operator.add); ProtoCore.AST.AssociativeAST.BinaryExpressionNode returnNode = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode(ProtoCore.DSDefinitions.Keyword.Return), returnExpr, ProtoCore.DSASM.Operator.assign); cbn.Body.Add(assignment1); cbn.Body.Add(returnNode); // Build the function definition foo const string functionName = "foo"; ProtoCore.AST.AssociativeAST.FunctionDefinitionNode funcDefNode = new ProtoCore.AST.AssociativeAST.FunctionDefinitionNode(); funcDefNode.Name = functionName; funcDefNode.FunctionBody = cbn; // build the args signature funcDefNode.Signature = new ProtoCore.AST.AssociativeAST.ArgumentSignatureNode(); ProtoCore.AST.AssociativeAST.VarDeclNode arg1Decl = new ProtoCore.AST.AssociativeAST.VarDeclNode(); arg1Decl.NameNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("a"); // Build the type of arg1 ProtoCore.Type arg1Type = new ProtoCore.Type(); arg1Type.Initialize(); arg1Type.UID = (int)ProtoCore.PrimitiveType.Integer; arg1Type.Name = ProtoCore.DSDefinitions.Keyword.Int; arg1Decl.ArgumentType = arg1Type; funcDefNode.Signature.AddArgument(arg1Decl); // Function Return type ProtoCore.Type returnType = new ProtoCore.Type(); returnType.Initialize(); returnType.UID = (int)ProtoCore.PrimitiveType.Var; returnType.Name = ProtoCore.DSDefinitions.Keyword.Var; funcDefNode.ReturnType = returnType; // Build the statement that calls the function foo ProtoCore.AST.AssociativeAST.FunctionCallNode functionCall = new ProtoCore.AST.AssociativeAST.FunctionCallNode(); functionCall.Function = new ProtoCore.AST.AssociativeAST.IdentifierNode(functionName); List <ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List <ProtoCore.AST.AssociativeAST.AssociativeNode>(); astList.Add(funcDefNode); // Function call // Function args List <ProtoCore.AST.AssociativeAST.AssociativeNode> args = new List <ProtoCore.AST.AssociativeAST.AssociativeNode>(); args.Add(new ProtoCore.AST.AssociativeAST.IntNode(1)); functionCall.FormalArguments = args; // Call the function ProtoCore.AST.AssociativeAST.BinaryExpressionNode callstmt = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("x"), functionCall, ProtoCore.DSASM.Operator.assign); astList.Add(callstmt); // 2. Execute AST and verify mirror = thisTest.RunASTSource(astList); thisTest.Verify("x", result1); // 3. Convert AST to source ProtoCore.CodeGenDS codegenDS = new ProtoCore.CodeGenDS(astList); string code = codegenDS.GenerateCode(); // 4. Execute source and verify mirror = thisTest.RunScriptSource(code); thisTest.Verify("x", result1); }
public void TestRoundTrip_FunctionDefAndCall_01() { //================================= // 1. Build AST // 2. Execute AST and verify // 3. Convert AST to source // 4. Execute source and verify //================================= int result1 = 20; ExecutionMirror mirror = null; // 1. Build the AST tree // def foo() // { // b = 10; // return = b + 10; // } // // x = foo(); ProtoCore.AST.AssociativeAST.CodeBlockNode cbn = new ProtoCore.AST.AssociativeAST.CodeBlockNode(); // Build the function body ProtoCore.AST.AssociativeAST.BinaryExpressionNode assignment1 = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("b"), new ProtoCore.AST.AssociativeAST.IntNode(10), ProtoCore.DSASM.Operator.assign); ProtoCore.AST.AssociativeAST.BinaryExpressionNode returnExpr = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("b"), new ProtoCore.AST.AssociativeAST.IntNode(10), ProtoCore.DSASM.Operator.add); ProtoCore.AST.AssociativeAST.BinaryExpressionNode returnNode = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode(ProtoCore.DSDefinitions.Keyword.Return), returnExpr, ProtoCore.DSASM.Operator.assign); cbn.Body.Add(assignment1); cbn.Body.Add(returnNode); // Build the function definition foo const string functionName = "foo"; ProtoCore.AST.AssociativeAST.FunctionDefinitionNode funcDefNode = new ProtoCore.AST.AssociativeAST.FunctionDefinitionNode(); funcDefNode.Name = functionName; funcDefNode.FunctionBody = cbn; // Function Return type ProtoCore.Type returnType = new ProtoCore.Type(); returnType.Initialize(); returnType.UID = (int)ProtoCore.PrimitiveType.kTypeVar; returnType.Name = ProtoCore.DSDefinitions.Keyword.Var; funcDefNode.ReturnType = returnType; List <ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List <ProtoCore.AST.AssociativeAST.AssociativeNode>(); astList.Add(funcDefNode); // Build the statement that calls the function foo ProtoCore.AST.AssociativeAST.FunctionCallNode functionCall = new ProtoCore.AST.AssociativeAST.FunctionCallNode(); functionCall.Function = new ProtoCore.AST.AssociativeAST.IdentifierNode(functionName); ProtoCore.AST.AssociativeAST.BinaryExpressionNode callstmt = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("x"), functionCall, ProtoCore.DSASM.Operator.assign); astList.Add(callstmt); // 2. Execute AST and verify mirror = thisTest.RunASTSource(astList); Assert.IsTrue((Int64)mirror.GetValue("x").Payload == result1); // 3. Convert AST to source ProtoCore.CodeGenDS codegenDS = new ProtoCore.CodeGenDS(astList); string code = codegenDS.GenerateCode(); Console.WriteLine(code); // 4. Execute source and verify mirror = thisTest.RunScriptSource(code); Assert.IsTrue((Int64)mirror.GetValue("x").Payload == result1); }
public void TestRoundTrip_ClassDecl_MemFunctionCall_01() { int result1 = 20; ExecutionMirror mirror = null; List <ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List <ProtoCore.AST.AssociativeAST.AssociativeNode>(); // Create an exact copy of the AST list to pass to the source conversion // This needs to be done because the astlist to be run will be SSA'd on the AST execution run List <ProtoCore.AST.AssociativeAST.AssociativeNode> astListcopy = new List <ProtoCore.AST.AssociativeAST.AssociativeNode>(); // 1. Build AST // class bar // { // f : var // def foo (b:int) // { // b = 10; // return = b + 10; // } // } // // p = bar.bar(); // a = p.foo(); ProtoCore.AST.AssociativeAST.CodeBlockNode cbn = new ProtoCore.AST.AssociativeAST.CodeBlockNode(); // Build the function body ProtoCore.AST.AssociativeAST.BinaryExpressionNode assignment1 = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("b"), new ProtoCore.AST.AssociativeAST.IntNode(10), ProtoCore.DSASM.Operator.assign); ProtoCore.AST.AssociativeAST.BinaryExpressionNode returnExpr = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("b"), new ProtoCore.AST.AssociativeAST.IntNode(10), ProtoCore.DSASM.Operator.add); ProtoCore.AST.AssociativeAST.BinaryExpressionNode returnNode = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode(ProtoCore.DSDefinitions.Keyword.Return), returnExpr, ProtoCore.DSASM.Operator.assign); cbn.Body.Add(assignment1); cbn.Body.Add(returnNode); // Build the function definition foo const string functionName = "foo"; ProtoCore.AST.AssociativeAST.FunctionDefinitionNode funcDefNode = new ProtoCore.AST.AssociativeAST.FunctionDefinitionNode(); funcDefNode.Name = functionName; funcDefNode.FunctionBody = cbn; // Function Return type ProtoCore.Type returnType = new ProtoCore.Type(); returnType.Initialize(); returnType.UID = (int)ProtoCore.PrimitiveType.kTypeVar; returnType.Name = ProtoCore.DSDefinitions.Keyword.Var; funcDefNode.ReturnType = returnType; // Create the class node AST ProtoCore.AST.AssociativeAST.ClassDeclNode classDefNode = new ProtoCore.AST.AssociativeAST.ClassDeclNode(); classDefNode.className = "bar"; // Add the member function 'foo' classDefNode.funclist.Add(funcDefNode); // Create the property AST ProtoCore.AST.AssociativeAST.VarDeclNode varDeclNode = new ProtoCore.AST.AssociativeAST.VarDeclNode(); varDeclNode.Name = "f"; varDeclNode.NameNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("f"); varDeclNode.ArgumentType = new ProtoCore.Type() { Name = "int", rank = 0, UID = (int)ProtoCore.PrimitiveType.kTypeInt }; classDefNode.varlist.Add(varDeclNode); // Add the constructed class AST astList.Add(classDefNode); astListcopy.Add(new ProtoCore.AST.AssociativeAST.ClassDeclNode(classDefNode)); // p = bar.bar(); ProtoCore.AST.AssociativeAST.FunctionCallNode constructorCall = new ProtoCore.AST.AssociativeAST.FunctionCallNode(); constructorCall.Function = new ProtoCore.AST.AssociativeAST.IdentifierNode("bar"); ProtoCore.AST.AssociativeAST.IdentifierListNode identListConstrcctorCall = new ProtoCore.AST.AssociativeAST.IdentifierListNode(); identListConstrcctorCall.LeftNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("bar"); identListConstrcctorCall.RightNode = constructorCall; ProtoCore.AST.AssociativeAST.BinaryExpressionNode stmtInitClass = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("p"), identListConstrcctorCall, ProtoCore.DSASM.Operator.assign); astList.Add(stmtInitClass); astListcopy.Add(new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(stmtInitClass)); // a = p.f; ProtoCore.AST.AssociativeAST.FunctionCallNode functionCall = new ProtoCore.AST.AssociativeAST.FunctionCallNode(); functionCall.Function = new ProtoCore.AST.AssociativeAST.IdentifierNode("foo"); ProtoCore.AST.AssociativeAST.IdentifierListNode identListFunctionCall = new ProtoCore.AST.AssociativeAST.IdentifierListNode(); identListFunctionCall.LeftNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("p"); identListFunctionCall.RightNode = functionCall; ProtoCore.AST.AssociativeAST.BinaryExpressionNode stmtPropertyAccess = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("a"), identListFunctionCall, ProtoCore.DSASM.Operator.assign); astList.Add(stmtPropertyAccess); astListcopy.Add(new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(stmtPropertyAccess)); // 2. Execute AST and verify mirror = thisTest.RunASTSource(astList); Assert.IsTrue((Int64)mirror.GetValue("a").Payload == result1); // 3. Convert AST to source ProtoCore.CodeGenDS codegenDS = new ProtoCore.CodeGenDS(astListcopy); string code = codegenDS.GenerateCode(); // 4. Execute source and verify mirror = thisTest.RunScriptSource(code); Assert.IsTrue((Int64)mirror.GetValue("a").Payload == result1); }
public void TestRoundTrip_ClassDecl_PropertyAccess_01() { int result1 = 10; ExecutionMirror mirror = null; List <ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List <ProtoCore.AST.AssociativeAST.AssociativeNode>(); // Create an exact copy of the AST list to pass to the source conversion // This needs to be done because the astlist to be run will be SSA'd on the AST execution run List <ProtoCore.AST.AssociativeAST.AssociativeNode> astListcopy = new List <ProtoCore.AST.AssociativeAST.AssociativeNode>(); // 1. Build AST // class bar // { // f : var; // } // // p = bar.bar(); // p.f = 10; // a = p.f; // Create the class node AST ProtoCore.AST.AssociativeAST.ClassDeclNode classDefNode = new ProtoCore.AST.AssociativeAST.ClassDeclNode(); classDefNode.className = "bar"; // Create the property AST ProtoCore.AST.AssociativeAST.VarDeclNode varDeclNode = new ProtoCore.AST.AssociativeAST.VarDeclNode(); varDeclNode.Name = "f"; varDeclNode.NameNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("f"); varDeclNode.ArgumentType = new ProtoCore.Type() { Name = "int", rank = 0, UID = (int)ProtoCore.PrimitiveType.kTypeInt }; classDefNode.varlist.Add(varDeclNode); astList.Add(classDefNode); astListcopy.Add(new ProtoCore.AST.AssociativeAST.ClassDeclNode(classDefNode)); // p = bar.bar(); ProtoCore.AST.AssociativeAST.FunctionCallNode constructorCall = new ProtoCore.AST.AssociativeAST.FunctionCallNode(); constructorCall.Function = new ProtoCore.AST.AssociativeAST.IdentifierNode("bar"); ProtoCore.AST.AssociativeAST.IdentifierListNode identListConstrcctorCall = new ProtoCore.AST.AssociativeAST.IdentifierListNode(); identListConstrcctorCall.LeftNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("bar"); identListConstrcctorCall.RightNode = constructorCall; ProtoCore.AST.AssociativeAST.BinaryExpressionNode stmtInitClass = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("p"), identListConstrcctorCall, ProtoCore.DSASM.Operator.assign); astList.Add(stmtInitClass); astListcopy.Add(new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(stmtInitClass)); // p.f = 10; ProtoCore.AST.AssociativeAST.IdentifierListNode identListPropertySet = new ProtoCore.AST.AssociativeAST.IdentifierListNode(); identListPropertySet.LeftNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("p"); identListPropertySet.RightNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("f"); ProtoCore.AST.AssociativeAST.BinaryExpressionNode stmtPropertySet = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( identListPropertySet, new ProtoCore.AST.AssociativeAST.IntNode(10), ProtoCore.DSASM.Operator.assign); astList.Add(stmtPropertySet); astListcopy.Add(new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(stmtPropertySet)); // a = p.f; ProtoCore.AST.AssociativeAST.IdentifierListNode identListPropertyAccess = new ProtoCore.AST.AssociativeAST.IdentifierListNode(); identListPropertyAccess.LeftNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("p"); identListPropertyAccess.RightNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("f"); ProtoCore.AST.AssociativeAST.BinaryExpressionNode stmtPropertyAccess = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("a"), identListPropertyAccess, ProtoCore.DSASM.Operator.assign); astList.Add(stmtPropertyAccess); astListcopy.Add(new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(stmtPropertyAccess)); // 2. Execute AST and verify mirror = thisTest.RunASTSource(astList); Assert.IsTrue((Int64)mirror.GetValue("a").Payload == result1); // 3. Convert AST to source ProtoCore.CodeGenDS codegenDS = new ProtoCore.CodeGenDS(astListcopy); string code = codegenDS.GenerateCode(); // 4. Execute source and verify mirror = thisTest.RunScriptSource(code); Assert.IsTrue((Int64)mirror.GetValue("a").Payload == result1); }
} } class B extends A { constructor B() { x = 2; } } ptrA = A.A(); ax = ptrA.x; ptrB = B.B(); bx = ptrB.x; "; ExecutionMirror mirror = thisTest.RunScriptSource(code); Assert.IsTrue((Int64)mirror.GetValue("ax").Payload == 1); Assert.IsTrue((Int64)mirror.GetValue("bx").Payload == 2); } [Test] public void TestClasses05() { String code = @" def sum : double (p : double) { return = p + 10.0; } class Obj { val : var; mx : var; my : var; mz : var; constructor Obj(xx : double, yy : double, zz : double) { mx = xx; my = yy; mz = zz; val = sum(zz); } } p = Obj.Obj(0.0, 1.0, 2.0); x = p.val; "; ExecutionMirror mirror = thisTest.RunScriptSource(code); Assert.IsTrue((double)mirror.GetValue("x").Payload == 12); } [Test] public void TestClasses06() { String code = @" class Point { mx : var; my : var; mz : var; constructor ByCoordinates(x : int, y : int, z : int) { mx = x; my = y; mz = z; } } class BSplineCurve { mpts : var[]; constructor ByPoints(ptsOnCurve : Point[]) { mpts = ptsOnCurve; } } pt1 = Point.ByCoordinates(1,2,3); pt2 = Point.ByCoordinates(4,5,6); pt3 = Point.ByCoordinates(7,8,9); pt4 = Point.ByCoordinates(10,11,12); pt5 = Point.ByCoordinates(15,16,17); pts = {pt1, pt2, pt3, pt4, pt5};
public void TestProtoASTExecute_Assign04() { GraphToDSCompiler.GraphCompiler gc = GraphToDSCompiler.GraphCompiler.CreateInstance(); /*b = 30*/ ProtoCore.AST.AssociativeAST.BinaryExpressionNode nodeB = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("b"), new ProtoCore.AST.AssociativeAST.IntNode(30), ProtoCore.DSASM.Operator.assign); /*a = (b - 10) * 20 + (b + 10) * (b - 20) */ ProtoCore.AST.AssociativeAST.BinaryExpressionNode assignment = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("a"), new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("b"), new ProtoCore.AST.AssociativeAST.IntNode(10), ProtoCore.DSASM.Operator.sub), new ProtoCore.AST.AssociativeAST.IntNode(20), ProtoCore.DSASM.Operator.mul), new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("b"), new ProtoCore.AST.AssociativeAST.IntNode(10), ProtoCore.DSASM.Operator.add), new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("b"), new ProtoCore.AST.AssociativeAST.IntNode(20), ProtoCore.DSASM.Operator.sub), ProtoCore.DSASM.Operator.mul), ProtoCore.DSASM.Operator.add), ProtoCore.DSASM.Operator.assign); /*c = a*/ ProtoCore.AST.AssociativeAST.BinaryExpressionNode nodeC = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("c"), new ProtoCore.AST.AssociativeAST.IdentifierNode("a"), ProtoCore.DSASM.Operator.assign); /*a = a + 1000*/ ProtoCore.AST.AssociativeAST.BinaryExpressionNode assignment2 = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("a"), new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("a"), new ProtoCore.AST.AssociativeAST.IntNode(1000), ProtoCore.DSASM.Operator.add), ProtoCore.DSASM.Operator.assign); List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>(); astList.Add(nodeB); astList.Add(assignment); astList.Add(nodeC); astList.Add(assignment2); ExecutionMirror mirror = thisTest.RunASTSource(astList); //a = 1800, c = a = 1800 Obj o = mirror.GetValue("a"); Assert.IsTrue((Int64)o.Payload == 1800); o = mirror.GetValue("c"); Assert.IsTrue((Int64)o.Payload == 1800); }
public void DFSTraverse(ref ProtoCore.AST.AssociativeAST.AssociativeNode node) { if (node is ProtoCore.AST.AssociativeAST.IdentifierNode) { EmitIdentifierNode(ref node); } else if (node is ProtoCore.AST.AssociativeAST.IdentifierListNode) { ProtoCore.AST.AssociativeAST.IdentifierListNode identList = node as ProtoCore.AST.AssociativeAST.IdentifierListNode; EmitIdentifierListNode(ref identList); } else if (node is ProtoCore.AST.AssociativeAST.IntNode) { ProtoCore.AST.AssociativeAST.IntNode intNode = node as ProtoCore.AST.AssociativeAST.IntNode; EmitIntNode(ref intNode); } else if (node is ProtoCore.AST.AssociativeAST.DoubleNode) { ProtoCore.AST.AssociativeAST.DoubleNode doubleNode = node as ProtoCore.AST.AssociativeAST.DoubleNode; EmitDoubleNode(ref doubleNode); } else if (node is ProtoCore.AST.AssociativeAST.FunctionCallNode) { ProtoCore.AST.AssociativeAST.FunctionCallNode funcCallNode = node as ProtoCore.AST.AssociativeAST.FunctionCallNode; EmitFunctionCallNode(ref funcCallNode); } else if (node is ProtoCore.AST.AssociativeAST.FunctionDotCallNode) { ProtoCore.AST.AssociativeAST.FunctionDotCallNode funcDotCall = node as ProtoCore.AST.AssociativeAST.FunctionDotCallNode; EmitFunctionDotCallNode(ref funcDotCall); } else if (node is ProtoCore.AST.AssociativeAST.BinaryExpressionNode) { ProtoCore.AST.AssociativeAST.BinaryExpressionNode binaryExpr = node as ProtoCore.AST.AssociativeAST.BinaryExpressionNode; if (binaryExpr.Optr != ProtoCore.DSASM.Operator.assign) { ; } EmitBinaryNode(ref binaryExpr); if (binaryExpr.Optr == ProtoCore.DSASM.Operator.assign) { } if (binaryExpr.Optr != ProtoCore.DSASM.Operator.assign) { ; } } else if (node is ProtoCore.AST.AssociativeAST.FunctionDefinitionNode) { ProtoCore.AST.AssociativeAST.FunctionDefinitionNode funcDefNode = node as ProtoCore.AST.AssociativeAST.FunctionDefinitionNode; EmitFunctionDefNode(ref funcDefNode); } else if (node is ProtoCore.AST.AssociativeAST.ClassDeclNode) { ProtoCore.AST.AssociativeAST.ClassDeclNode classDeclNode = node as ProtoCore.AST.AssociativeAST.ClassDeclNode; EmitClassDeclNode(ref classDeclNode); } else if (node is ProtoCore.AST.AssociativeAST.NullNode) { ProtoCore.AST.AssociativeAST.NullNode nullNode = node as ProtoCore.AST.AssociativeAST.NullNode; EmitNullNode(ref nullNode); } else if (node is ProtoCore.AST.AssociativeAST.ArrayIndexerNode) { ProtoCore.AST.AssociativeAST.ArrayIndexerNode arrIdxNode = node as ProtoCore.AST.AssociativeAST.ArrayIndexerNode; EmitArrayIndexerNode(ref arrIdxNode); } else if (node is ProtoCore.AST.AssociativeAST.ExprListNode) { ProtoCore.AST.AssociativeAST.ExprListNode exprListNode = node as ProtoCore.AST.AssociativeAST.ExprListNode; EmitExprListNode(ref exprListNode); } }
public void TestCodeGenDS_Assign04() { /*b = 30*/ ProtoCore.AST.AssociativeAST.BinaryExpressionNode nodeB = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("b"), new ProtoCore.AST.AssociativeAST.IntNode(30), ProtoCore.DSASM.Operator.assign); /*a = (b - 10) * 20 + (b + 10) * (b - 20) */ ProtoCore.AST.AssociativeAST.BinaryExpressionNode assignment = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("a"), new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("b"), new ProtoCore.AST.AssociativeAST.IntNode(10), ProtoCore.DSASM.Operator.sub), new ProtoCore.AST.AssociativeAST.IntNode(20), ProtoCore.DSASM.Operator.mul), new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("b"), new ProtoCore.AST.AssociativeAST.IntNode(10), ProtoCore.DSASM.Operator.add), new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("b"), new ProtoCore.AST.AssociativeAST.IntNode(20), ProtoCore.DSASM.Operator.sub), ProtoCore.DSASM.Operator.mul), ProtoCore.DSASM.Operator.add), ProtoCore.DSASM.Operator.assign); /*c = a*/ ProtoCore.AST.AssociativeAST.BinaryExpressionNode nodeC = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("c"), new ProtoCore.AST.AssociativeAST.IdentifierNode("a"), ProtoCore.DSASM.Operator.assign); /*a = a + 1000*/ ProtoCore.AST.AssociativeAST.BinaryExpressionNode assignment2 = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("a"), new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("a"), new ProtoCore.AST.AssociativeAST.IntNode(1000), ProtoCore.DSASM.Operator.add), ProtoCore.DSASM.Operator.assign); List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>(); astList.Add(nodeB); astList.Add(assignment); astList.Add(nodeC); astList.Add(assignment2); ProtoCore.CodeGenDS codegen = new ProtoCore.CodeGenDS(astList); string code = codegen.GenerateCode(); ExecutionMirror mirror = thisTest.RunScriptSource(code); //a = 1800, c = a = 1800 Obj o = mirror.GetValue("a"); Assert.IsTrue((Int64)o.Payload == 1800); o = mirror.GetValue("c"); Assert.IsTrue((Int64)o.Payload == 1800); }
public void TestCodeGenDS_FunctionDefNode1() { ProtoCore.AST.AssociativeAST.CodeBlockNode cbn = new ProtoCore.AST.AssociativeAST.CodeBlockNode(); ProtoCore.AST.AssociativeAST.BinaryExpressionNode assignment1 = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("b"), new ProtoCore.AST.AssociativeAST.IntNode(10), ProtoCore.DSASM.Operator.assign); ProtoCore.AST.AssociativeAST.BinaryExpressionNode returnExpr = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("b"), new ProtoCore.AST.AssociativeAST.IntNode(10), ProtoCore.DSASM.Operator.add); var returnIdent = new ProtoCore.AST.AssociativeAST.IdentifierNode("return"); ProtoCore.AST.AssociativeAST.BinaryExpressionNode returnNode = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(returnIdent, returnExpr); cbn.Body.Add(assignment1); cbn.Body.Add(returnNode); /// ProtoCore.AST.AssociativeAST.FunctionDefinitionNode funcDefNode = new ProtoCore.AST.AssociativeAST.FunctionDefinitionNode(); funcDefNode.Name = "foo"; funcDefNode.FunctionBody = cbn; /* def foo() * { * b = 10; * return = b + 10; * }*/ List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>();
private ProtoCore.AST.AssociativeAST.BinaryExpressionNode CreateDeclareArrayNode(string name, int[] intList) { ProtoCore.AST.AssociativeAST.ExprListNode expr = CreateExprListNodeFromArray(intList); ProtoCore.AST.AssociativeAST.BinaryExpressionNode declareArrayNode = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode(name), expr, ProtoCore.DSASM.Operator.assign); return declareArrayNode; }
public void TestProtoASTExecute_Assign05() { /*b = 30*/ ProtoCore.AST.AssociativeAST.BinaryExpressionNode nodeB = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("b"), new ProtoCore.AST.AssociativeAST.IntNode(30), ProtoCore.DSASM.Operator.assign); /*c = b + 30*/ ProtoCore.AST.AssociativeAST.BinaryExpressionNode nodeC = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("c"), new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("b"), new ProtoCore.AST.AssociativeAST.IntNode(30), ProtoCore.DSASM.Operator.add), ProtoCore.DSASM.Operator.assign); /*a = (b + 20) - (c - 10) + c*5 */ ProtoCore.AST.AssociativeAST.BinaryExpressionNode assignment = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("a"), new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("b"), new ProtoCore.AST.AssociativeAST.IntNode(20), ProtoCore.DSASM.Operator.add), new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("c"), new ProtoCore.AST.AssociativeAST.IntNode(10), ProtoCore.DSASM.Operator.sub), ProtoCore.DSASM.Operator.sub), new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("c"), new ProtoCore.AST.AssociativeAST.IntNode(5), ProtoCore.DSASM.Operator.mul), ProtoCore.DSASM.Operator.add), ProtoCore.DSASM.Operator.assign); List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>(); astList.Add(nodeB); astList.Add(nodeC); astList.Add(assignment); /*a = 300, b = 30, c= 60 */ ExecutionMirror mirror = thisTest.RunASTSource(astList); Obj o = mirror.GetValue("a"); Assert.IsTrue((Int64)o.Payload == 300); }
public void TestProtoASTExecute_FunctionDefAndCall_03() { // def add(a : int, b : int) // { // return = a + b; // } // // x = add(2,3); ProtoCore.AST.AssociativeAST.CodeBlockNode cbn = new ProtoCore.AST.AssociativeAST.CodeBlockNode(); // Build the function body ProtoCore.AST.AssociativeAST.BinaryExpressionNode returnExpr = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("a"), new ProtoCore.AST.AssociativeAST.IdentifierNode("b"), ProtoCore.DSASM.Operator.add); ProtoCore.AST.AssociativeAST.BinaryExpressionNode returnNode = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode(ProtoCore.DSDefinitions.Keyword.Return), returnExpr, ProtoCore.DSASM.Operator.assign); cbn.Body.Add(returnNode); // Build the function definition foo const string functionName = "foo"; ProtoCore.AST.AssociativeAST.FunctionDefinitionNode funcDefNode = new ProtoCore.AST.AssociativeAST.FunctionDefinitionNode(); funcDefNode.Name = functionName; funcDefNode.FunctionBody = cbn; // build the args signature funcDefNode.Signature = new ProtoCore.AST.AssociativeAST.ArgumentSignatureNode(); // Build arg1 ProtoCore.AST.AssociativeAST.VarDeclNode arg1Decl = new ProtoCore.AST.AssociativeAST.VarDeclNode(); arg1Decl.NameNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("a"); // Build the type of arg1 ProtoCore.Type arg1Type = new ProtoCore.Type(); arg1Type.Initialize(); arg1Type.UID = (int)ProtoCore.PrimitiveType.Integer; arg1Type.Name = ProtoCore.DSDefinitions.Keyword.Int; arg1Decl.ArgumentType = arg1Type; funcDefNode.Signature.AddArgument(arg1Decl); // Build arg2 ProtoCore.AST.AssociativeAST.VarDeclNode arg2Decl = new ProtoCore.AST.AssociativeAST.VarDeclNode(); arg2Decl.NameNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("b"); // Build the type of arg2 ProtoCore.Type arg2Type = new ProtoCore.Type(); arg2Type.Initialize(); arg2Type.UID = (int)ProtoCore.PrimitiveType.Integer; arg2Type.Name = ProtoCore.DSDefinitions.Keyword.Int; arg2Decl.ArgumentType = arg2Type; funcDefNode.Signature.AddArgument(arg2Decl); // Function Return type ProtoCore.Type returnType = new ProtoCore.Type(); returnType.Initialize(); returnType.UID = (int)ProtoCore.PrimitiveType.Var; returnType.Name = ProtoCore.DSDefinitions.Keyword.Var; funcDefNode.ReturnType = returnType; // Build the statement that calls the function foo ProtoCore.AST.AssociativeAST.FunctionCallNode functionCall = new ProtoCore.AST.AssociativeAST.FunctionCallNode(); functionCall.Function = new ProtoCore.AST.AssociativeAST.IdentifierNode(functionName); List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>(); astList.Add(funcDefNode); // Function call // Function args List<ProtoCore.AST.AssociativeAST.AssociativeNode> args = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>(); args.Add(new ProtoCore.AST.AssociativeAST.IntNode(2)); args.Add(new ProtoCore.AST.AssociativeAST.IntNode(3)); functionCall.FormalArguments = args; // Call the function ProtoCore.AST.AssociativeAST.BinaryExpressionNode callstmt = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("x"), functionCall, ProtoCore.DSASM.Operator.assign); astList.Add(callstmt); ExecutionMirror mirror = thisTest.RunASTSource(astList); Obj o = mirror.GetValue("x"); Assert.IsTrue((Int64)o.Payload == 5); }
// Generate the script ProtoCore.CodeGenDS codegen = new ProtoCore.CodeGenDS(astList); string code = codegen.GenerateCode(); ExecutionMirror mirror = thisTest.RunScriptSource(code); Assert.IsTrue((Int64)mirror.GetValue("a").Payload == 12); } [Test] public void TestProtoASTExecute_Imperative_Assign01() { // // a = [Imperative] // { // return = 10; // } // List<ProtoCore.AST.ImperativeAST.ImperativeNode> imperativeList = new List<ProtoCore.AST.ImperativeAST.ImperativeNode>(); // return = 10 ProtoCore.AST.ImperativeAST.BinaryExpressionNode imperativeAssign = new ProtoCore.AST.ImperativeAST.BinaryExpressionNode( new ProtoCore.AST.ImperativeAST.IdentifierNode("return"), new ProtoCore.AST.ImperativeAST.IntNode(10), ProtoCore.DSASM.Operator.assign); imperativeList.Add(imperativeAssign); // Build the language block ProtoCore.AST.ImperativeAST.CodeBlockNode imperativeCodeBlock = new ProtoCore.AST.ImperativeAST.CodeBlockNode(); imperativeCodeBlock.Body = imperativeList; ProtoCore.AST.AssociativeAST.LanguageBlockNode langblock = new ProtoCore.AST.AssociativeAST.LanguageBlockNode(); langblock.codeblock = new ProtoCore.LanguageCodeBlock(ProtoCore.Language.Imperative); langblock.CodeBlockNode = imperativeCodeBlock; // Build an assignment where the rhs is the imperative block ProtoCore.AST.AssociativeAST.BinaryExpressionNode assign = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("a"), langblock, ProtoCore.DSASM.Operator.assign);
public SourceGen(ProtoCore.AST.AssociativeAST.BinaryExpressionNode bNode) { ChildTree = bNode; }
List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>(); astList.Add(assign); // Verify the results ExecutionMirror mirror = thisTest.RunASTSource(astList); Obj o = mirror.GetValue("a"); Assert.IsTrue((Int64)o.Payload == 12); } [Test] public void TestLocalizedStringInAST() { // Build the AST tree ProtoCore.AST.AssociativeAST.BinaryExpressionNode assign = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
/// <summary> /// Depth first traversal of an AST node /// </summary> /// <param name="node"></param> public void DFSTraverse(ProtoCore.AST.Node node, bool useByProtoAst = false) { if (node is ProtoCore.AST.AssociativeAST.ImportNode) { EmitImportNode(node as ProtoCore.AST.AssociativeAST.ImportNode); } else if (node is ProtoCore.AST.AssociativeAST.IdentifierNode) { //if (useByProtoAst) //{ // ProtoCore.AST.AssociativeAST.IdentifierNode iNode = ChildTree.LeftNode as ProtoCore.AST.AssociativeAST.IdentifierNode; // Validity.Assert(iNode != null); // if ((node as ProtoCore.AST.AssociativeAST.IdentifierNode).Value == iNode.Value) // node = ChildTree; //} //else EmitIdentifierNode(node as ProtoCore.AST.AssociativeAST.IdentifierNode); } else if (node is ProtoCore.AST.AssociativeAST.IdentifierListNode) { EmitIdentifierListNode(node as ProtoCore.AST.AssociativeAST.IdentifierListNode); } else if (node is ProtoCore.AST.AssociativeAST.IntNode) { EmitIntNode(node as ProtoCore.AST.AssociativeAST.IntNode); } else if (node is ProtoCore.AST.AssociativeAST.DoubleNode) { EmitDoubleNode(node as ProtoCore.AST.AssociativeAST.DoubleNode); } else if (node is ProtoCore.AST.AssociativeAST.FunctionCallNode) { EmitFunctionCallNode(node as ProtoCore.AST.AssociativeAST.FunctionCallNode); } else if (node is ProtoCore.AST.AssociativeAST.FunctionDotCallNode) { EmitFunctionDotCallNode(node as ProtoCore.AST.AssociativeAST.FunctionDotCallNode); } else if (node is ProtoCore.AST.AssociativeAST.BinaryExpressionNode) { ProtoCore.AST.AssociativeAST.BinaryExpressionNode binaryExpr = node as ProtoCore.AST.AssociativeAST.BinaryExpressionNode; if (binaryExpr.Optr != DSASM.Operator.assign) { EmitCode("("); } EmitBinaryNode(binaryExpr); if (binaryExpr.Optr == DSASM.Operator.assign) { EmitCode(ProtoCore.DSASM.Constants.termline); } if (binaryExpr.Optr != DSASM.Operator.assign) { EmitCode(")"); } } else if (node is ProtoCore.AST.AssociativeAST.FunctionDefinitionNode) { EmitFunctionDefNode(node as ProtoCore.AST.AssociativeAST.FunctionDefinitionNode); } else if (node is ProtoCore.AST.AssociativeAST.ClassDeclNode) { EmitClassDeclNode(node as ProtoCore.AST.AssociativeAST.ClassDeclNode); } else if (node is ProtoCore.AST.AssociativeAST.NullNode) { EmitNullNode(node as ProtoCore.AST.AssociativeAST.NullNode); } else if (node is ProtoCore.AST.AssociativeAST.RangeExprNode) { EmitRangeExprNode(node as ProtoCore.AST.AssociativeAST.RangeExprNode); } else if (node is ProtoCore.AST.AssociativeAST.ArrayIndexerNode) { EmitArrayIndexerNode(node as ProtoCore.AST.AssociativeAST.ArrayIndexerNode); } else if (node is ProtoCore.AST.AssociativeAST.ArrayNode) { EmitArrayNode(node as ProtoCore.AST.AssociativeAST.ArrayNode); } else if (node is ProtoCore.AST.AssociativeAST.ExprListNode) { EmitExprListNode(node as ProtoCore.AST.AssociativeAST.ExprListNode); } }
public void TestRoundTrip_FunctionDefAndCall_01() { //================================= // 1. Build AST // 2. Execute AST and verify // 3. Convert AST to source // 4. Execute source and verify //================================= int result1 = 20; ExecutionMirror mirror = null; // 1. Build the AST tree // def foo() // { // b = 10; // return = b + 10; // } // // x = foo(); ProtoCore.AST.AssociativeAST.CodeBlockNode cbn = new ProtoCore.AST.AssociativeAST.CodeBlockNode(); // Build the function body ProtoCore.AST.AssociativeAST.BinaryExpressionNode assignment1 = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("b"), new ProtoCore.AST.AssociativeAST.IntNode(10), ProtoCore.DSASM.Operator.assign); ProtoCore.AST.AssociativeAST.BinaryExpressionNode returnExpr = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("b"), new ProtoCore.AST.AssociativeAST.IntNode(10), ProtoCore.DSASM.Operator.add); ProtoCore.AST.AssociativeAST.BinaryExpressionNode returnNode = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode(ProtoCore.DSDefinitions.Keyword.Return), returnExpr, ProtoCore.DSASM.Operator.assign); cbn.Body.Add(assignment1); cbn.Body.Add(returnNode); // Build the function definition foo const string functionName = "foo"; ProtoCore.AST.AssociativeAST.FunctionDefinitionNode funcDefNode = new ProtoCore.AST.AssociativeAST.FunctionDefinitionNode(); funcDefNode.Name = functionName; funcDefNode.FunctionBody = cbn; // Function Return type ProtoCore.Type returnType = new ProtoCore.Type(); returnType.Initialize(); returnType.UID = (int)ProtoCore.PrimitiveType.Var; returnType.Name = ProtoCore.DSDefinitions.Keyword.Var; funcDefNode.ReturnType = returnType; List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>(); astList.Add(funcDefNode); // Build the statement that calls the function foo ProtoCore.AST.AssociativeAST.FunctionCallNode functionCall = new ProtoCore.AST.AssociativeAST.FunctionCallNode(); functionCall.Function = new ProtoCore.AST.AssociativeAST.IdentifierNode(functionName); ProtoCore.AST.AssociativeAST.BinaryExpressionNode callstmt = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("x"), functionCall, ProtoCore.DSASM.Operator.assign); astList.Add(callstmt); // 2. Execute AST and verify mirror = thisTest.RunASTSource(astList); Assert.IsTrue((Int64)mirror.GetValue("x").Payload == result1); // 3. Convert AST to source ProtoCore.CodeGenDS codegenDS = new ProtoCore.CodeGenDS(astList); string code = codegenDS.GenerateCode(); Console.WriteLine(code); // 4. Execute source and verify mirror = thisTest.RunScriptSource(code); Assert.IsTrue((Int64)mirror.GetValue("x").Payload == result1); }
public void TestCodeGenDS_Assign02() { // Build the AST tree ProtoCore.AST.AssociativeAST.BinaryExpressionNode assign = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("a"), new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IntNode(10), new ProtoCore.AST.AssociativeAST.IntNode(20), ProtoCore.DSASM.Operator.add), ProtoCore.DSASM.Operator.assign); List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>(); astList.Add(assign); // emit the DS code from the AST tree ProtoCore.CodeGenDS codegenDS = new ProtoCore.CodeGenDS(astList); string code = codegenDS.GenerateCode(); // Verify the results ExecutionMirror mirror = thisTest.RunScriptSource(code); Obj o = mirror.GetValue("a"); Assert.IsTrue((Int64)o.Payload == 30); }
public void TestProtoASTExecute_ArrayIndex_RHS_Assign03() { List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>(); // a = {1, 2, 3, 4}; int[] input = { 1, 2, 3, 4 }; ProtoCore.AST.AssociativeAST.BinaryExpressionNode declareNodeA = CreateDeclareArrayNode("a", input); astList.Add(declareNodeA); // b = -1; ProtoCore.AST.AssociativeAST.BinaryExpressionNode declareNodeB = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("b"), new ProtoCore.AST.AssociativeAST.IntNode(-1), ProtoCore.DSASM.Operator.assign); astList.Add(declareNodeB); // c = a[b + 3]; ProtoCore.AST.AssociativeAST.BinaryExpressionNode operation1 = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("b"), new ProtoCore.AST.AssociativeAST.IntNode(3), ProtoCore.DSASM.Operator.add); ProtoCore.AST.AssociativeAST.IdentifierNode nodeALHS = new ProtoCore.AST.AssociativeAST.IdentifierNode("a"); nodeALHS.ArrayDimensions = new ProtoCore.AST.AssociativeAST.ArrayNode { Expr = operation1}; ProtoCore.AST.AssociativeAST.BinaryExpressionNode nodeALHSAssignment = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("c"), nodeALHS, ProtoCore.DSASM.Operator.assign); astList.Add(nodeALHSAssignment); // Verify the results ExecutionMirror mirror = thisTest.RunASTSource(astList); Obj o = mirror.GetValue("c"); // Expected: c = 3 Assert.AreEqual(3, Convert.ToInt32(o.Payload)); }
public void TestCodeGenDS_Assign03() { /*b = 20;*/ ProtoCore.AST.AssociativeAST.BinaryExpressionNode assign2 = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("b"), new ProtoCore.AST.AssociativeAST.IntNode(20), ProtoCore.DSASM.Operator.assign); /*a = (b + 50)*(b + 20)*/ ProtoCore.AST.AssociativeAST.BinaryExpressionNode assign1 = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("a"), new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("b"), new ProtoCore.AST.AssociativeAST.IntNode(50), ProtoCore.DSASM.Operator.add), new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("b"), new ProtoCore.AST.AssociativeAST.IntNode(20), ProtoCore.DSASM.Operator.add), ProtoCore.DSASM.Operator.mul), ProtoCore.DSASM.Operator.assign); /*c = a - 200*/ ProtoCore.AST.AssociativeAST.BinaryExpressionNode assign3 = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("c"), new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("a"), new ProtoCore.AST.AssociativeAST.IntNode(200), ProtoCore.DSASM.Operator.sub), ProtoCore.DSASM.Operator.assign); /*d = b*/ ProtoCore.AST.AssociativeAST.BinaryExpressionNode assign4 = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("d"), new ProtoCore.AST.AssociativeAST.IdentifierNode("b"), ProtoCore.DSASM.Operator.assign); List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>(); astList.Add(assign2); astList.Add(assign1); astList.Add(assign3); astList.Add(assign4); ProtoCore.CodeGenDS codegen = new ProtoCore.CodeGenDS(astList); string code = codegen.GenerateCode(); ExecutionMirror mirror = thisTest.RunScriptSource(code); //a = 2800, c = 2600, d = b = 20 Obj o = mirror.GetValue("a"); Assert.IsTrue((Int64)o.Payload == 2800); o = mirror.GetValue("c"); Assert.IsTrue((Int64)o.Payload == 2600); Obj p = mirror.GetValue("b"); Assert.IsTrue((Int64)p.Payload == 20); o = mirror.GetValue("d"); Assert.IsTrue((Int64)o.Payload == 20); }
public void TestProtoASTExecute_ArrayIndex_RHS_Assign05() { List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>(); // a = { { 0, 1 }, { 3, 4, 5 } }; int[] input1 = { 0, 1 }; int[] input2 = { 2, 3, 4 }; List<ProtoCore.AST.AssociativeAST.AssociativeNode> arrayList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>(); arrayList.Add(CreateExprListNodeFromArray(input1)); arrayList.Add(CreateExprListNodeFromArray(input2)); ProtoCore.AST.AssociativeAST.ExprListNode arrayExpr = new ProtoCore.AST.AssociativeAST.ExprListNode { Exprs = arrayList }; ProtoCore.AST.AssociativeAST.BinaryExpressionNode declareNodeA = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("a"), arrayExpr, ProtoCore.DSASM.Operator.assign); astList.Add(declareNodeA); // b = 2; ProtoCore.AST.AssociativeAST.BinaryExpressionNode declareNodeB = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("b"), new ProtoCore.AST.AssociativeAST.IntNode(2), ProtoCore.DSASM.Operator.assign); astList.Add(declareNodeB); // c = a[1][b]; ProtoCore.AST.AssociativeAST.IdentifierNode nodeARHS = new ProtoCore.AST.AssociativeAST.IdentifierNode("a"); nodeARHS.ArrayDimensions = new ProtoCore.AST.AssociativeAST.ArrayNode { Expr = new ProtoCore.AST.AssociativeAST.IntNode(1), Type = new ProtoCore.AST.AssociativeAST.ArrayNode { Expr = new ProtoCore.AST.AssociativeAST.IdentifierNode("b") } }; ProtoCore.AST.AssociativeAST.BinaryExpressionNode nodeARHSAssignment = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("c"), nodeARHS, ProtoCore.DSASM.Operator.assign); astList.Add(nodeARHSAssignment); // Verify the results ExecutionMirror mirror = thisTest.RunASTSource(astList); Obj o = mirror.GetValue("c"); // expected : c = 4 Assert.AreEqual(4, Convert.ToInt32(o.Payload)); }
public void TestCodeGenDS_Assign05() { GraphToDSCompiler.GraphCompiler gc = GraphToDSCompiler.GraphCompiler.CreateInstance(); /*b = 30*/ ProtoCore.AST.AssociativeAST.BinaryExpressionNode nodeB = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("b"), new ProtoCore.AST.AssociativeAST.IntNode(30), ProtoCore.DSASM.Operator.assign); /*c = b + 30*/ ProtoCore.AST.AssociativeAST.BinaryExpressionNode nodeC = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("c"), new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("b"), new ProtoCore.AST.AssociativeAST.IntNode(30), ProtoCore.DSASM.Operator.add), ProtoCore.DSASM.Operator.assign); /*a = (b + 20) - (c - 10) + c*5 */ ProtoCore.AST.AssociativeAST.BinaryExpressionNode assignment = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("a"), new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("b"), new ProtoCore.AST.AssociativeAST.IntNode(20), ProtoCore.DSASM.Operator.add), new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("c"), new ProtoCore.AST.AssociativeAST.IntNode(10), ProtoCore.DSASM.Operator.sub), ProtoCore.DSASM.Operator.sub), new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("c"), new ProtoCore.AST.AssociativeAST.IntNode(5), ProtoCore.DSASM.Operator.mul), ProtoCore.DSASM.Operator.add), ProtoCore.DSASM.Operator.assign); List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>(); astList.Add(nodeB); astList.Add(nodeC); astList.Add(assignment); ProtoCore.CodeGenDS codegen = new ProtoCore.CodeGenDS(astList); string code = codegen.GenerateCode(); /*a = 300, b = 30, c= 60 */ ExecutionMirror mirror = thisTest.RunScriptSource(code); Obj o = mirror.GetValue("a"); Assert.IsTrue((Int64)o.Payload == 300); }
public void TestProtoASTExecute_FunctionDefAndCall_01() { // def foo() // { // b = 10; // return = b + 10; // } // // x = foo(); ProtoCore.AST.AssociativeAST.CodeBlockNode cbn = new ProtoCore.AST.AssociativeAST.CodeBlockNode(); // Build the function body ProtoCore.AST.AssociativeAST.BinaryExpressionNode assignment1 = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("b"), new ProtoCore.AST.AssociativeAST.IntNode(10), ProtoCore.DSASM.Operator.assign); ProtoCore.AST.AssociativeAST.BinaryExpressionNode returnExpr = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("b"), new ProtoCore.AST.AssociativeAST.IntNode(10), ProtoCore.DSASM.Operator.add); ProtoCore.AST.AssociativeAST.BinaryExpressionNode returnNode = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode(ProtoCore.DSDefinitions.Keyword.Return), returnExpr, ProtoCore.DSASM.Operator.assign); cbn.Body.Add(assignment1); cbn.Body.Add(returnNode); // Build the function definition foo const string functionName = "foo"; ProtoCore.AST.AssociativeAST.FunctionDefinitionNode funcDefNode = new ProtoCore.AST.AssociativeAST.FunctionDefinitionNode(); funcDefNode.Name = functionName; funcDefNode.FunctionBody = cbn; // Function Return type ProtoCore.Type returnType = new ProtoCore.Type(); returnType.Initialize(); returnType.UID = (int)ProtoCore.PrimitiveType.Var; returnType.Name = ProtoCore.DSDefinitions.Keyword.Var; funcDefNode.ReturnType = returnType; List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>(); astList.Add(funcDefNode); // Build the statement that calls the function foo ProtoCore.AST.AssociativeAST.FunctionCallNode functionCall = new ProtoCore.AST.AssociativeAST.FunctionCallNode(); functionCall.Function = new ProtoCore.AST.AssociativeAST.IdentifierNode(functionName); ProtoCore.AST.AssociativeAST.BinaryExpressionNode callstmt = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("x"), functionCall, ProtoCore.DSASM.Operator.assign); astList.Add(callstmt); ExecutionMirror mirror = thisTest.RunASTSource(astList); Obj o = mirror.GetValue("x"); Assert.IsTrue((Int64)o.Payload == 20); }
public void TestRoundTrip_ClassDecl_PropertyAccess_01() { int result1 = 10; ExecutionMirror mirror = null; List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>(); // Create an exact copy of the AST list to pass to the source conversion // This needs to be done because the astlist to be run will be SSA'd on the AST execution run List<ProtoCore.AST.AssociativeAST.AssociativeNode> astListcopy= new List<ProtoCore.AST.AssociativeAST.AssociativeNode>(); // 1. Build AST // class bar // { // f : var; // } // // p = bar.bar(); // p.f = 10; // a = p.f; // Create the class node AST ProtoCore.AST.AssociativeAST.ClassDeclNode classDefNode = new ProtoCore.AST.AssociativeAST.ClassDeclNode(); classDefNode.ClassName = "bar"; // Create the property AST ProtoCore.AST.AssociativeAST.VarDeclNode varDeclNode = new ProtoCore.AST.AssociativeAST.VarDeclNode(); varDeclNode.Name = "f"; varDeclNode.NameNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("f"); varDeclNode.ArgumentType = new ProtoCore.Type() { Name = "int", rank = 0, UID = (int)ProtoCore.PrimitiveType.Integer }; classDefNode.Variables.Add(varDeclNode); astList.Add(classDefNode); astListcopy.Add(new ProtoCore.AST.AssociativeAST.ClassDeclNode(classDefNode)); // p = bar.bar(); ProtoCore.AST.AssociativeAST.FunctionCallNode constructorCall = new ProtoCore.AST.AssociativeAST.FunctionCallNode(); constructorCall.Function = new ProtoCore.AST.AssociativeAST.IdentifierNode("bar"); ProtoCore.AST.AssociativeAST.IdentifierListNode identListConstrcctorCall = new ProtoCore.AST.AssociativeAST.IdentifierListNode(); identListConstrcctorCall.LeftNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("bar"); identListConstrcctorCall.RightNode = constructorCall; ProtoCore.AST.AssociativeAST.BinaryExpressionNode stmtInitClass = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("p"), identListConstrcctorCall, ProtoCore.DSASM.Operator.assign); astList.Add(stmtInitClass); astListcopy.Add(new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(stmtInitClass)); // p.f = 10; ProtoCore.AST.AssociativeAST.IdentifierListNode identListPropertySet = new ProtoCore.AST.AssociativeAST.IdentifierListNode(); identListPropertySet.LeftNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("p"); identListPropertySet.RightNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("f"); ProtoCore.AST.AssociativeAST.BinaryExpressionNode stmtPropertySet = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( identListPropertySet, new ProtoCore.AST.AssociativeAST.IntNode(10), ProtoCore.DSASM.Operator.assign); astList.Add(stmtPropertySet); astListcopy.Add(new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(stmtPropertySet)); // a = p.f; ProtoCore.AST.AssociativeAST.IdentifierListNode identListPropertyAccess = new ProtoCore.AST.AssociativeAST.IdentifierListNode(); identListPropertyAccess.LeftNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("p"); identListPropertyAccess.RightNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("f"); ProtoCore.AST.AssociativeAST.BinaryExpressionNode stmtPropertyAccess = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("a"), identListPropertyAccess, ProtoCore.DSASM.Operator.assign); astList.Add(stmtPropertyAccess); astListcopy.Add(new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(stmtPropertyAccess)); // 2. Execute AST and verify mirror = thisTest.RunASTSource(astList); Assert.IsTrue((Int64)mirror.GetValue("a").Payload == result1); // 3. Convert AST to source ProtoCore.CodeGenDS codegenDS = new ProtoCore.CodeGenDS(astListcopy); string code = codegenDS.GenerateCode(); // 4. Execute source and verify mirror = thisTest.RunScriptSource(code); Assert.IsTrue((Int64)mirror.GetValue("a").Payload == result1); }
public void TestProtoASTExecute_ClassDecl_PropertyAccess_01() { // class bar // { // f : var; // } // // p = bar.bar(); // p.f = 10; // a = p.f; // Create the class node AST ProtoCore.AST.AssociativeAST.ClassDeclNode classDefNode = new ProtoCore.AST.AssociativeAST.ClassDeclNode(); classDefNode.ClassName = "bar"; // Create the property AST ProtoCore.AST.AssociativeAST.VarDeclNode varDeclNode = new ProtoCore.AST.AssociativeAST.VarDeclNode(); varDeclNode.Name = "f"; varDeclNode.NameNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("f"); varDeclNode.ArgumentType = new ProtoCore.Type() { Name = "int", rank = 0, UID = (int)ProtoCore.PrimitiveType.Integer }; classDefNode.Variables.Add(varDeclNode); List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>(); astList.Add(classDefNode); // p = bar.bar(); ProtoCore.AST.AssociativeAST.FunctionCallNode constructorCall = new ProtoCore.AST.AssociativeAST.FunctionCallNode(); constructorCall.Function = new ProtoCore.AST.AssociativeAST.IdentifierNode("bar"); ProtoCore.AST.AssociativeAST.IdentifierListNode identListConstrcctorCall = new ProtoCore.AST.AssociativeAST.IdentifierListNode(); identListConstrcctorCall.LeftNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("bar"); identListConstrcctorCall.RightNode = constructorCall; ProtoCore.AST.AssociativeAST.BinaryExpressionNode stmtInitClass = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("p"), identListConstrcctorCall, ProtoCore.DSASM.Operator.assign); astList.Add(stmtInitClass); // p.f = 10; ProtoCore.AST.AssociativeAST.IdentifierListNode identListPropertySet = new ProtoCore.AST.AssociativeAST.IdentifierListNode(); identListPropertySet.LeftNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("p"); identListPropertySet.RightNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("f"); ProtoCore.AST.AssociativeAST.BinaryExpressionNode stmtPropertySet = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( identListPropertySet, new ProtoCore.AST.AssociativeAST.IntNode(10), ProtoCore.DSASM.Operator.assign); astList.Add(stmtPropertySet); // a = p.f; ProtoCore.AST.AssociativeAST.IdentifierListNode identListPropertyAccess = new ProtoCore.AST.AssociativeAST.IdentifierListNode(); identListPropertyAccess.LeftNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("p"); identListPropertyAccess.RightNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("f"); ProtoCore.AST.AssociativeAST.BinaryExpressionNode stmtPropertyAccess = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("a"), identListPropertyAccess, ProtoCore.DSASM.Operator.assign); astList.Add(stmtPropertyAccess); // Execute the AST ExecutionMirror mirror = thisTest.RunASTSource(astList); Assert.IsTrue((Int64)mirror.GetValue("a").Payload == 10); }
public void TestRoundTrip_ClassDecl_MemFunctionCall_01() { int result1 = 20; ExecutionMirror mirror = null; List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>(); // Create an exact copy of the AST list to pass to the source conversion // This needs to be done because the astlist to be run will be SSA'd on the AST execution run List<ProtoCore.AST.AssociativeAST.AssociativeNode> astListcopy = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>(); // 1. Build AST // class bar // { // f : var // def foo (b:int) // { // b = 10; // return = b + 10; // } // } // // p = bar.bar(); // a = p.foo(); ProtoCore.AST.AssociativeAST.CodeBlockNode cbn = new ProtoCore.AST.AssociativeAST.CodeBlockNode(); // Build the function body ProtoCore.AST.AssociativeAST.BinaryExpressionNode assignment1 = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("b"), new ProtoCore.AST.AssociativeAST.IntNode(10), ProtoCore.DSASM.Operator.assign); ProtoCore.AST.AssociativeAST.BinaryExpressionNode returnExpr = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("b"), new ProtoCore.AST.AssociativeAST.IntNode(10), ProtoCore.DSASM.Operator.add); ProtoCore.AST.AssociativeAST.BinaryExpressionNode returnNode = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode(ProtoCore.DSDefinitions.Keyword.Return), returnExpr, ProtoCore.DSASM.Operator.assign); cbn.Body.Add(assignment1); cbn.Body.Add(returnNode); // Build the function definition foo const string functionName = "foo"; ProtoCore.AST.AssociativeAST.FunctionDefinitionNode funcDefNode = new ProtoCore.AST.AssociativeAST.FunctionDefinitionNode(); funcDefNode.Name = functionName; funcDefNode.FunctionBody = cbn; // Function Return type ProtoCore.Type returnType = new ProtoCore.Type(); returnType.Initialize(); returnType.UID = (int)ProtoCore.PrimitiveType.Var; returnType.Name = ProtoCore.DSDefinitions.Keyword.Var; funcDefNode.ReturnType = returnType; // Create the class node AST ProtoCore.AST.AssociativeAST.ClassDeclNode classDefNode = new ProtoCore.AST.AssociativeAST.ClassDeclNode(); classDefNode.ClassName = "bar"; // Add the member function 'foo' classDefNode.Procedures.Add(funcDefNode); // Create the property AST ProtoCore.AST.AssociativeAST.VarDeclNode varDeclNode = new ProtoCore.AST.AssociativeAST.VarDeclNode(); varDeclNode.Name = "f"; varDeclNode.NameNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("f"); varDeclNode.ArgumentType = new ProtoCore.Type() { Name = "int", rank = 0, UID = (int)ProtoCore.PrimitiveType.Integer }; classDefNode.Variables.Add(varDeclNode); // Add the constructed class AST astList.Add(classDefNode); astListcopy.Add(new ProtoCore.AST.AssociativeAST.ClassDeclNode(classDefNode)); // p = bar.bar(); ProtoCore.AST.AssociativeAST.FunctionCallNode constructorCall = new ProtoCore.AST.AssociativeAST.FunctionCallNode(); constructorCall.Function = new ProtoCore.AST.AssociativeAST.IdentifierNode("bar"); ProtoCore.AST.AssociativeAST.IdentifierListNode identListConstrcctorCall = new ProtoCore.AST.AssociativeAST.IdentifierListNode(); identListConstrcctorCall.LeftNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("bar"); identListConstrcctorCall.RightNode = constructorCall; ProtoCore.AST.AssociativeAST.BinaryExpressionNode stmtInitClass = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("p"), identListConstrcctorCall, ProtoCore.DSASM.Operator.assign); astList.Add(stmtInitClass); astListcopy.Add(new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(stmtInitClass)); // a = p.f; ProtoCore.AST.AssociativeAST.FunctionCallNode functionCall = new ProtoCore.AST.AssociativeAST.FunctionCallNode(); functionCall.Function = new ProtoCore.AST.AssociativeAST.IdentifierNode("foo"); ProtoCore.AST.AssociativeAST.IdentifierListNode identListFunctionCall = new ProtoCore.AST.AssociativeAST.IdentifierListNode(); identListFunctionCall.LeftNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("p"); identListFunctionCall.RightNode = functionCall; ProtoCore.AST.AssociativeAST.BinaryExpressionNode stmtPropertyAccess = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("a"), identListFunctionCall, ProtoCore.DSASM.Operator.assign); astList.Add(stmtPropertyAccess); astListcopy.Add(new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(stmtPropertyAccess)); // 2. Execute AST and verify mirror = thisTest.RunASTSource(astList); Assert.IsTrue((Int64)mirror.GetValue("a").Payload == result1); // 3. Convert AST to source ProtoCore.CodeGenDS codegenDS = new ProtoCore.CodeGenDS(astListcopy); string code = codegenDS.GenerateCode(); // 4. Execute source and verify mirror = thisTest.RunScriptSource(code); Assert.IsTrue((Int64)mirror.GetValue("a").Payload == result1); }