public LanguageBlockNode(LanguageBlockNode rhs) : base(rhs) { CodeBlockNode = NodeUtils.Clone(rhs.CodeBlockNode); codeblock = new ProtoCore.LanguageCodeBlock(rhs.codeblock); Attributes = new List<AssociativeNode>(); foreach (AssociativeNode aNode in rhs.Attributes) { AssociativeNode newNode = ProtoCore.Utils.NodeUtils.Clone(aNode); Attributes.Add(newNode); } }
private void EmitInlineConditionalNode(AssociativeNode node, ref ProtoCore.Type inferedType, ProtoCore.AssociativeGraph.GraphNode graphNode = null, ProtoCore.CompilerDefinitions.Associative.SubCompilePass subPass = ProtoCore.CompilerDefinitions.Associative.SubCompilePass.kNone, ProtoCore.AST.AssociativeAST.BinaryExpressionNode parentNode = null) { if (subPass == ProtoCore.CompilerDefinitions.Associative.SubCompilePass.kUnboundIdentifier) { return; } bool isInlineConditionalFlag = false; int startPC = pc; bool isReturn = false; if (graphNode != null) { isInlineConditionalFlag = graphNode.isInlineConditional; graphNode.isInlineConditional = true; startPC = graphNode.updateBlock.startpc; isReturn = graphNode.isReturn; } InlineConditionalNode inlineConditionalNode = node as InlineConditionalNode; // TODO: Jun, this 'if' condition needs to be removed as it was the old implementation - pratapa if (inlineConditionalNode.IsAutoGenerated) { // Normal inline conditional IfStatementNode ifNode = new IfStatementNode(); ifNode.ifExprNode = inlineConditionalNode.ConditionExpression; List<AssociativeNode> ifBody = new List<AssociativeNode>(); List<AssociativeNode> elseBody = new List<AssociativeNode>(); ifBody.Add(inlineConditionalNode.TrueExpression); elseBody.Add(inlineConditionalNode.FalseExpression); ifNode.IfBody = ifBody; ifNode.ElseBody = elseBody; EmitIfStatementNode(ifNode, ref inferedType, graphNode); } else { // CPS inline conditional FunctionCallNode inlineCall = new FunctionCallNode(); IdentifierNode identNode = new IdentifierNode(); identNode.Name = ProtoCore.DSASM.Constants.kInlineConditionalMethodName; inlineCall.Function = identNode; DebugProperties.BreakpointOptions oldOptions = core.DebuggerProperties.breakOptions; DebugProperties.BreakpointOptions newOptions = oldOptions; newOptions |= DebugProperties.BreakpointOptions.EmitInlineConditionalBreakpoint; core.DebuggerProperties.breakOptions = newOptions; core.DebuggerProperties.highlightRange = new ProtoCore.CodeModel.CodeRange { StartInclusive = new ProtoCore.CodeModel.CodePoint { LineNo = parentNode.line, CharNo = parentNode.col }, EndExclusive = new ProtoCore.CodeModel.CodePoint { LineNo = parentNode.endLine, CharNo = parentNode.endCol } }; // As SSA conversion is enabled, we have got the values of // true and false branch, so it isn't necessary to create // language blocks. if (core.Options.GenerateSSA) { inlineCall.FormalArguments.Add(inlineConditionalNode.ConditionExpression); inlineCall.FormalArguments.Add(inlineConditionalNode.TrueExpression); inlineCall.FormalArguments.Add(inlineConditionalNode.FalseExpression); } else { // True condition language block BinaryExpressionNode bExprTrue = AstFactory.BuildReturnStatement(inlineConditionalNode.TrueExpression); LanguageBlockNode langblockT = new LanguageBlockNode(); int trueBlockId = Constants.kInvalidIndex; langblockT.codeblock.Language = ProtoCore.Language.Associative; core.AssocNode = bExprTrue; core.InlineConditionalBodyGraphNodes.Push(new List<GraphNode>()); EmitDynamicLanguageBlockNode(langblockT, bExprTrue, ref inferedType, ref trueBlockId, graphNode, ProtoCore.CompilerDefinitions.Associative.SubCompilePass.kNone); List<GraphNode> trueBodyNodes = core.InlineConditionalBodyGraphNodes.Pop(); // Append dependent nodes of the inline conditional foreach (GraphNode gnode in trueBodyNodes) foreach (GraphNode dNode in gnode.dependentList) graphNode.PushDependent(dNode); core.AssocNode = null; DynamicBlockNode dynBlockT = new DynamicBlockNode(trueBlockId); // False condition language block BinaryExpressionNode bExprFalse = AstFactory.BuildReturnStatement(inlineConditionalNode.FalseExpression); LanguageBlockNode langblockF = new LanguageBlockNode(); int falseBlockId = Constants.kInvalidIndex; langblockF.codeblock.Language = ProtoCore.Language.Associative; core.AssocNode = bExprFalse; core.InlineConditionalBodyGraphNodes.Push(new List<GraphNode>()); EmitDynamicLanguageBlockNode(langblockF, bExprFalse, ref inferedType, ref falseBlockId, graphNode, ProtoCore.CompilerDefinitions.Associative.SubCompilePass.kNone); List<GraphNode> falseBodyNodes = core.InlineConditionalBodyGraphNodes.Pop(); // Append dependent nodes of the inline conditional foreach (GraphNode gnode in falseBodyNodes) foreach (GraphNode dNode in gnode.dependentList) graphNode.PushDependent(dNode); core.AssocNode = null; DynamicBlockNode dynBlockF = new DynamicBlockNode(falseBlockId); inlineCall.FormalArguments.Add(inlineConditionalNode.ConditionExpression); inlineCall.FormalArguments.Add(dynBlockT); inlineCall.FormalArguments.Add(dynBlockF); } core.DebuggerProperties.breakOptions = oldOptions; core.DebuggerProperties.highlightRange = new ProtoCore.CodeModel.CodeRange { StartInclusive = new ProtoCore.CodeModel.CodePoint { LineNo = Constants.kInvalidIndex, CharNo = Constants.kInvalidIndex }, EndExclusive = new ProtoCore.CodeModel.CodePoint { LineNo = Constants.kInvalidIndex, CharNo = Constants.kInvalidIndex } }; // Save the pc and store it after the call EmitFunctionCallNode(inlineCall, ref inferedType, false, graphNode, ProtoCore.CompilerDefinitions.Associative.SubCompilePass.kUnboundIdentifier); EmitFunctionCallNode(inlineCall, ref inferedType, false, graphNode, ProtoCore.CompilerDefinitions.Associative.SubCompilePass.kNone, parentNode); // Need to restore those settings. if (graphNode != null) { graphNode.isInlineConditional = isInlineConditionalFlag; graphNode.updateBlock.startpc = startPC; graphNode.isReturn = isReturn; } } }
private void EmitInlineConditionalNode(AssociativeNode node, ref ProtoCore.Type inferedType, ProtoCore.AssociativeGraph.GraphNode graphNode = null, ProtoCore.DSASM.AssociativeSubCompilePass subPass = ProtoCore.DSASM.AssociativeSubCompilePass.kNone, ProtoCore.AST.AssociativeAST.BinaryExpressionNode parentNode = null) { if (subPass == AssociativeSubCompilePass.kUnboundIdentifier) { return; } bool isInlineConditionalFlag = false; int startPC = pc; bool isReturn = false; if (graphNode != null) { isInlineConditionalFlag = graphNode.isInlineConditional; graphNode.isInlineConditional = true; startPC = graphNode.updateBlock.startpc; isReturn = graphNode.isReturn; } InlineConditionalNode inlineConditionalNode = node as InlineConditionalNode; // TODO: Jun, this 'if' condition needs to be removed as it was the old implementation - pratapa if (inlineConditionalNode.IsAutoGenerated) { // Normal inline conditional IfStatementNode ifNode = new IfStatementNode(); ifNode.ifExprNode = inlineConditionalNode.ConditionExpression; List<AssociativeNode> ifBody = new List<AssociativeNode>(); List<AssociativeNode> elseBody = new List<AssociativeNode>(); ifBody.Add(inlineConditionalNode.TrueExpression); elseBody.Add(inlineConditionalNode.FalseExpression); ifNode.IfBody = ifBody; ifNode.ElseBody = elseBody; EmitIfStatementNode(ifNode, ref inferedType, graphNode); } else { // CPS inline conditional FunctionCallNode inlineCall = new FunctionCallNode(); IdentifierNode identNode = new IdentifierNode(); identNode.Name = ProtoCore.DSASM.Constants.kInlineConditionalMethodName; inlineCall.Function = identNode; DebugProperties.BreakpointOptions oldOptions = compileStateTracker.DebugProps.breakOptions; DebugProperties.BreakpointOptions newOptions = oldOptions; newOptions |= DebugProperties.BreakpointOptions.EmitInlineConditionalBreakpoint; compileStateTracker.DebugProps.breakOptions = newOptions; compileStateTracker.DebugProps.highlightRange = new ProtoCore.CodeModel.CodeRange { StartInclusive = new ProtoCore.CodeModel.CodePoint { LineNo = parentNode.line, CharNo = parentNode.col }, EndExclusive = new ProtoCore.CodeModel.CodePoint { LineNo = parentNode.endLine, CharNo = parentNode.endCol } }; // True condition language block BinaryExpressionNode bExprTrue = new BinaryExpressionNode(); bExprTrue.LeftNode = nodeBuilder.BuildReturn(); bExprTrue.Optr = Operator.assign; bExprTrue.RightNode = inlineConditionalNode.TrueExpression; LanguageBlockNode langblockT = new LanguageBlockNode(); int trueBlockId = ProtoCore.DSASM.Constants.kInvalidIndex; langblockT.codeblock.language = ProtoCore.Language.kAssociative; langblockT.codeblock.fingerprint = ""; langblockT.codeblock.version = ""; compileStateTracker.AssocNode = bExprTrue; EmitDynamicLanguageBlockNode(langblockT, bExprTrue, ref inferedType, ref trueBlockId, graphNode, AssociativeSubCompilePass.kNone); compileStateTracker.AssocNode = null; ProtoCore.AST.AssociativeAST.DynamicBlockNode dynBlockT = new ProtoCore.AST.AssociativeAST.DynamicBlockNode(trueBlockId); // False condition language block BinaryExpressionNode bExprFalse = new BinaryExpressionNode(); bExprFalse.LeftNode = nodeBuilder.BuildReturn(); bExprFalse.Optr = Operator.assign; bExprFalse.RightNode = inlineConditionalNode.FalseExpression; LanguageBlockNode langblockF = new LanguageBlockNode(); int falseBlockId = ProtoCore.DSASM.Constants.kInvalidIndex; langblockF.codeblock.language = ProtoCore.Language.kAssociative; langblockF.codeblock.fingerprint = ""; langblockF.codeblock.version = ""; compileStateTracker.AssocNode = bExprFalse; EmitDynamicLanguageBlockNode(langblockF, bExprFalse, ref inferedType, ref falseBlockId, graphNode, AssociativeSubCompilePass.kNone); compileStateTracker.AssocNode = null; ProtoCore.AST.AssociativeAST.DynamicBlockNode dynBlockF = new ProtoCore.AST.AssociativeAST.DynamicBlockNode(falseBlockId); compileStateTracker.DebugProps.breakOptions = oldOptions; compileStateTracker.DebugProps.highlightRange = new ProtoCore.CodeModel.CodeRange { StartInclusive = new ProtoCore.CodeModel.CodePoint { LineNo = Constants.kInvalidIndex, CharNo = Constants.kInvalidIndex }, EndExclusive = new ProtoCore.CodeModel.CodePoint { LineNo = Constants.kInvalidIndex, CharNo = Constants.kInvalidIndex } }; inlineCall.FormalArguments.Add(inlineConditionalNode.ConditionExpression); inlineCall.FormalArguments.Add(dynBlockT); inlineCall.FormalArguments.Add(dynBlockF); // Save the pc and store it after the call EmitFunctionCallNode(inlineCall, ref inferedType, false, graphNode, AssociativeSubCompilePass.kUnboundIdentifier); EmitFunctionCallNode(inlineCall, ref inferedType, false, graphNode, AssociativeSubCompilePass.kNone, parentNode); // Need to restore those settings. if (graphNode != null) { graphNode.isInlineConditional = isInlineConditionalFlag; graphNode.updateBlock.startpc = startPC; graphNode.isReturn = isReturn; } } }
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 }; }
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 }); }
void Associative_LanguageBlock(out ProtoCore.AST.AssociativeAST.AssociativeNode node) { node = null; ProtoCore.AST.AssociativeAST.LanguageBlockNode langblock = new ProtoCore.AST.AssociativeAST.LanguageBlockNode(); Expect(10); NodeUtils.SetNodeLocation(langblock, t); Expect(1); if( 0 == t.val.CompareTo(ProtoCore.DSASM.kw.imperative)) { langblock.codeblock.Language = ProtoCore.Language.Imperative; } else if( 0 == t.val.CompareTo(ProtoCore.DSASM.kw.associative)) { langblock.codeblock.Language = ProtoCore.Language.Associative; } else { langblock.codeblock.Language = ProtoCore.Language.NotSpecified; errors.SemErr(t.line, t.col, String.Format(Resources.InvalidLanguageBlockIdentifier, t.val)); } Expect(11); Expect(45); Node codeBlockNode = null; if (langblock.codeblock.Language == ProtoCore.Language.Associative || langblock.codeblock.Language == ProtoCore.Language.NotSpecified) { Hydrogen(out codeBlockNode); } else if (langblock.codeblock.Language == ProtoCore.Language.Imperative ) { Imperative(out codeBlockNode); } else SynErr(82); if (langblock.codeblock.Language == ProtoCore.Language.NotSpecified) { int openCurlyBraceCount = 0, closeCurlyBraceCount = 0; ProtoCore.AST.AssociativeAST.CodeBlockNode codeBlockInvalid = new ProtoCore.AST.AssociativeAST.CodeBlockNode(); ProtoCore.AST.AssociativeAST.AssociativeNode validBlockInInvalid = null; while (closeCurlyBraceCount <= openCurlyBraceCount) { if (la.kind == 10) { Associative_LanguageBlock(out validBlockInInvalid); codeBlockInvalid.Body.Add(validBlockInInvalid); } else if (la.kind == 45) { Get(); openCurlyBraceCount++; } else if (la.kind == 46) { Get(); closeCurlyBraceCount++; } else if (la.kind == 0) { Get(); Expect(46); break; } else if (StartOf(13)) { Get(); } else SynErr(83); } codeBlockNode = codeBlockInvalid; } else if (la.kind == 46) { Get(); } else SynErr(84); langblock.CodeBlockNode = codeBlockNode; node = langblock; }
void Associative_LanguageBlock(out ProtoCore.AST.AssociativeAST.AssociativeNode node) { node = null; ProtoCore.AST.AssociativeAST.LanguageBlockNode langblock = new ProtoCore.AST.AssociativeAST.LanguageBlockNode(); Expect(8); NodeUtils.SetNodeLocation(langblock, t); Expect(1); if( 0 == t.val.CompareTo(ProtoCore.DSASM.kw.imperative)) { langblock.codeblock.language = ProtoCore.Language.kImperative; } else if( 0 == t.val.CompareTo(ProtoCore.DSASM.kw.associative)) { langblock.codeblock.language = ProtoCore.Language.kAssociative; } else { langblock.codeblock.language = ProtoCore.Language.kInvalid; errors.SemErr(t.line, t.col, String.Format(Resources.InvalidLanguageBlockIdentifier, t.val)); } while (WeakSeparator(48,5,6) ) { if (IsLanguageBlockProperty()) { Expect(1); string key = t.val; Expect(49); Expect(4); if ("fingerprint" == key) { langblock.codeblock.fingerprint = t.val; langblock.codeblock.fingerprint = langblock.codeblock.fingerprint.Remove(0,1); langblock.codeblock.fingerprint = langblock.codeblock.fingerprint.Remove(langblock.codeblock.fingerprint.Length-1,1); } else if ("version" == key) { langblock.codeblock.version = t.val; langblock.codeblock.version = langblock.codeblock.version.Remove(0,1); langblock.codeblock.version = langblock.codeblock.version.Remove(langblock.codeblock.version.Length-1,1); } } else if (la.kind == 1) { ProtoCore.AST.AssociativeAST.AssociativeNode attr = null; Associative_Attribute(out attr); if (attr != null) langblock.Attributes.Add(attr); } else SynErr(83); } Expect(9); Expect(46); Node codeBlockNode = null; if (langblock.codeblock.language == ProtoCore.Language.kAssociative || langblock.codeblock.language == ProtoCore.Language.kInvalid) { Hydrogen(out codeBlockNode); } else if (langblock.codeblock.language == ProtoCore.Language.kImperative ) { Imperative(out codeBlockNode); } else SynErr(84); if (langblock.codeblock.language == ProtoCore.Language.kInvalid ) { int openCurlyBraceCount = 0, closeCurlyBraceCount = 0; ProtoCore.AST.AssociativeAST.CodeBlockNode codeBlockInvalid = new ProtoCore.AST.AssociativeAST.CodeBlockNode(); ProtoCore.AST.AssociativeAST.AssociativeNode validBlockInInvalid = null; while (closeCurlyBraceCount <= openCurlyBraceCount) { if (la.kind == 8) { Associative_LanguageBlock(out validBlockInInvalid); codeBlockInvalid.Body.Add(validBlockInInvalid); } else if (la.kind == 46) { Get(); openCurlyBraceCount++; } else if (la.kind == 47) { Get(); closeCurlyBraceCount++; } else if (la.kind == 0) { Get(); Expect(47); break; } else if (StartOf(13)) { Get(); } else SynErr(85); } codeBlockNode = codeBlockInvalid; } else if (la.kind == 47) { Get(); } else SynErr(86); langblock.CodeBlockNode = codeBlockNode; node = langblock; }
public override AssociativeNode VisitLanguageBlockNode(LanguageBlockNode node) { var impCbn = node.CodeBlockNode as ProtoCore.AST.ImperativeAST.ImperativeNode; if (impCbn != null) { var replacer = new ImperativeIdentifierInPlaceMapper(core, cond, mapper); impCbn.Accept(replacer); } return node; }