protected override Void handleIf(IfStatementNode ifStatement, Void source)
        {
            var condition = ifStatement.Condition;

            this.ExpressionValidator.handleExpression(condition, context.TypeSystem.BooleanType, true);
            ValidationHelper.setBoxing(context, context.TypeSystem.BooleanType, condition);
            var cinfo = condition.getUserData(typeof(ExpressionInfo));

            if (cinfo == null || ValidationHelper.getType(context, condition) != context.TypeSystem.BooleanType)
            {
                throw context.error(CompileErrorId.NoImplicitConversion, condition,
                                    BytecodeHelper.getDisplayName(cinfo == null ? null : cinfo.Type),
                                    BytecodeHelper.getDisplayName(context.TypeSystem.BooleanType));
            }
            try {
                context.MemberResolver.enterScope();
                handleStatement(ifStatement.IfTrue, null);
            } finally {
                context.MemberResolver.leaveScope();
            }
            if (ifStatement.IfFalse != null)
            {
                try {
                    context.MemberResolver.enterScope();
                    handleStatement(ifStatement.IfFalse, null);
                } finally {
                    context.MemberResolver.leaveScope();
                }
            }
            return(null);
        }
Example #2
0
        public Node IfStatement()
        {
            var if_node = new IfStatementNode()
            {
                AnchorToken = Expect(TokenCategory.IF)
            };

            if_node.Add(Expression());
            Expect(TokenCategory.THEN);
            if_node.Add(new StatementListNode()
            {
                ZeroOrMore(firstOfStatement, Statement)
            });
            if (Has(TokenCategory.ELSEIF))
            {
                if_node.Add(new ElseIfListNode()
                {
                    ZeroOrMore(TokenCategory.ELSEIF, () =>
                    {
                        return(ElifStatement());
                    })
                });
            }

            if (Has(TokenCategory.ELSE))
            {
                if_node.Add(ElseStatement());
            }

            Expect(TokenCategory.END);
            Expect(TokenCategory.SEMICOLON);
            return(if_node);
        }
 private void ProcessIfStatement(IfStatementNode ifStatementNode)
 {
     if (EvaluateIfStatementCondition(ifStatementNode.Expression))
     {
         ProcessCallStatement(ifStatementNode.Statement as CallStatementNode);
     }
 }
Example #4
0
        private void Write(IfStatementNode stmt)
        {
            Fill();
            _code.Append("if ");
            var numberOfIfs = (stmt.Children.Last() is IfStatementTestNode) ? stmt.Children.Count : stmt.Children.Count - 1;

            for (var i = 0; i < numberOfIfs; i++)
            {
                var test = stmt.Children[i];
                Write(test.Children[0]);
                Enter();
                Write(test.Children[1]);
                Leave();
                if (i < numberOfIfs - 1)
                {
                    Fill();
                    _code.Append("elif ");
                }
            }
            if (!(stmt.Children.Last() is IfStatementTestNode))
            {
                Fill();
                _code.Append("else");
                Enter();
                Write(stmt.Children.Last());
                Leave();
            }
        }
Example #5
0
 private object IfStatementNode(IfStatementNode i)
 {
     if ((bool)Evaluate(i.i))
     {
         return(Evaluate(i.truebody));
     }
     return(false);
 }
Example #6
0
        private IfStatement BindIfStatement(IfStatementNode node)
        {
            var condition     = BindExpression(node.Condition, BuiltinTypes.Bool);
            var thenStatement = BindStatement(node.Statement);
            var elseStatement = node.Else == null ? null : BindStatement(node.Else.Statement);

            return(new IfStatement(Scope, condition, thenStatement, elseStatement));
        }
Example #7
0
        private object ProcessIfStatement(IfStatementNode ifStatementNode)
        {
            if (EvaluateIfStatementCondition(ifStatementNode.Expression))
            {
                return(ProcessExpression(ifStatementNode.Statement));
            }

            return(string.Empty);
        }
