예제 #1
0
        // TODO: implement nesting bs for every control flow statement (ifelsewhile etc)

        public override bool VisitStmtIfElse([NotNull] GamaParser.StmtIfElseContext context)
        {
            var expr = VisitExpression(context.expr());

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

            if (expr.Type != InstanceTypes.Bool)
            {
                NamespaceContext.Context.AddError(new ErrorTypeMismatch(context.expr()));
                return(false);
            }

            /* LLVM */
            CurrentBlock.PositionBuilderAtEnd(Builder);

            var iftrue  = Self.AddBlock("if.true");
            var iffalse = Self.AddBlock("if.false");
            var ifend   = Self.AddBlock("if.end");

            Builder.BuildCondBr(expr.Value, iftrue.Block, iffalse.Block);

            // Compile true and false blocks
            SetBlock(iftrue);
            Push();
            {
                Visit(context.block(0));
            }
            Pop();
            if (!iftrue.HasTerminator())
            {
                iftrue.PositionBuilderAtEnd(Builder);
                Builder.BuildBr(ifend.Block);
            }
            if (CurrentBlock != iftrue) /* Block nesting */
            {
                CurrentBlock.PositionBuilderAtEnd(Builder);
                Builder.BuildBr(ifend.Block);
            }
            SetBlock(iffalse);
            Push();
            {
                Visit(context.block(1));
            }
            if (!iffalse.HasTerminator())
            {
                iffalse.PositionBuilderAtEnd(Builder);
                Builder.BuildBr(ifend.Block);
            }
            if (CurrentBlock != iffalse) /* Block nesting */
            {
                CurrentBlock.PositionBuilderAtEnd(Builder);
                Builder.BuildBr(ifend.Block);
            }
            Pop();

            SetBlock(ifend);

            return(true);
        }
예제 #2
0
 /// <summary>
 /// Visit a parse tree produced by the <c>StmtIfElse</c>
 /// labeled alternative in <see cref="GamaParser.stmtIf"/>.
 /// <para>
 /// The default implementation returns the result of calling <see cref="AbstractParseTreeVisitor{Result}.VisitChildren(IRuleNode)"/>
 /// on <paramref name="context"/>.
 /// </para>
 /// </summary>
 /// <param name="context">The parse tree.</param>
 /// <return>The visitor result.</return>
 public virtual Result VisitStmtIfElse([NotNull] GamaParser.StmtIfElseContext context)
 {
     return(VisitChildren(context));
 }
예제 #3
0
 /// <summary>
 /// Exit a parse tree produced by the <c>StmtIfElse</c>
 /// labeled alternative in <see cref="GamaParser.stmtIf"/>.
 /// <para>The default implementation does nothing.</para>
 /// </summary>
 /// <param name="context">The parse tree.</param>
 public virtual void ExitStmtIfElse([NotNull] GamaParser.StmtIfElseContext context)
 {
 }