public override void Execute() { NodesToCodeCompletedEventArgs args = null; List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>(); if (subtrees != null) { foreach (var tree in subtrees) { Validity.Assert(tree.AstNodes != null && tree.AstNodes.Count > 0); astList.AddRange(tree.AstNodes); } } lock (runner.operationsMutex) { try { string code = new ProtoCore.CodeGenDS(astList).GenerateCode(); args = new NodesToCodeCompletedEventArgs(code, EventStatus.OK, "Node to code task complete."); } catch (Exception exception) { args = new NodesToCodeCompletedEventArgs(string.Empty, EventStatus.Error, exception.Message); } } // Notify the listener if (null != runner.NodesToCodeCompleted) { runner.NodesToCodeCompleted(this, args); } }
public string BuildGraphDAG() { bool removeFromRemovedNodes = true; this.RemoveNodes(removeFromRemovedNodes); this.AddNodesToAST(); this.MakeConnectionsForAddedNodes(); this.UpdateModifiedNodes(); string code = null; #if __TRANSFORM_TO_PROTOAST GraphTransform transform = new GraphTransform(); List<ProtoCore.AST.AssociativeAST.AssociativeNode> graphIR = transform.ToGraphIR(this.Graph, this.gc); ProtoCore.CodeGenDS codeGen = new ProtoCore.CodeGenDS(graphIR); code = codeGen.GenerateCode(); gc.UpdateAddedNodesInModifiedNameList(); #else code = this.gc.PrintGraph(); #endif gc.UpdateDirtyFlags(nodesToModify); return code; }
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); }
public static string ConvertNodesToCode(DynamoModel dynamoModel, IEnumerable<NodeModel> nodeList) { var astBuilder = new AstBuilder(dynamoModel, null); var astNodes = astBuilder.CompileToAstNodes(nodeList, false); var codeGen = new ProtoCore.CodeGenDS(astNodes); return codeGen.GenerateCode(); }
/// <summary> /// Emits the DS code given the list of ast nodes /// </summary> /// <param name="astList"></param> /// <returns></returns> public string Emit(List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList) { // Emit the DS code from the AST tree using ProtoCore code generator ProtoCore.CodeGenDS codegenDS = new ProtoCore.CodeGenDS(astList); string code = codegenDS.GenerateCode(); UpdateAddedNodesInModifiedNameList(); return code; }
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); thisTest.Verify("a", 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("a", result1); }
// 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 == 20); } [Test] public void TestcodegenDS_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); List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>();
[Test] [Ignore][Category("DSDefinedClass_Ignored_DSDefinedClassSemantics")] public void TestCodeGenDS_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);
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 thisTest.Verify("a", 1800); thisTest.Verify("c", 1800); }
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); }
/// <summary> /// API used by external host to build AST for any function call /// </summary> /// <param name="type"></param> /// <param name="hostInstancePtr"></param> /// <param name="functionName"></param> /// <param name="userDefinedArgs"></param> /// <param name="primitiveArgs"></param> /// <param name="formatString"></param> /// <param name="core"></param> /// <param name="symbolName"></param> /// <param name="code"></param> /// <returns></returns> public static AssociativeNode BuildAST(string type, long hostInstancePtr, string functionName, List<IntPtr> userDefinedArgs, List<string> primitiveArgs, string formatString, ProtoCore.Core core, ref string symbolName, ref string code) { symbolName = string.Empty; List<AssociativeNode> astNodes = new List<AssociativeNode>(); FunctionDotCallNode dotCall = null; BinaryExpressionNode bNode = null; ProtoCore.AST.AssociativeAST.FunctionDotCallNode dotCallNode = null; List<AssociativeNode> argNodes = new List<AssociativeNode>(); if (userDefinedArgs != null) { foreach (var arg in userDefinedArgs) { dotCallNode = CreateEntityNode((long)arg, core); bNode = CreateAssignmentNode(dotCallNode); argNodes.Add(bNode); } astNodes.AddRange(argNodes); } List<ProtoCore.AST.AssociativeAST.AssociativeNode> args = CreateArgs(formatString, primitiveArgs, argNodes); if (hostInstancePtr != 0) { dotCallNode = CreateEntityNode(hostInstancePtr, core); bNode = CreateAssignmentNode(dotCallNode); astNodes.Add(bNode); dotCall = CreateFunctionCallNode((bNode.LeftNode as IdentifierNode).Value, functionName, args, core); } else { dotCall = CreateFunctionCallNode(type, functionName, args, core); } bNode = CreateAssignmentNode(dotCall); if (bNode.LeftNode is IdentifierNode) { symbolName = (bNode.LeftNode as IdentifierNode).Value; } astNodes.Add(bNode); CodeBlockNode codeBlockNode = new CodeBlockNode(); codeBlockNode.Body = astNodes; ProtoCore.CodeGenDS codeGen = new ProtoCore.CodeGenDS(astNodes); code = codeGen.GenerateCode(); return codeBlockNode; }
/// <summary> /// This converts a list of ProtoASTs into ds using CodeGenDS /// </summary> /// <param name="astList"></param> /// <returns></returns> public static string ASTListToCode(List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList) { string code = string.Empty; if (null != astList && astList.Count > 0) { ProtoCore.CodeGenDS codeGen = new ProtoCore.CodeGenDS(astList); code = codeGen.GenerateCode(); } return code; }
private static List<string> ParseCore(string expression, ref bool parseSuccess) { List<string> compiled = new List<string>(); ProtoCore.AST.AssociativeAST.CodeBlockNode commentNode = null; ProtoCore.AST.Node codeBlockNode = Parse(expression, out commentNode); parseSuccess = true; List<ProtoCore.AST.Node> nodes = ParserUtils.GetAstNodes(codeBlockNode); Validity.Assert(nodes != null); int cNodeNum = 0; if (nodes.Count == 0) { InsertCommentsInCode(null, null, commentNode, ref cNodeNum, ref compiled, expression); return compiled; } foreach (var node in nodes) { ProtoCore.AST.AssociativeAST.AssociativeNode n = node as ProtoCore.AST.AssociativeAST.AssociativeNode; ProtoCore.Utils.Validity.Assert(n != null); if (n is ProtoCore.AST.AssociativeAST.ModifierStackNode) { core.BuildStatus.LogSemanticError("Modifier Blocks are not supported currently."); } else if (n is ProtoCore.AST.AssociativeAST.ImportNode) { core.BuildStatus.LogSemanticError("Import statements are not supported in CodeBlock Nodes."); } else if (n is ProtoCore.AST.AssociativeAST.LanguageBlockNode) { core.BuildStatus.LogSemanticError("Language blocks are not supported in CodeBlock Nodes."); } string stmt = string.Empty; // Append the temporaries only if it is not a function def or class decl bool isFunctionOrClassDef = n is ProtoCore.AST.AssociativeAST.FunctionDefinitionNode || n is ProtoCore.AST.AssociativeAST.ClassDeclNode; if (isFunctionOrClassDef) { ProtoCore.CodeGenDS codegen = new ProtoCore.CodeGenDS(new List<ProtoCore.AST.AssociativeAST.AssociativeNode>{ n }); stmt = codegen.GenerateCode(); } else { stmt = ProtoCore.Utils.ParserUtils.ExtractStatementFromCode(expression, node); ProtoCore.AST.AssociativeAST.BinaryExpressionNode ben = node as ProtoCore.AST.AssociativeAST.BinaryExpressionNode; if (ben != null && ben.Optr == ProtoCore.DSASM.Operator.assign) { ProtoCore.AST.AssociativeAST.IdentifierNode lNode = ben.LeftNode as ProtoCore.AST.AssociativeAST.IdentifierNode; if (lNode != null && lNode.Value == ProtoCore.DSASM.Constants.kTempProcLeftVar) { stmt = "%t =" + stmt; } } else { // These nodes are non-assignment nodes stmt = "%t =" + stmt; } } compiled.Add(stmt); InsertCommentsInCode(stmt, node, commentNode, ref cNodeNum, ref compiled, expression); } InsertCommentsInCode(null, null, commentNode, ref cNodeNum, ref compiled, expression); return compiled; }
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_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); }
private string GetExpressionFromNode(AssociativeNode astNode) { List<AssociativeNode> astNodes = new List<AssociativeNode>(); astNodes.Add(astNode); ProtoCore.CodeGenDS codegen = new ProtoCore.CodeGenDS(astNodes); return codegen.GenerateCode(); }
internal static void ConvertNodesToCodeInternal(this WorkspaceModel workspace, EngineController engineController, INamingProvider namingProvider) { var selectedNodes = DynamoSelection.Instance .Selection .OfType <NodeModel>() .Where(n => n.IsConvertible); if (!selectedNodes.Any()) { return; } var cliques = NodeToCodeCompiler.GetCliques(selectedNodes).Where(c => !(c.Count == 1 && c.First() is CodeBlockNodeModel)); var codeBlockNodes = new List <CodeBlockNodeModel>(); //UndoRedo Action Group---------------------------------------------- NodeToCodeUndoHelper undoHelper = new NodeToCodeUndoHelper(); foreach (var nodeList in cliques) { //Create two dictionarys to store the details of the external connections that have to //be recreated after the conversion var externalInputConnections = new Dictionary <ConnectorModel, string>(); var externalOutputConnections = new Dictionary <ConnectorModel, string>(); //Also collect the average X and Y co-ordinates of the different nodes int nodeCount = nodeList.Count; var nodeToCodeResult = engineController.ConvertNodesToCode(workspace.Nodes, nodeList, namingProvider); #region Step I. Delete all nodes and their connections double totalX = 0, totalY = 0; foreach (var node in nodeList) { #region Step I.A. Delete the connections for the node foreach (var connector in node.AllConnectors.ToList()) { if (!IsInternalNodeToCodeConnection(nodeList, connector)) { //If the connector is an external connector, the save its details //for recreation later var startNode = connector.Start.Owner; int index = startNode.OutPorts.IndexOf(connector.Start); //We use the varibleName as the connection between the port of the old Node //to the port of the new node. var variableName = startNode.GetAstIdentifierForOutputIndex(index).Value; //Store the data in the corresponding dictionary if (startNode == node) { if (nodeToCodeResult.OutputMap.ContainsKey(variableName)) { variableName = nodeToCodeResult.OutputMap[variableName]; } externalOutputConnections.Add(connector, variableName); } else { if (nodeToCodeResult.InputMap.ContainsKey(variableName)) { variableName = nodeToCodeResult.InputMap[variableName]; } externalInputConnections.Add(connector, variableName); } } //Delete the connector undoHelper.RecordDeletion(connector); connector.Delete(); } #endregion #region Step I.B. Delete the node totalX += node.X; totalY += node.Y; undoHelper.RecordDeletion(node); workspace.RemoveAndDisposeNode(node); #endregion } #endregion #region Step II. Create the new code block node var outputVariables = externalOutputConnections.Values; var newResult = NodeToCodeCompiler.ConstantPropagationForTemp(nodeToCodeResult, outputVariables); // Rewrite the AST using the shortest unique name in case of namespace conflicts NodeToCodeCompiler.ReplaceWithShortestQualifiedName( engineController.LibraryServices.LibraryManagementCore.ClassTable, newResult.AstNodes, workspace.ElementResolver); var codegen = new ProtoCore.CodeGenDS(newResult.AstNodes); var code = codegen.GenerateCode(); var codeBlockNode = new CodeBlockNodeModel( code, System.Guid.NewGuid(), totalX / nodeCount, totalY / nodeCount, engineController.LibraryServices, workspace.ElementResolver); undoHelper.RecordCreation(codeBlockNode); workspace.AddAndRegisterNode(codeBlockNode, false); codeBlockNodes.Add(codeBlockNode); #endregion #region Step III. Recreate the necessary connections var newInputConnectors = ReConnectInputConnections(externalInputConnections, codeBlockNode, workspace); foreach (var connector in newInputConnectors) { undoHelper.RecordCreation(connector); } var newOutputConnectors = ReConnectOutputConnections(externalOutputConnections, codeBlockNode); foreach (var connector in newOutputConnectors) { undoHelper.RecordCreation(connector); } #endregion } undoHelper.ApplyActions(workspace.UndoRecorder); DynamoSelection.Instance.ClearSelection(); DynamoSelection.Instance.Selection.AddRange(codeBlockNodes); Debug.WriteLine(string.Format("Workspace has {0} nodes and {1} connectors after N2C operation.", workspace.Nodes.Count(), workspace.Connectors.Count())); workspace.RequestRun(); }
} } 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};
/// <summary> /// API for external hosts to build an identifier array assignment node, e.g.: var = {var1, var2, ...} /// </summary> /// <param name="arrayInputs"></param> /// <param name="symbolName"></param> /// <param name="code"></param> /// <returns></returns> public static AssociativeNode BuildArrayNode(List<string> arrayInputs, ref string symbolName, ref string code) { ExprListNode arrayNode = AstFactory.BuildExprList(arrayInputs); BinaryExpressionNode bNode = CreateAssignmentNode(arrayNode); if (bNode.LeftNode is IdentifierNode) { symbolName = (bNode.LeftNode as IdentifierNode).Value; } List<AssociativeNode> astNodes = new List<AssociativeNode>(); astNodes.Add(bNode); ProtoCore.CodeGenDS codeGen = new ProtoCore.CodeGenDS(astNodes); code = codeGen.GenerateCode(); return bNode; }
public void TestCodeGenDS_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); 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); }
internal void ConvertNodesToCodeInternal(EngineController engineController, INamingProvider namingProvider) { var selectedNodes = DynamoSelection.Instance .Selection .OfType<NodeModel>() .Where(n => n.IsConvertible); if (!selectedNodes.Any()) return; var cliques = NodeToCodeCompiler.GetCliques(selectedNodes).Where(c => !(c.Count == 1 && c.First() is CodeBlockNodeModel)); var codeBlockNodes = new List<CodeBlockNodeModel>(); //UndoRedo Action Group---------------------------------------------- NodeToCodeUndoHelper undoHelper = new NodeToCodeUndoHelper(); // using (UndoRecorder.BeginActionGroup()) { foreach (var nodeList in cliques) { //Create two dictionarys to store the details of the external connections that have to //be recreated after the conversion var externalInputConnections = new Dictionary<ConnectorModel, string>(); var externalOutputConnections = new Dictionary<ConnectorModel, string>(); //Also collect the average X and Y co-ordinates of the different nodes int nodeCount = nodeList.Count; var nodeToCodeResult = engineController.ConvertNodesToCode(this.nodes, nodeList, namingProvider); #region Step I. Delete all nodes and their connections double totalX = 0, totalY = 0; foreach (var node in nodeList) { #region Step I.A. Delete the connections for the node foreach (var connector in node.AllConnectors.ToList()) { if (!IsInternalNodeToCodeConnection(nodeList, connector)) { //If the connector is an external connector, the save its details //for recreation later var startNode = connector.Start.Owner; int index = startNode.OutPorts.IndexOf(connector.Start); //We use the varibleName as the connection between the port of the old Node //to the port of the new node. var variableName = startNode.GetAstIdentifierForOutputIndex(index).Value; //Store the data in the corresponding dictionary if (startNode == node) { if (nodeToCodeResult.OutputMap.ContainsKey(variableName)) variableName = nodeToCodeResult.OutputMap[variableName]; externalOutputConnections.Add(connector, variableName); } else { if (nodeToCodeResult.InputMap.ContainsKey(variableName)) variableName = nodeToCodeResult.InputMap[variableName]; externalInputConnections.Add(connector, variableName); } } //Delete the connector undoHelper.RecordDeletion(connector); connector.Delete(); } #endregion #region Step I.B. Delete the node totalX += node.X; totalY += node.Y; undoHelper.RecordDeletion(node); RemoveNode(node); #endregion } #endregion #region Step II. Create the new code block node var outputVariables = externalOutputConnections.Values; var newResult = NodeToCodeCompiler.ConstantPropagationForTemp(nodeToCodeResult, outputVariables); // Rewrite the AST using the shortest unique name in case of namespace conflicts NodeToCodeCompiler.ReplaceWithShortestQualifiedName( engineController.LibraryServices.LibraryManagementCore.ClassTable, newResult.AstNodes, ElementResolver); var codegen = new ProtoCore.CodeGenDS(newResult.AstNodes); var code = codegen.GenerateCode(); var codeBlockNode = new CodeBlockNodeModel( code, System.Guid.NewGuid(), totalX / nodeCount, totalY / nodeCount, engineController.LibraryServices, ElementResolver); undoHelper.RecordCreation(codeBlockNode); AddAndRegisterNode(codeBlockNode, false); codeBlockNodes.Add(codeBlockNode); #endregion #region Step III. Recreate the necessary connections var newInputConnectors = ReConnectInputConnections(externalInputConnections, codeBlockNode); foreach (var connector in newInputConnectors) { undoHelper.RecordCreation(connector); } var newOutputConnectors = ReConnectOutputConnections(externalOutputConnections, codeBlockNode); foreach (var connector in newOutputConnectors) { undoHelper.RecordCreation(connector); } #endregion } } undoHelper.ApplyActions(UndoRecorder); DynamoSelection.Instance.ClearSelection(); DynamoSelection.Instance.Selection.AddRange(codeBlockNodes); RequestRun(); }
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 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 TestCodeGenDS_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); 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 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_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); // 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); thisTest.Verify("a", 10); }
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 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 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>();
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); }
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_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); }
// 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 == 11); } [Test] public void TestCodegenDS_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); List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>();
private void ConvertCurrentWorkspaceToDesignScriptAndSave(string filePathBase) { try { var workspace = CurrentDynamoModel.CurrentWorkspace; var libCore = CurrentDynamoModel.EngineController.LibraryServices.LibraryManagementCore; var libraryServices = new LibraryCustomizationServices(CurrentDynamoModel.PathManager); var nameProvider = new NamingProvider(libCore, libraryServices); var controller = CurrentDynamoModel.EngineController; var resolver = CurrentDynamoModel.CurrentWorkspace.ElementResolver; var namingProvider = new NamingProvider(controller.LibraryServices.LibraryManagementCore, libraryServices); var result = NodeToCodeCompiler.NodeToCode(libCore, workspace.Nodes, workspace.Nodes, namingProvider); NodeToCodeCompiler.ReplaceWithShortestQualifiedName( controller.LibraryServices.LibraryManagementCore.ClassTable, result.AstNodes, resolver); var codegen = new ProtoCore.CodeGenDS(result.AstNodes); var ds = codegen.GenerateCode(); var dsPath = filePathBase + ".ds"; if (File.Exists(dsPath)) { File.Delete(dsPath); } File.WriteAllText(dsPath, ds); } catch { Assert.Inconclusive("The current workspace could not be converted to Design Script."); } }