Example #8
0
        private bool BindInIfStatement(IfStatementNode ifStatement, VariableIdentifierMap variableIdentifierMap)
        {
            TypeSymbolNode?ifConditionType = BindInExpression(ifStatement.ConditionNode, variableIdentifierMap);

            if (ifConditionType == null)
            {
                return(false);
            }

            TypeSymbolNode boolType = typeManager[FrameworkType.Bool];

            if (!TypeIsCompatibleWith(ifConditionType,
                                      boolType,
                                      possiblyOffendingNode: ifStatement.ConditionNode,
                                      out ImplicitConversionSymbolNode? conversion))
            {
                return(false);
            }

            if (conversion != null)
            {
                ifStatement.ConditionNode.SpecifyImplicitConversion(conversion);
            }

            bool success = BindInStatementBlock(ifStatement.StatementNodes, variableIdentifierMap);

            if (!success)
            {
                return(false);
            }

            foreach (ElseIfPartNode elseIfPart in ifStatement.ElseIfPartNodes)
            {
                TypeSymbolNode?elseIfConditionType = BindInExpression(elseIfPart.ConditionNode, variableIdentifierMap);

                if (elseIfConditionType == null)
                {
                    return(false);
                }

                success = BindInStatementBlock(elseIfPart.StatementNodes, variableIdentifierMap);

                if (!success)
                {
                    return(false);
                }
            }

            success = true;

            if (ifStatement.ElsePartNode != null)
            {
                success = BindInStatementBlock(ifStatement.ElsePartNode.StatementNodes, variableIdentifierMap);
            }

            return(success);
        }
Example #9
0
 public virtual void VisitIfStatementNode(IfStatementNode node)
 {
     Visit(node.IfKeywordNode);
     Visit(node.ConditionNode);
     Visit(node.ThenKeywordNode);
     Visit(node.ThenStatementNode);
     Visit(node.ElseKeywordNode);
     Visit(node.ElseStatementNode);
 }
Example #10
0
 protected override Void handleIf(IfStatementNode ifStatement, Set <TypeInfo> dependencies)
 {
     expressionHandler.handleExpression(ifStatement.Condition, dependencies, true);
     handleStatement(ifStatement.IfTrue, dependencies);
     if (ifStatement.IfFalse != null)
     {
         handleStatement(ifStatement.IfFalse, dependencies);
     }
     return(null);
 }
Example #11
0
 /// <summary>
 /// This method prints the ifStatementNode and make an indentation
 /// It accepts Expresstion if there is any and also accepts all ifStatement nodes
 /// Then outdent
 /// </summary>
 /// <param name="ifStatementNode">The node to print.</param>
 /// <returns>Returns null</returns>
 public override object Visit(IfStatementNode ifStatementNode)
 {
     Print("IfstatementNode");
     Indent++;
     ifStatementNode.Expression?.Accept(this);
     if (ifStatementNode.Statements.Any())
     {
         ifStatementNode.Statements.ForEach(node => node.Accept(this));
     }
     Indent--;
     return(null);
 }
        public override string VisitIfStatement(IfStatementNode ifStatement)
        {
            var str = "";

            str += $"{AddSpaces()}if({VisitNode(ifStatement.IfCheck)})\r\n";
            str += $"{VisitNode(ifStatement.IfTrue)};\r\n";
            if (ifStatement.IfFalse != null)
            {
                str += $"{AddSpaces()}else\r\n";
                str += $"{VisitNode(ifStatement.IfFalse)};\r\n";
            }

            return(str);
        }
Example #13
0
        /// <summary>
        /// if条件语句
        /// </summary>
        /// <param name="node"></param>
        public void Visit(IfStatementNode node)
        {
            var builder = new StringBuilder();

            builder.Append("if条件语句:");
            builder.Append("  条件:");
            Visit((Object)node.Condition);
            builder.AppendLine("条件语句块:");
            Console.WriteLine(builder.ToString());
            foreach (var item in node.SubNodes)
            {
                Visit((Object)item);
            }
        }
