protected virtual void EmitIdentifierListNode(ref ProtoCore.AST.AssociativeAST.IdentifierListNode identList) { Validity.Assert(null != identList); ProtoCore.AST.AssociativeAST.AssociativeNode left = identList.LeftNode; DFSTraverse(ref left); ProtoCore.AST.AssociativeAST.AssociativeNode right = identList.RightNode; DFSTraverse(ref right); }
protected virtual void EmitIdentifierListNode(ProtoCore.AST.AssociativeAST.IdentifierListNode identList) { Validity.Assert(null != identList); //ProtoCore.AST.AssociativeAST.AssociativeNode left = identList.LeftNode; DFSTraverse(identList.LeftNode); EmitCode("."); DFSTraverse(identList.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_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); }
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_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); }
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 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); }
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 GraphILTest_FFIClassUsage_03() { /* def f() { * X = 10; * return = X; * } */ ProtoCore.AST.AssociativeAST.BinaryExpressionNode assign1 = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("X"), new ProtoCore.AST.AssociativeAST.IntNode(10), ProtoCore.DSASM.Operator.assign); ProtoCore.AST.AssociativeAST.IdentifierNode returnExpr = new ProtoCore.AST.AssociativeAST.IdentifierNode("X"); ProtoCore.AST.AssociativeAST.ReturnNode returnNode = new ProtoCore.AST.AssociativeAST.ReturnNode(); returnNode.ReturnExpr = returnExpr; ProtoCore.AST.AssociativeAST.CodeBlockNode cbn = new ProtoCore.AST.AssociativeAST.CodeBlockNode(); cbn.Body.Add(assign1); cbn.Body.Add(returnNode); ProtoCore.AST.AssociativeAST.FunctionDefinitionNode funcDefNode = new ProtoCore.AST.AssociativeAST.FunctionDefinitionNode(); funcDefNode.FunctionBody = cbn; funcDefNode.Name = "f"; funcDefNode.ReturnType = new ProtoCore.Type() { Name = "int", UID = (int)ProtoCore.PrimitiveType.kTypeInt, //IsIndexable = false, rank = 0 }; /*Class C { }*/ ProtoCore.AST.AssociativeAST.VarDeclNode varDeclNode = new ProtoCore.AST.AssociativeAST.VarDeclNode(); varDeclNode.Name = "X"; ProtoCore.AST.AssociativeAST.IdentifierNode varDeclId = new ProtoCore.AST.AssociativeAST.IdentifierNode() { Value = "X", Name = "X", datatype = new ProtoCore.Type() { Name = "int", //IsIndexable = false, rank = 0, UID = (int)ProtoCore.PrimitiveType.kTypeInt } }; varDeclNode.NameNode = varDeclId; varDeclNode.ArgumentType = new ProtoCore.Type() { Name = "int", //IsIndexable = false, rank = 0, UID = (int)ProtoCore.PrimitiveType.kTypeVar }; ProtoCore.AST.AssociativeAST.ClassDeclNode classDeclNode = new ProtoCore.AST.AssociativeAST.ClassDeclNode(); classDeclNode.className = "C"; classDeclNode.funclist.Add(funcDefNode); classDeclNode.varlist.Add(varDeclNode); // p = new C.C(); t = p.f(); val = p.X; ProtoCore.AST.AssociativeAST.FunctionCallNode funcCallP = new ProtoCore.AST.AssociativeAST.FunctionCallNode(); funcCallP.Function = new ProtoCore.AST.AssociativeAST.IdentifierNode("C"); List<ProtoCore.AST.AssociativeAST.AssociativeNode> listArgs = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>(); funcCallP.FormalArguments = listArgs; ProtoCore.AST.AssociativeAST.FunctionDotCallNode funcDotCallNode = new ProtoCore.AST.AssociativeAST.FunctionDotCallNode("C", funcCallP); ProtoCore.AST.AssociativeAST.BinaryExpressionNode assignP = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("p"), funcDotCallNode, ProtoCore.DSASM.Operator.assign ); //p = C.C() ProtoCore.AST.AssociativeAST.FunctionCallNode funcCallT = new ProtoCore.AST.AssociativeAST.FunctionCallNode(); funcCallT.Function = new ProtoCore.AST.AssociativeAST.IdentifierNode("f"); funcCallT.FormalArguments = listArgs; funcDotCallNode = new ProtoCore.AST.AssociativeAST.FunctionDotCallNode("p", funcCallT); ProtoCore.AST.AssociativeAST.BinaryExpressionNode assignT = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("t"), funcDotCallNode, ProtoCore.DSASM.Operator.assign ); //t = p.f(); ProtoCore.AST.AssociativeAST.IdentifierListNode idListNode = new ProtoCore.AST.AssociativeAST.IdentifierListNode(); idListNode.LeftNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("p"); idListNode.Optr = ProtoCore.DSASM.Operator.dot; idListNode.RightNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("X"); ProtoCore.AST.AssociativeAST.BinaryExpressionNode assignVal = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("val"), idListNode, ProtoCore.DSASM.Operator.assign); List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>(); astList.Add(classDeclNode); astList.Add(assignP); astList.Add(assignT); astList.Add(assignVal); //============================================== // emit the DS code from the AST tree // // Class C { // X : int // def f() { // x = 2; // return = X; // } // } // p = new C(); // t = p.f(); // val = p.X; //============================================== GraphToDSCompiler.GraphCompiler gc = GraphToDSCompiler.GraphCompiler.CreateInstance(); string code = gc.Emit(astList); //============================================== // Verify the results - get the value of the x property //============================================== ExecutionMirror mirror = thisTest.RunScriptSource(code); Obj o = mirror.GetValue("val"); Assert.IsTrue((Int64)o.Payload == 10); }
public void GraphILTest_FFIClassUsage_02() { List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>(); //============================================== // Build the import Nodes //============================================== ProtoCore.AST.AssociativeAST.ImportNode importNode = new ProtoCore.AST.AssociativeAST.ImportNode(); importNode.ModuleName = "ProtoGeometry.dll"; astList.Add(importNode); //============================================== // Build the constructor call nodes // Point.ByCoordinates(10,10,10) //============================================== ProtoCore.AST.AssociativeAST.FunctionCallNode constructorCall = new ProtoCore.AST.AssociativeAST.FunctionCallNode(); constructorCall.Function = new ProtoCore.AST.AssociativeAST.IdentifierNode("ByCoordinates"); List<ProtoCore.AST.AssociativeAST.AssociativeNode> listArgs = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>(); listArgs.Add(new ProtoCore.AST.AssociativeAST.DoubleNode(10.0)); listArgs.Add(new ProtoCore.AST.AssociativeAST.DoubleNode(10.0)); listArgs.Add(new ProtoCore.AST.AssociativeAST.DoubleNode(10.0)); constructorCall.FormalArguments = listArgs; ProtoCore.AST.AssociativeAST.FunctionDotCallNode dotCall = new ProtoCore.AST.AssociativeAST.FunctionDotCallNode("Point", constructorCall); //============================================== // Build the binary expression // p = Point.ByCoordinates(10,10,10) //============================================== ProtoCore.AST.AssociativeAST.BinaryExpressionNode stmt1 = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("p"), dotCall, ProtoCore.DSASM.Operator.assign); astList.Add(stmt1); //============================================== // Translate the point // newPoint = p.Translate(1,2,3); //============================================== ProtoCore.AST.AssociativeAST.FunctionCallNode functionCallTranslate = new ProtoCore.AST.AssociativeAST.FunctionCallNode(); functionCallTranslate.Function = new ProtoCore.AST.AssociativeAST.IdentifierNode("Translate"); listArgs = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>(); listArgs.Add(new ProtoCore.AST.AssociativeAST.DoubleNode(1.0)); listArgs.Add(new ProtoCore.AST.AssociativeAST.DoubleNode(2.0)); listArgs.Add(new ProtoCore.AST.AssociativeAST.DoubleNode(3.0)); functionCallTranslate.FormalArguments = listArgs; ProtoCore.AST.AssociativeAST.FunctionDotCallNode dotCallTranslate = new ProtoCore.AST.AssociativeAST.FunctionDotCallNode("p", functionCallTranslate); //============================================== // Build the binary expression //============================================== ProtoCore.AST.AssociativeAST.BinaryExpressionNode stmt2 = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("newPoint"), dotCallTranslate, ProtoCore.DSASM.Operator.assign); astList.Add(stmt2); //============================================== // Build a binary expression to retirieve the x property // xval = newPoint.X //============================================== ProtoCore.AST.AssociativeAST.IdentifierListNode identListNode = new ProtoCore.AST.AssociativeAST.IdentifierListNode(); identListNode.LeftNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("newPoint"); identListNode.Optr = ProtoCore.DSASM.Operator.dot; identListNode.RightNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("X"); ProtoCore.AST.AssociativeAST.BinaryExpressionNode stmt3 = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("xval"), identListNode, ProtoCore.DSASM.Operator.assign); astList.Add(stmt3); //============================================== // emit the DS code from the AST tree // // import ("ProtoGeometry.dll"); // p = Point.Bycoordinates(10.0, 10.0, 10.0); // newPoint = p.Translate(1.0,2.0,3.0); // xval = newPoint.X; // //============================================== GraphToDSCompiler.GraphCompiler gc = GraphToDSCompiler.GraphCompiler.CreateInstance(); string code = gc.Emit(astList); //============================================== // Verify the results - get the value of the x property //============================================== ExecutionMirror mirror = thisTest.RunScriptSource(code); Obj o = mirror.GetValue("xval"); Assert.IsTrue((Double)o.Payload == 11.0); }