private List<AssociativeNode> GetAstsForBranch(int branch, List<AssociativeNode> inputAstNodes, bool verboseLogging, AstBuilder builder) { // Get all upstream nodes and then remove nodes that are not var nodes = GetInScopeNodesForInport(branch, false).Where(n => !(n is Symbol)); nodes = ScopedNodeModel.GetNodesInTopScope(nodes); // The second parameter, isDeltaExecution, is set to false so that // all AST nodes will be added to this IF graph node instead of // adding to the corresponding graph node. var allAstNodes = builder.CompileToAstNodes(nodes, CompilationContext.None, verboseLogging); var astNodes = allAstNodes.SelectMany(t => t.Item2).ToList(); astNodes.Add(AstFactory.BuildReturnStatement(inputAstNodes[branch])); return astNodes; }
/// <summary> /// Similar to NodeModel.BuildAst(). When compiled to AST, for /// ScopedNodeModel this method will be called when all requirements /// are satisfied. /// </summary> /// <param name="inputAstNodes"></param> /// <param name="verboseLogging"></param> /// <param name="builder"></param> /// <returns></returns> internal virtual IEnumerable<AssociativeNode> BuildAstInScope(List<AssociativeNode> inputAstNodes, bool verboseLogging, AstBuilder builder) { OnBuilt(); var result = BuildOutputAstInScope(inputAstNodes, verboseLogging, builder); return result; }
/// <summary> /// Similar to NodeModel.BuildOutputAst(). When compiled to AST, for /// ScopedNodeModel this method will be called when all requirements are /// satisfied. The derived class needs to implement this method to /// compile its children into some scopes. /// </summary> /// <param name="inputAstNodes"></param> /// <param name="verboseLogging"></param> /// <param name="builder"></param> /// <returns></returns> public virtual IEnumerable<AssociativeNode> BuildOutputAstInScope(List<AssociativeNode> inputAstNodes, bool verboseLogging, AstBuilder builder) { throw new NotImplementedException("BuildOutputAstInScope"); }
public override IEnumerable<AssociativeNode> BuildOutputAstInScope(List<AssociativeNode> inputAstNodes, bool verboseLogging, AstBuilder builder) { // This function will compile IF node to the following format: // // cond = ...; // v = [Imperative] // { // if (cond) { // return = [Associative] { // ... // } // } // else { // return = [Associative] { // ... // } // } // } // var astsInTrueBranch = GetAstsForBranch(1, inputAstNodes, verboseLogging, builder); var astsInFalseBranch = GetAstsForBranch(2, inputAstNodes, verboseLogging, builder); // if (cond) { // return = [Associative] {...} // } var ifBlock = new LanguageBlockNode { codeblock = new LanguageCodeBlock(Language.kAssociative), CodeBlockNode = new CodeBlockNode { Body = astsInTrueBranch } }; var ifBranch = AstFactory.BuildReturnStatement(ifBlock).ToImperativeAST(); // else { // return = [Associative] { ... } // } var elseBlock = new LanguageBlockNode { codeblock = new LanguageCodeBlock(Language.kAssociative), CodeBlockNode = new CodeBlockNode { Body = astsInFalseBranch } }; var elseBranch = AstFactory.BuildReturnStatement(elseBlock).ToImperativeAST(); var ifelseStatement = new ProtoCore.AST.ImperativeAST.IfStmtNode() { IfExprNode = inputAstNodes[0].ToImperativeAST(), IfBody = new List<ProtoCore.AST.ImperativeAST.ImperativeNode> { ifBranch }, ElseBody = new List<ProtoCore.AST.ImperativeAST.ImperativeNode> { elseBranch } }; // thisVariable = [Imperative] // { // ... // } var outerBlock = new LanguageBlockNode { codeblock = new LanguageCodeBlock(Language.kImperative), CodeBlockNode = new ProtoCore.AST.ImperativeAST.CodeBlockNode { Body = new List<ProtoCore.AST.ImperativeAST.ImperativeNode> { ifelseStatement } } }; var thisVariable = GetAstIdentifierForOutputIndex(0); var assignment = AstFactory.BuildAssignment(thisVariable, outerBlock); return new AssociativeNode[] { assignment }; }