Example #14
0
        public override void Visit(IfStatementNode node)
        {
            string ElseLabel    = ElseLabelGenerator.GetNewLabel();
            string ElseEndLabel = ElseLabel + "End";

            node.expression.Accept(this);
            Gen("cmp", "eax", "0");
            Gen("je", ElseLabel);
            node.thenStatement.Accept(this);
            Gen("jmp", ElseEndLabel);
            GenText(ElseLabel + ":");
            node.elseStatement.Accept(this);
            GenText(ElseEndLabel + ":");
        }
Example #15
0
 /// <summary>
 /// This method type checks the IfStatementNode node in the AST.
 /// </summary>
 /// <param name="ifStatementNode">The node to check.</param>
 /// <returns>Returns null</returns>
 public override object Visit(IfStatementNode ifStatementNode)
 {
     CurrentScope = GlobalScope.FindChild($"{ifStatementNode.Type}_{ifStatementNode.Line}");
     if (((TypeContext)ifStatementNode.Expression.Accept(this)).Type == BOOL)
     {
         ifStatementNode.Statements.ForEach(stmnt => stmnt.Accept(this));
     }
     else
     {
         new InvalidTypeException($"If statement expected a boolean expression at {ifStatementNode.Line}:{ifStatementNode.Offset}");
     }
     CurrentScope = CurrentScope.Parent ?? GlobalScope;
     return(null);
 }
Example #16
0
 public override void Visit(IfStatementNode node)
 {
     Console.WriteLine(this.indentation + "If           --- Statement ----");
     indentation = indentation + "   ";
     node.expression.Accept(this);
     indentation = indentation.Substring(0, indentation.Length - 3);
     Console.WriteLine(this.indentation + "Then");
     indentation = indentation + "   ";
     node.thenStatement.Accept(this);
     indentation = indentation.Substring(0, indentation.Length - 3);
     Console.WriteLine(this.indentation + "Else");
     indentation = indentation + "   ";
     node.elseStatement.Accept(this);
     indentation = indentation.Substring(0, indentation.Length - 3);
 }
Example #17
0
        private IfStatementNode Visit(IfStatementNode node)
        {
            var     trueLabel = MakeLabel();
            var     nextLabel = MakeLabel();
            dynamic BoolProp  = new ExpandoObject();

            properties.Add(node.Condition, BoolProp);
            BoolProp.trueLabel  = trueLabel;
            BoolProp.falseLabel = nextLabel;
            node.Condition.Visit(this);
            generatedCode.Add(trueLabel);
            node.StatementNode.Visit(this);
            generatedCode.Add(nextLabel);
            return(node);
        }
