public void VisitIfElse(IfElseNode node) { Print("If Else"); VisitSubnode(node.Condition); VisitBody(node.IfBody); VisitBody(node.ElseBody); }
public void RemoveInBlock() { // { if (a) EmptyNode; } // { if (a) EmptyNode; else EmptyNode; } var if1 = new IfElseNode(new IdNode("a"), new EmptyNode()); var if2 = new IfElseNode(new IdNode("a"), new EmptyNode(), new EmptyNode()); var block1 = new BlockNode(new StListNode(if1)); var block2 = new BlockNode(new StListNode(if2)); if1.Parent = block1; if2.Parent = block2; var root = new StListNode(block1); root.Add(block2); block1.Parent = block2.Parent = root; var opt = new IfNullElseNull(); root.Visit(opt); Assert.IsNull(root.Parent); Assert.AreEqual(root.ExprChildren.Count, 0); Assert.AreEqual(root.StatChildren.Count, 2); foreach (var node in root.StatChildren) { Assert.IsTrue(node is BlockNode); Assert.AreEqual(node.ExprChildren.Count, 0); Assert.AreEqual(node.StatChildren.Count, 1); Assert.IsTrue(node.StatChildren[0] is EmptyNode); } }
public void Visit(IfElseNode node) { source.Append("If ("); node.GetCondition().Visit(this); source.AppendLine(")"); source.Indent++; node.GetBody().Visit(this); source.Indent--; source.AppendLine(); var lastBody = node.GetBody(); foreach (var childNode in node.GetElseIf().Children) { var elseIf = childNode as IfElseNode; source.Append("ElseIf ("); elseIf.GetCondition().Visit(this); source.AppendLine(")"); source.Indent++; elseIf.GetBody().Visit(this); source.Indent--; source.AppendLine(); lastBody = elseIf.GetBody(); } if (node.GetElse().Size != 0) { source.Append("Else"); source.Indent++; source.AppendLine(); node.GetElse().Visit(this); source.Indent--; source.AppendLine(); } source.Append("EndIf"); }
private IfElseNode Process(IfElseNode node) { Process(node.GetCondition()); Process(node.GetTrue()); Process(node.GetFalse()); return(node); }
public void CreateIfElseMethod() { List <TypeSpecifier> argumentTypes = new List <TypeSpecifier>() { typeof(int), typeof(bool), }; List <TypeSpecifier> returnTypes = new List <TypeSpecifier>() { typeof(int), }; // Create method ifElseMethod = new Method("IfElse") { Modifiers = MethodModifiers.Public }; ifElseMethod.ArgumentTypes.AddRange(argumentTypes); ifElseMethod.ReturnTypes.AddRange(returnTypes); // Create nodes IfElseNode ifElseNode = new IfElseNode(ifElseMethod); LiteralNode literalNode = new LiteralNode(ifElseMethod, typeof(int), 123); // Connect exec nodes GraphUtil.ConnectExecPins(ifElseMethod.EntryNode.InitialExecutionPin, ifElseNode.ExecutionPin); GraphUtil.ConnectExecPins(ifElseNode.TruePin, ifElseMethod.ReturnNode.ReturnPin); GraphUtil.ConnectExecPins(ifElseNode.FalsePin, ifElseMethod.ReturnNode.ReturnPin); // Connect node data GraphUtil.ConnectDataPins(ifElseMethod.EntryNode.OutputDataPins[1], ifElseNode.ConditionPin); GraphUtil.ConnectDataPins(ifElseMethod.EntryNode.OutputDataPins[0], ifElseMethod.ReturnNode.InputDataPins[0]); GraphUtil.ConnectDataPins(literalNode.ValuePin, ifElseMethod.ReturnNode.InputDataPins[0]); }
public void VisitIfElse(IfElseNode node) { _visitor.VisitStatement(node); node.Condition.AcceptSyntaxTreeVisitor(_childrenVisitor); VisitBody(node.IfBody); VisitBody(node.ElseBody); }
public void VisitIfElse(IfElseNode node) { _scopeManager.SetScope(node, _currentScope); RecurseExpression(node.Condition); node.IfBody.AcceptStatementVisitor(this); node.ElseBody.AcceptStatementVisitor(this); }
public override void VisitIfElseNode(IfElseNode i) { Text += IndentStr() + "if "; i.Expr.Visit(this); if (i.TrueStat is BlockNode) { Text += " "; i.TrueStat.Visit(this); } else { IndentPlus(); Text += Environment.NewLine; i.TrueStat.Visit(this); IndentMinus(); } if (i.FalseStat == null) { return; } Text += Environment.NewLine + IndentStr() + "else"; if (i.FalseStat is BlockNode) { Text += " "; i.FalseStat.Visit(this); } else { IndentPlus(); Text += Environment.NewLine; i.FalseStat.Visit(this); IndentMinus(); } }
/// <summary> /// Process an If-Else node. /// </summary> /// <param name="node">If-Else node to be processed.</param> /// <param name="depth">Current indentation.</param> /// <param name="omitIndent">If true, the 'if' should NOT be indented.</param> private void ProcessIfElse(IfElseNode node, int depth, bool omitIndent) { if (omitIndent) { Emit("if ("); } else { EmitIndented(depth, "if ("); } ProcessCondition(node.Condition); Emit(") {"); ProcessNode(node.IfBranch, depth + 1); if (node.ElseBranch is EmptyNode) { EmitIndented(depth, "}"); } else if (node.ElseBranch is IfElseNode) { EmitIndented(depth, "} else "); ProcessIfElse((IfElseNode)node.ElseBranch, depth, true); // no close brace!!! } else { EmitIndented(depth, "} else {"); ProcessNode(node.ElseBranch, depth + 1); EmitIndented(depth, "}"); } }
public override void VisitIfElseNode(IfElseNode i) { // перевод в трёхадресный код условия var exprTmpName = Gen(i.Expr); var trueLabel = i.TrueStat is LabelStatementNode label ? label.Label.Num.ToString() : i.TrueStat is BlockNode block && block.List.StatChildren[0] is LabelStatementNode labelB ? labelB.Label.Num.ToString() : ThreeAddressCodeTmp.GenTmpLabel(); var falseLabel = ThreeAddressCodeTmp.GenTmpLabel(); GenCommand("", "ifgoto", exprTmpName, trueLabel, ""); // перевод в трёхадресный код false ветки i.FalseStat?.Visit(this); GenCommand("", "goto", falseLabel, "", ""); // перевод в трёхадресный код true ветки var instructionIndex = Instructions.Count; i.TrueStat.Visit(this); Instructions[instructionIndex].Label = trueLabel; GenCommand(falseLabel, "noop", "", "", ""); }
public override void VisitIfElseNode(IfElseNode i) { Text.Append(IndentStr() + "if ("); i.Expr.Visit(this); Text.Append(" != 0)\n"); if (!(i.Stat is BlockNode)) { IndentPlus(); } i.Stat.Visit(this); if (!(i.Stat is BlockNode)) { IndentMinus(); } Text.Append(Environment.NewLine + IndentStr() + "else" + Environment.NewLine); if (!(i.ElseStat is BlockNode)) { IndentPlus(); } i.ElseStat.Visit(this); if (!(i.ElseStat is BlockNode)) { IndentMinus(); } }
private static IfElseNode CreateIfElse(string condition, Node in1, Node in2) { var node = new IfElseNode(); node.InputCondition.Value = condition; node.InputThen.ConnectedNode = in1; node.InputElse.ConnectedNode = in2; return(node); }
public void VisitIfElse(IfElseNode node) { // Don't insert unreachable code if (!_builder.InsertBlock.IsValid) { return; } Metadata oldLexicalScope = _lexicalScope; if (_genContext.DebugInfo) { _genContext.TryGetNodeSymbol(node.Condition, out Symbol condRange); _lexicalScope = _genContext.DiBuilder.CreateLexicalBlock(_lexicalScope, _genContext.DiFile, condRange.LLVMLine, _genContext.ColumnInfo ? condRange.LLVMColumn : 0); } node.Condition.AcceptExpressionVisitor(this); Value condVal = _visitedValue; if (!condVal.IsValid) { throw new InvalidOperationException("condition did not produce a usable rvalue"); } SetCurrentDebugLocation(node); condVal = ConvertToBool(condVal); BasicBlock ifThenBasicBlock = _genContext.Context.AppendBasicBlock(_function.FunctionValue, "if.then"); BasicBlock ifElseBasicBlock = BasicBlock.Null; if (node.ElseBody.Statements.Count > 0) { ifElseBasicBlock = _genContext.Context.CreateBasicBlock("if.else"); } BasicBlock ifEndBasicBlock = _genContext.Context.CreateBasicBlock("if.end"); _builder.BuildCondBr(condVal, ifThenBasicBlock, ifElseBasicBlock.IsValid ? ifElseBasicBlock : ifEndBasicBlock); _builder.PositionAtEnd(ifThenBasicBlock); VisitBody(node.IfBody); BuildBrIfNecessary(ifEndBasicBlock); if (node.ElseBody.Statements.Count > 0) { _function.FunctionValue.AppendExistingBasicBlock(ifElseBasicBlock); _builder.PositionAtEnd(ifElseBasicBlock); VisitBody(node.ElseBody); BuildBrIfNecessary(ifEndBasicBlock); } _function.FunctionValue.AppendExistingBasicBlock(ifEndBasicBlock); _builder.PositionAtEnd(ifEndBasicBlock); _lexicalScope = oldLexicalScope; }
public override void VisitIfElseNode(IfElseNode i) { PreVisit(i); i.Expr.Visit(this); i.TrueStat.Visit(this); if (i.FalseStat != null) { i.FalseStat.Visit(this); } PostVisit(i); }
public object Visit(IfElseNode node) { var condition = node.Condition.Accept(this); if (condition is bool bvalue) { return(bvalue ? node.IfInstr.Accept(this) : node.ElseInstr.Accept(this)); } throw new Exception("A condition must return a boolean value"); }
public override void VisitIfElseNode(IfElseNode i) { CurrentDepth++; i.Expr.Visit(this); i.Stat.Visit(this); i.ElseStat.Visit(this); if (CurrentDepth > MaxNest) { MaxNest = CurrentDepth; } CurrentDepth--; }
public override void VisitIfElseNode(IfElseNode i) { Text += IndentStr() + "if "; i.Expr.Visit(this); Text += " then" + Environment.NewLine; IndentPlus(); i.Stat.Visit(this); IndentMinus(); Text += Environment.NewLine + IndentStr() + "else" + Environment.NewLine; IndentPlus(); i.ElseStat.Visit(this); IndentMinus(); }
public void VisitIfElse(IfElseNode node) { node.Condition.AcceptSyntaxTreeVisitor(_childrenVisitor); bool wasAssigned = _isTargetCurrentlyAssigned; node.IfBody.AcceptStatementVisitor(this); bool assignedInIf = _isTargetCurrentlyAssigned; _isTargetCurrentlyAssigned = wasAssigned; node.ElseBody.AcceptStatementVisitor(this); bool assignedInElse = _isTargetCurrentlyAssigned; _isTargetCurrentlyAssigned = wasAssigned || assignedInIf && assignedInElse; }
public void test_if_else() { var testNode = new IfElseNode( "testNode", new OnCheckCondition(context => { return(false); }), thenNode.Object, elseNode.Object ); testNode.Execute(bContext.Object); thenNode.Verify(x => x.Execute(bContext.Object), Times.Never); elseNode.Verify(x => x.Execute(bContext.Object)); }
public void VisitIfElse(IfElseNode node) { // TODO: Add unreachable code warning if we've already returned if (!_didReturn) { node.IfBody.VisitStatements(this); if (!_didReturn) { return; } node.ElseBody.VisitStatements(this); } }
public override void VisitIfElseNode(IfElseNode i) { string endLabel, exprTmpName; if (i.FalseStat == null) { var wasInverted = InvertIfExpression(i); exprTmpName = Gen(i.Expr); endLabel = GenEndLabel(i); if (wasInverted) { GenCommand("", "ifgoto", exprTmpName, endLabel, ""); } else { var tmpVar = ThreeAddressCodeTmp.GenTmpName(); GenCommand("", "assign", $"!{exprTmpName}", "", tmpVar); GenCommand("", "ifgoto", tmpVar, endLabel, ""); } i.TrueStat.Visit(this); } else { exprTmpName = Gen(i.Expr); var trueLabel = i.TrueStat is LabelStatementNode label ? label.Label.Num.ToString(CultureInfo.InvariantCulture) : i.TrueStat is BlockNode block && block.List.StatChildren[0] is LabelStatementNode labelB ? labelB.Label.Num.ToString(CultureInfo.InvariantCulture) : ThreeAddressCodeTmp.GenTmpLabel(); GenCommand("", "ifgoto", exprTmpName, trueLabel, ""); i.FalseStat.Visit(this); endLabel = GenEndLabel(i); GenCommand("", "goto", endLabel, "", ""); var instructionIndex = Instructions.Count; i.TrueStat.Visit(this); Instructions[instructionIndex].Label = trueLabel; } if (endLabel.StartsWith("L", System.StringComparison.Ordinal)) { GenCommand(endLabel, "noop", "", "", ""); } }
public void CreateIfElseMethod() { // Create method ifElseMethod = new Method("IfElse") { Visibility = MemberVisibility.Public }; // Add arguments List <TypeNode> argTypeNodes = new List <TypeNode>() { new TypeNode(ifElseMethod, TypeSpecifier.FromType <int>()), new TypeNode(ifElseMethod, TypeSpecifier.FromType <bool>()), }; for (int i = 0; i < argTypeNodes.Count; i++) { ifElseMethod.EntryNode.AddArgument(); GraphUtil.ConnectTypePins(argTypeNodes[i].OutputTypePins[0], ifElseMethod.EntryNode.InputTypePins[i]); } // Add return types List <TypeNode> returnTypeNodes = new List <TypeNode>() { new TypeNode(ifElseMethod, TypeSpecifier.FromType <int>()), }; for (int i = 0; i < returnTypeNodes.Count; i++) { ifElseMethod.MainReturnNode.AddReturnType(); GraphUtil.ConnectTypePins(returnTypeNodes[i].OutputTypePins[0], ifElseMethod.MainReturnNode.InputTypePins[i]); } // Create nodes IfElseNode ifElseNode = new IfElseNode(ifElseMethod); LiteralNode literalNode = LiteralNode.WithValue(ifElseMethod, 123); // Connect exec nodes GraphUtil.ConnectExecPins(ifElseMethod.EntryNode.InitialExecutionPin, ifElseNode.ExecutionPin); GraphUtil.ConnectExecPins(ifElseNode.TruePin, ifElseMethod.ReturnNodes.First().ReturnPin); GraphUtil.ConnectExecPins(ifElseNode.FalsePin, ifElseMethod.ReturnNodes.First().ReturnPin); // Connect node data GraphUtil.ConnectDataPins(ifElseMethod.EntryNode.OutputDataPins[1], ifElseNode.ConditionPin); GraphUtil.ConnectDataPins(ifElseMethod.EntryNode.OutputDataPins[0], ifElseMethod.ReturnNodes.First().InputDataPins[0]); GraphUtil.ConnectDataPins(literalNode.ValuePin, ifElseMethod.ReturnNodes.First().InputDataPins[0]); }
public void TranslateIfElseNode(IfElseNode node) { // Translate all the pure nodes this node depends on in // the correct order TranslateDependentPureNodes(node); string conditionVar = node.ConditionPin.IncomingPin != null? GetOrCreatePinName(node.ConditionPin.IncomingPin) : "false"; builder.AppendLine($"if({conditionVar})"); builder.AppendLine("{"); WriteGotoOutputPin(node.TruePin); builder.AppendLine("}"); builder.AppendLine("else"); builder.AppendLine("{"); WriteGotoOutputPin(node.FalsePin); builder.AppendLine("}"); }
private Statement ProcessIfElseStatement(IfElseNode node) { Expression condition = _expressionBuilder.BuildExpression(node.Condition); if (condition is MemberExpression) { condition = _expressionBuilder.TransformMemberExpression((MemberExpression)condition); } Statement ifStatement = BuildStatement((StatementNode)node.IfBlock); Statement elseStatement = null; if (node.ElseBlock != null) { elseStatement = BuildStatement((StatementNode)node.ElseBlock); } return(new IfElseStatement(condition, ifStatement, elseStatement)); }
private string putAttributes(BaseNode node) { StringBuilder sb = new StringBuilder(); if (node is DeclareNode) { DeclareNode declareNode = (DeclareNode)node; sb.Append(" Variable_Name = \"" + declareNode._Var.VarName + "\""); sb.Append(" Variable_Type = \"" + declareNode._Var.VarType + "\""); sb.Append(" Single_Variable = \"" + declareNode._Var.Single + "\""); sb.Append(" Size = \"" + declareNode._Var.Size + "\""); } if (node is IfElseNode) { IfElseNode ifElseNode = (IfElseNode)node; sb.Append(" True_End_Location = \"" + ifElseNode.BackNode.NodeLocation.X.ToString() + "," + ifElseNode.BackNode.NodeLocation.Y.ToString() + "\" "); sb.Append(" False_End_Location = \"" + ifElseNode.BackfalseNode.NodeLocation.X.ToString() + "," + ifElseNode.BackfalseNode.NodeLocation.Y.ToString() + "\" "); sb.Append(" Mid_Location = \"" + ifElseNode.MiddleNode.NodeLocation.X.ToString() + "," + ifElseNode.MiddleNode.NodeLocation.Y.ToString() + "\" "); } else if (node is IfNode) { IfNode ifNode = (IfNode)node; sb.Append(" True_End_Location = \"" + ifNode.BackNode.NodeLocation.X.ToString() + "," + ifNode.BackNode.NodeLocation.Y.ToString() + "\" "); sb.Append(" Mid_Location = \"" + ifNode.MiddleNode.NodeLocation.X.ToString() + "," + ifNode.MiddleNode.NodeLocation.Y.ToString() + "\" "); } else if (node is DecisionNode) { DecisionNode decisionNode = (DecisionNode)node; sb.Append(" True_End_Location = \"" + decisionNode.BackNode.NodeLocation.X.ToString() + "," + decisionNode.BackNode.NodeLocation.Y.ToString() + "\" "); if (node is ForNode) { ForNode forNode = node as ForNode; sb.Append(" Variable = \"" + forNode.Var + "\" "); sb.Append(" Direction = \"" + forNode.Direction + "\" "); sb.Append(" StartVal = \"" + forNode.StartVal + "\" "); sb.Append(" EndVal = \"" + forNode.EndVal + "\" "); sb.Append(" StepBy = \"" + forNode.StepBy + "\" "); } } sb.Append(" Statment = \"" + xmlWithEscapes(node.Statement) + "\" "); sb.Append(" Location = \"" + node.NodeLocation.X.ToString() + "," + node.NodeLocation.Y.ToString() + "\" "); return(sb.ToString()); }
//This is all body transferring private void traverseRight() { if (current.rightChild() == null) { traverseLeft(); return; } BlockNode newBlock = current.rightChild(); if (newBlock is IConditional) { IConditional lop = (IConditional)newBlock; if (!lop.didPass(this)) { if (lop is IfElseNode) { IfElseNode ifElse = (IfElseNode)lop; if (ifElse.Else == null) { traverseLeft(); } else { current = ifElse.Else; bodies.Push(current); } } else { traverseLeft(); } } else { current = newBlock; bodies.Push(current); } } }
public override void VisitIfElseNode(IfElseNode i) { // перевод в трёхадресный код условия string exprTmpName = Gen(i.Expr); string trueLabel = ThreeAddressCodeTmp.GenTmpLabel(); string falseLabel = ThreeAddressCodeTmp.GenTmpLabel(); GenCommand("", "ifgoto", exprTmpName, trueLabel, ""); // перевод в трёхадресный код false ветки i.FalseStat?.Visit(this); GenCommand("", "goto", falseLabel, "", ""); // перевод в трёхадресный код true ветки var instructionIndex = Instructions.Count; i.TrueStat.Visit(this); Instructions[instructionIndex].Label = trueLabel; GenCommand(falseLabel, "noop", "", "", ""); }
public void RemoveInnerIf2() { // if (a) // if (b) EmptyNode; var ifInner = new IfElseNode(new IdNode("b"), new EmptyNode()); var ifOuter = new IfElseNode(new IdNode("a"), ifInner); ifInner.Parent = ifOuter; var root = new StListNode(ifOuter); ifOuter.Parent = root; var opt = new IfNullElseNull(); root.Visit(opt); Assert.IsNull(root.Parent); Assert.AreEqual(root.ExprChildren.Count, 0); Assert.AreEqual(root.StatChildren.Count, 1); Assert.IsTrue(root.StatChildren[0] is EmptyNode); }
private static string GenEndLabel(IfElseNode i) { Node parent, curNode; if (i.Parent is LabelStatementNode) { parent = i.Parent.Parent; curNode = i.Parent; } else { parent = i.Parent; curNode = i; } var ifIndex = parent.StatChildren.IndexOf(curNode as StatementNode); return(parent.StatChildren.Count > ifIndex + 1 ? // next statement exists (parent.StatChildren[ifIndex + 1] is LabelStatementNode nextLabelNode) ? // next statement has label nextLabelNode.Label.Num.ToString(CultureInfo.InvariantCulture) : ThreeAddressCodeTmp.GenTmpLabel() : ThreeAddressCodeTmp.GenTmpLabel()); }
public void RemoveSimple() { // if (a) EmptyNode; // if (a) EmptyNode; else EmptyNode; var if1 = new IfElseNode(new IdNode("a"), new EmptyNode()); var if2 = new IfElseNode(new IdNode("a"), new EmptyNode(), new EmptyNode()); var root = new StListNode(if1); root.Add(if2); if1.Parent = if2.Parent = root; var opt = new IfNullElseNull(); root.Visit(opt); Assert.IsNull(root.Parent); Assert.AreEqual(root.ExprChildren.Count, 0); Assert.AreEqual(root.StatChildren.Count, 2); Assert.IsTrue(root.StatChildren[0] is EmptyNode); Assert.IsTrue(root.StatChildren[1] is EmptyNode); }