private void EmitExprListNode(ref ProtoCore.AST.AssociativeAST.ExprListNode exprListNode) { for (int i = 0; i < exprListNode.list.Count; i++) { ProtoCore.AST.AssociativeAST.AssociativeNode node = exprListNode.list[i]; DFSTraverse(ref node); exprListNode.list[i] = node; } }
public static ProtoCore.AST.AssociativeAST.ExprListNode BuildArrayExprList(ProtoCore.AST.AssociativeAST.AssociativeNode arrayNode) { ProtoCore.AST.AssociativeAST.ExprListNode exprlist = new ProtoCore.AST.AssociativeAST.ExprListNode(); while (arrayNode is ProtoCore.AST.AssociativeAST.ArrayNode) { ProtoCore.AST.AssociativeAST.ArrayNode array = arrayNode as ProtoCore.AST.AssociativeAST.ArrayNode; exprlist.list.Add(array.Expr); arrayNode = array.Type; } return exprlist; }
public static ProtoCore.AST.AssociativeAST.ExprListNode BuildArrayExprList(ProtoCore.AST.AssociativeAST.AssociativeNode arrayNode) { ProtoCore.AST.AssociativeAST.ExprListNode exprlist = new ProtoCore.AST.AssociativeAST.ExprListNode(); while (arrayNode is ProtoCore.AST.AssociativeAST.ArrayNode) { ProtoCore.AST.AssociativeAST.ArrayNode array = arrayNode as ProtoCore.AST.AssociativeAST.ArrayNode; exprlist.list.Add(array.Expr); arrayNode = array.Type; } return(exprlist); }
private ProtoCore.AST.AssociativeAST.ExprListNode CreateExprListNodeFromArray(int[] intList) { List<ProtoCore.AST.AssociativeAST.AssociativeNode> listIntNode = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>(); for (int i = 0; i < intList.Length; i++) listIntNode.Add(new ProtoCore.AST.AssociativeAST.IntNode(intList[i])); ProtoCore.AST.AssociativeAST.ExprListNode expr = new ProtoCore.AST.AssociativeAST.ExprListNode { Exprs = listIntNode }; return expr; }
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 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 static ProtoCore.AST.AssociativeAST.FunctionDotCallNode GenerateCallDotNode(ProtoCore.AST.AssociativeAST.AssociativeNode lhs, ProtoCore.AST.AssociativeAST.FunctionCallNode rhsCall, ProtoLanguage.CompileStateTracker compileState = null) { // The function name to call string rhsName = rhsCall.Function.Name; int argNum = rhsCall.FormalArguments.Count; ProtoCore.AST.AssociativeAST.ExprListNode argList = new ProtoCore.AST.AssociativeAST.ExprListNode(); foreach (ProtoCore.AST.AssociativeAST.AssociativeNode arg in rhsCall.FormalArguments) { // The function arguments argList.list.Add(arg); } ProtoCore.AST.AssociativeAST.FunctionCallNode funCallNode = new ProtoCore.AST.AssociativeAST.FunctionCallNode(); ProtoCore.AST.AssociativeAST.IdentifierNode funcName = new ProtoCore.AST.AssociativeAST.IdentifierNode { Value = ProtoCore.DSASM.Constants.kDotArgMethodName, Name = ProtoCore.DSASM.Constants.kDotArgMethodName }; funCallNode.Function = funcName; funCallNode.Name = ProtoCore.DSASM.Constants.kDotArgMethodName; NodeUtils.CopyNodeLocation(funCallNode, lhs); int rhsIdx = ProtoCore.DSASM.Constants.kInvalidIndex; string lhsName = null; if (lhs is ProtoCore.AST.AssociativeAST.IdentifierNode) { lhsName = (lhs as ProtoCore.AST.AssociativeAST.IdentifierNode).Name; if (lhsName == ProtoCore.DSDefinitions.Keyword.This) { lhs = new ProtoCore.AST.AssociativeAST.ThisPointerNode(); } } if (compileState != null) { if (argNum >= 0) { ProtoCore.DSASM.DynamicFunctionNode dynamicFunctionNode = new ProtoCore.DSASM.DynamicFunctionNode(rhsName, new List<ProtoCore.Type>()); compileState.DynamicFunctionTable.functionTable.Add(dynamicFunctionNode); rhsIdx = compileState.DynamicFunctionTable.functionTable.Count - 1; } else { DSASM.DyanmicVariableNode dynamicVariableNode = new DSASM.DyanmicVariableNode(rhsName); compileState.DynamicVariableTable.variableTable.Add(dynamicVariableNode); rhsIdx = compileState.DynamicVariableTable.variableTable.Count - 1; } } // The first param to the dot arg (the pointer or the class name) ProtoCore.AST.AssociativeAST.IntNode rhs = new ProtoCore.AST.AssociativeAST.IntNode() { value = rhsIdx.ToString() }; funCallNode.FormalArguments.Add(lhs); // The second param which is the dynamic table index of the function to call funCallNode.FormalArguments.Add(rhs); // The array dimensions ProtoCore.AST.AssociativeAST.ExprListNode arrayDimExperList = new ProtoCore.AST.AssociativeAST.ExprListNode(); int dimCount = 0; if (rhsCall.Function is ProtoCore.AST.AssociativeAST.IdentifierNode) { // Number of dimensions ProtoCore.AST.AssociativeAST.IdentifierNode fIdent = rhsCall.Function as ProtoCore.AST.AssociativeAST.IdentifierNode; if (fIdent.ArrayDimensions != null) { arrayDimExperList = ProtoCore.Utils.CoreUtils.BuildArrayExprList(fIdent.ArrayDimensions); dimCount = arrayDimExperList.list.Count; } else if (rhsCall.ArrayDimensions != null) { arrayDimExperList = ProtoCore.Utils.CoreUtils.BuildArrayExprList(rhsCall.ArrayDimensions); dimCount = arrayDimExperList.list.Count; } else { arrayDimExperList = new ProtoCore.AST.AssociativeAST.ExprListNode(); } } funCallNode.FormalArguments.Add(arrayDimExperList); // Number of dimensions ProtoCore.AST.AssociativeAST.IntNode dimNode = new ProtoCore.AST.AssociativeAST.IntNode() { value = dimCount.ToString() }; funCallNode.FormalArguments.Add(dimNode); if (argNum >= 0) { funCallNode.FormalArguments.Add(argList); funCallNode.FormalArguments.Add(new ProtoCore.AST.AssociativeAST.IntNode() { value = argNum.ToString() }); } ProtoCore.AST.AssociativeAST.FunctionDotCallNode funDotCallNode = new ProtoCore.AST.AssociativeAST.FunctionDotCallNode(rhsCall); funDotCallNode.DotCall = funCallNode; funDotCallNode.FunctionCall.Function = rhsCall.Function; // Consider the case of "myClass.Foo(a, b)", we will have "DotCall" being // equal to "myClass" (in terms of its starting line/column), and "rhsCall" // matching with the location of "Foo(a, b)". For execution cursor to cover // this whole statement, the final "DotCall" function call node should // range from "lhs.col" to "rhs.col". // NodeUtils.SetNodeEndLocation(funDotCallNode.DotCall, rhsCall); NodeUtils.CopyNodeLocation(funDotCallNode, funDotCallNode.DotCall); return funDotCallNode; }
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 static ProtoCore.AST.AssociativeAST.FunctionDotCallNode GenerateCallDotNode(ProtoCore.AST.AssociativeAST.AssociativeNode lhs, ProtoCore.AST.AssociativeAST.FunctionCallNode rhsCall, Core core = null) { // The function name to call string rhsName = rhsCall.Function.Name; int argNum = rhsCall.FormalArguments.Count; ProtoCore.AST.AssociativeAST.ExprListNode argList = new ProtoCore.AST.AssociativeAST.ExprListNode(); foreach (ProtoCore.AST.AssociativeAST.AssociativeNode arg in rhsCall.FormalArguments) { // The function arguments argList.list.Add(arg); } ProtoCore.AST.AssociativeAST.FunctionCallNode funCallNode = new ProtoCore.AST.AssociativeAST.FunctionCallNode(); ProtoCore.AST.AssociativeAST.IdentifierNode funcName = new ProtoCore.AST.AssociativeAST.IdentifierNode { Value = ProtoCore.DSASM.Constants.kDotArgMethodName, Name = ProtoCore.DSASM.Constants.kDotArgMethodName }; funCallNode.Function = funcName; funCallNode.Name = ProtoCore.DSASM.Constants.kDotArgMethodName; NodeUtils.CopyNodeLocation(funCallNode, lhs); int rhsIdx = ProtoCore.DSASM.Constants.kInvalidIndex; string lhsName = null; if (lhs is ProtoCore.AST.AssociativeAST.IdentifierNode) { lhsName = (lhs as ProtoCore.AST.AssociativeAST.IdentifierNode).Name; if (lhsName == ProtoCore.DSDefinitions.Kw.kw_this) { lhs = new ProtoCore.AST.AssociativeAST.ThisPointerNode(); } } if (core != null) { if (argNum >= 0) { ProtoCore.DSASM.DynamicFunctionNode dynamicFunctionNode = new ProtoCore.DSASM.DynamicFunctionNode(rhsName, new List <ProtoCore.Type>()); core.DynamicFunctionTable.functionTable.Add(dynamicFunctionNode); rhsIdx = core.DynamicFunctionTable.functionTable.Count - 1; } else { DSASM.DyanmicVariableNode dynamicVariableNode = new DSASM.DyanmicVariableNode(rhsName); core.DynamicVariableTable.variableTable.Add(dynamicVariableNode); rhsIdx = core.DynamicVariableTable.variableTable.Count - 1; } } // The first param to the dot arg (the pointer or the class name) ProtoCore.AST.AssociativeAST.IntNode rhs = new ProtoCore.AST.AssociativeAST.IntNode() { value = rhsIdx.ToString() }; funCallNode.FormalArguments.Add(lhs); // The second param which is the dynamic table index of the function to call funCallNode.FormalArguments.Add(rhs); // The array dimensions ProtoCore.AST.AssociativeAST.ExprListNode arrayDimExperList = new ProtoCore.AST.AssociativeAST.ExprListNode(); int dimCount = 0; if (rhsCall.Function is ProtoCore.AST.AssociativeAST.IdentifierNode) { // Number of dimensions ProtoCore.AST.AssociativeAST.IdentifierNode fIdent = rhsCall.Function as ProtoCore.AST.AssociativeAST.IdentifierNode; if (fIdent.ArrayDimensions != null) { arrayDimExperList = ProtoCore.Utils.CoreUtils.BuildArrayExprList(fIdent.ArrayDimensions); dimCount = arrayDimExperList.list.Count; } else if (rhsCall.ArrayDimensions != null) { arrayDimExperList = ProtoCore.Utils.CoreUtils.BuildArrayExprList(rhsCall.ArrayDimensions); dimCount = arrayDimExperList.list.Count; } else { arrayDimExperList = new ProtoCore.AST.AssociativeAST.ExprListNode(); } } funCallNode.FormalArguments.Add(arrayDimExperList); // Number of dimensions ProtoCore.AST.AssociativeAST.IntNode dimNode = new ProtoCore.AST.AssociativeAST.IntNode() { value = dimCount.ToString() }; funCallNode.FormalArguments.Add(dimNode); if (argNum >= 0) { funCallNode.FormalArguments.Add(argList); funCallNode.FormalArguments.Add(new ProtoCore.AST.AssociativeAST.IntNode() { value = argNum.ToString() }); } ProtoCore.AST.AssociativeAST.FunctionDotCallNode funDotCallNode = new ProtoCore.AST.AssociativeAST.FunctionDotCallNode(rhsCall); funDotCallNode.DotCall = funCallNode; funDotCallNode.FunctionCall.Function = rhsCall.Function; // Consider the case of "myClass.Foo(a, b)", we will have "DotCall" being // equal to "myClass" (in terms of its starting line/column), and "rhsCall" // matching with the location of "Foo(a, b)". For execution cursor to cover // this whole statement, the final "DotCall" function call node should // range from "lhs.col" to "rhs.col". // NodeUtils.SetNodeEndLocation(funDotCallNode.DotCall, rhsCall); NodeUtils.CopyNodeLocation(funDotCallNode, funDotCallNode.DotCall); return(funDotCallNode); }
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); thisTest.Verify("a", new [] { new [] { 0, 1, 2}, new [] { 3, 4, 5}}); }