Example #18
0
        private IfStatementNode Visit(IfStatementNode node)
        {
            var condBB = func.AppendBasicBlock($"cond_{++count}");
            var bodyBB = func.AppendBasicBlock($"body_{++count}");
            var contBB = func.AppendBasicBlock($"next_{++count}");

            builder.BuildBr(condBB);
            builder.PositionAtEnd(condBB);
            Visit(node.Condition);
            builder.BuildCondBr(GetProperty(node.Condition).addr, bodyBB, contBB);
            builder.PositionAtEnd(bodyBB);
            Visit(node.StatementNode);
            builder.BuildBr(contBB);
            builder.PositionAtEnd(contBB);
            return(node);
        }
        /// <summary>
        /// This method visits an if statement node
        /// It first write "if"
        /// Then it checks if there is an espression to accepts
        /// Then it accepts the statements
        /// </summary>
        /// <param name="ifStatementNode">The name of the node</param>
        /// <returns>It returns an if node</returns>
        public override object Visit(IfStatementNode ifStatementNode)
        {
            string ifNode = "";

            ifNode += "if(";
            ifNode += ifStatementNode.Expression?.Accept(this);
            ifNode += "){";
            if (ifStatementNode.Statements.Any())
            {
                ifStatementNode.Statements.ForEach(node => node.Parent = ifStatementNode);

                ifStatementNode.Statements.ForEach(node => ifNode += node.Accept(this));
            }
            ifNode += "}";
            return(ifNode);
        }
        protected override Boolean handleIf(IfStatementNode ifStatement, HashSet <StatementNode> visited)
        {
            var state = expressionChecker.handleExpression(ifStatement.Condition, visited, true);

            switch (state)
            {
            case Assigned:
                return(Boolean.FALSE);

            case AssignedAfterTrue:
                if (ifStatement.IfFalse != null)
                {
                    return(visitOrigin(ifStatement.IfFalse));
                }
                break;

            case AssignedAfterFalse:
                return(visitOrigin(ifStatement.IfTrue));

            default:
                ExpressionInfo cinfo = ifStatement.getCondition().getUserData(typeof(ExpressionInfo));
                if (cinfo.IsConstant)
                {
                    if ((Boolean)cinfo.Value)
                    {
                        return(visitOrigin(ifStatement.IfTrue));
                    }
                    else
                    {
                        return(visitOrigin(ifStatement.IfTrue));
                    }
                }
                Boolean tr = visitOrigin(ifStatement.IfTrue);
                Boolean rr = Boolean.TRUE;
                if (ifStatement.getIfFalse() != null)
                {
                    rr = visitOrigin(ifStatement.IfFalse);
                }
                if (!tr && !rr)
                {
                    return(Boolean.FALSE);
                }
                break;
            }
            return(Boolean.TRUE);
        }
        private IfStatementNode BuildIfStatementNode(
            LexSpan lIf,
            SyntaxNodeOrToken condition,
            SyntaxNodeOrToken body,
            LexSpan end,
            LexSpan rIf)
        {
            var ifExp = new IfStatementNode();

            ifExp.AddNode(new IfToken(lIf));
            ifExp.AddNode(condition);
            ifExp.AddNode(body);
            ifExp.AddNode(new EndToken(end));
            ifExp.AddNode(new IfToken(rIf));

            return(ifExp);
        }
Example #22
0
        private IfStatementNode Visit(IfStatementNode node)
        {
            dynamic props = new ExpandoObject();

            properties.Add(node, props);
            var code = new List <CodeEntry>();

            props.code = code;
            var trueLabel  = properties[node.Condition].trueLabel;
            var falseLabel = properties[node.Condition].falseLabel;

            code.AddRange(properties[node.Condition].code);
            code.Add(trueLabel);
            code.AddRange(properties[node.StatementNode].code);
            code.Add(falseLabel);
            return(node);
        }
Example #23
0
        public override void Visit(IfStatementNode node)
        {
            try
            {
                node.expression.Accept(this);

                if (!AreTypeCompatible(node.expression.ExpressionType.GetType(), typeof(BooleanType)))
                {
                    throw new Exception("If condition expression is not of type Boolean!");
                }
            }
            catch (Exception e)
            {
                Analysis.LogSemanticError(e.Message, node.lineNumber);
            }
            node.thenStatement.Accept(this);
            node.elseStatement.Accept(this);
        }
Example #24
0
        private static PythonNode Wrap(IfStatement stmt, PythonNode parent)
        {
            var result = new IfStatementNode(stmt)
            {
                Parent = parent
            };

            for (var i = 0; i < stmt.Tests.Count; i++)
            {
                var test  = stmt.Tests[i];
                var child = Wrap(test, result);
                result.AddChild(child);
            }
            if (stmt.ElseStatement != null)
            {
                result.HasElse = true;
                result.AddChild(Wrap(stmt.ElseStatement, result));
            }
            return(result);
        }
 public virtual Value evaluate(Context cx, IfStatementNode node)
 {
     output("<IfStatementNode position=\"" + node.pos() + "\">");
     indent_Renamed_Field++;
     if (node.condition != null)
     {
         node.condition.evaluate(cx, this);
     }
     if (node.thenactions != null)
     {
         node.thenactions.evaluate(cx, this);
     }
     if (node.elseactions != null)
     {
         node.elseactions.evaluate(cx, this);
     }
     indent_Renamed_Field--;
     output("</IfStatementNode>");
     return(null);
 }
        private void EmitIfStatement(IfStatementNode ifStatement)
        {
            output.Append("if(");
            EmitExpression(ifStatement.ConditionNode);
            output.Append(')');
            EmitStatementBlock(ifStatement.StatementNodes);

            foreach (ElseIfPartNode elseIfPart in ifStatement.ElseIfPartNodes)
            {
                output.Append("else if(");
                EmitExpression(elseIfPart.ConditionNode);
                output.Append(')');
                EmitStatementBlock(elseIfPart.StatementNodes);
            }

            if (ifStatement.ElsePartNode != null)
            {
                output.Append("else");
                EmitStatementBlock(ifStatement.ElsePartNode.StatementNodes);
            }
        }
Example #27
0
        public void Visit(IfStatementNode node)
        {
            currentIfId = id++;
            int previousElseCount = currentElseCount;

            currentElseCount = 0;

            builder.AppendLine();
            builder.AppendLine($"\tIf_{currentIfId}_0_condition:");
            Visit((dynamic)node[0]);
            builder.AppendLine($"\t\tbrzero If_{currentIfId}_1_condition");

            builder.AppendLine($"\tIf_{currentIfId}_0_body:");
            Visit((dynamic)node[1]);
            builder.AppendLine($"\t\tbr If_{currentIfId}_End");
            builder.AppendLine();

            VisitChildren(node, 2);
            builder.AppendLine($"\tIf_{currentIfId}_{currentElseCount + 1}_condition:");
            builder.AppendLine($"\tIf_{currentIfId}_End:");

            currentElseCount = previousElseCount;
        }
Example #28
0
        public void Visit(IfStatementNode node)
        {

            node.TrueNode.AcceptVisitor(this);
            AppendToResult(" if ");
            node.ConditionNode.AcceptVisitor(this);
            AppendToResult(" else ");
            node.FalseNode.AcceptVisitor(this);
        }
Example #29
0
        /// <summary>
        /// Visits the specified node.
        /// </summary>
        /// <param name="node">The node.</param>
        public void Visit(IfStatementNode node)
        {
            using (new DestinationNodeContext(node))
            {
                AppendToResult("(((bool?)");
                node.ConditionNode.AcceptVisitor(this);
                AppendToResult(").HasValue ? ");

                AppendToResult("(((bool)(");
                node.ConditionNode.AcceptVisitor(this);
                AppendToResult(")) ? (object) (");
                node.TrueNode.AcceptVisitor(this);
                AppendToResult(") : (object) (");
                node.FalseNode.AcceptVisitor(this);
                AppendToResult("))");

                AppendToResult(" : null)");
            }
        }
		public virtual Value evaluate(Context cx, IfStatementNode node)
		{
			output("<IfStatementNode position=\"" + node.pos() + "\">");
			indent_Renamed_Field++;
			if (node.condition != null)
			{
				node.condition.evaluate(cx, this);
			}
			if (node.thenactions != null)
			{
				node.thenactions.evaluate(cx, this);
			}
			if (node.elseactions != null)
			{
				node.elseactions.evaluate(cx, this);
			}
			indent_Renamed_Field--;
			output("</IfStatementNode>");
			return null;
		}
Example #31
0
 public virtual void VisitIfStatementNode(IfStatementNode node)
 {
     DefaultVisit(node);
 }
Example #32
0
 public virtual TAssociative VisitIfStatementNode(IfStatementNode node)
 {
     return(VisitAssociativeNode(node));
 }
 public virtual bool VisitIfStatementNode(IfStatementNode node)
 {
     return(DefaultVisit(node));
 }