Exemplo n.º 1
0
        public override Node VisitIf(DeltinScriptParser.IfContext context)
        {
            // Get the if data
            IfData ifData = new IfData
                            (
                VisitExpr(context.expr()),
                (BlockNode)VisitBlock(context.block())
                            );

            // Get the else-if data
            IfData[] elseIfData = null;
            if (context.else_if() != null)
            {
                elseIfData = new IfData[context.else_if().Length];
                for (int i = 0; i < context.else_if().Length; i++)
                {
                    elseIfData[i] = new IfData
                                    (
                        VisitExpr(context.else_if()[i].expr()),
                        (BlockNode)VisitBlock(context.else_if()[i].block())
                                    );
                }
            }

            // Get the else block
            BlockNode elseBlock = null;

            if (context.@else() != null)
            {
                elseBlock = (BlockNode)VisitBlock(context.@else().block());
            }

            return(new IfNode(ifData, elseIfData, elseBlock, new Location(file, Range.GetRange(context))));
        }
        public IfAction(ParseInfo parseInfo, Scope scope, DeltinScriptParser.IfContext ifContext)
        {
            if (ifContext.expr() == null)
            {
                parseInfo.Script.Diagnostics.Error("Expected expression.", DocRange.GetRange(ifContext.LEFT_PAREN()));
            }
            else
            {
                Expression = DeltinScript.GetExpression(parseInfo, scope, ifContext.expr());
            }

            var paths = new List <PathInfo>();

            if (ifContext.block() == null)
            {
                parseInfo.Script.Diagnostics.Error("Expected block.", DocRange.GetRange(ifContext.IF()));
            }
            else
            {
                Block = new BlockAction(parseInfo, scope, ifContext.block());
            }
            paths.Add(new PathInfo(Block, DocRange.GetRange(ifContext.IF()), false));

            if (ifContext.else_if() != null)
            {
                ElseIfs = new ElseIf[ifContext.else_if().Length];
                for (int i = 0; i < ElseIfs.Length; i++)
                {
                    ElseIfs[i] = new ElseIf(parseInfo, scope, ifContext.else_if(i));
                    paths.Add(new PathInfo(Block, DocRange.GetRange(ifContext.else_if(i).ELSE(), ifContext.else_if(i).IF()), false));
                }
            }
            else
            {
                ElseIfs = new ElseIf[0];
            }

            // If there is an else statement, get the else block.
            if (ifContext.@else() != null)
            {
                if (ifContext.block() == null)
                {
                    parseInfo.Script.Diagnostics.Error("Expected block.", DocRange.GetRange(ifContext.@else().block()));
                }
                else
                {
                    ElseBlock = new BlockAction(parseInfo, scope, ifContext.@else().block());
                }
                // Add the else path info.
                paths.Add(new PathInfo(ElseBlock, DocRange.GetRange(ifContext.@else().ELSE()), true));
            }
            Paths = paths.ToArray();
        }
        public IfNode(DeltinScriptParser.IfContext context, BuildAstVisitor visitor) : base(new Location(visitor.file, DocRange.GetRange(context)))
        {
            // Get the if data
            IfData = new IfData
                     (
                visitor.VisitExpr(context.expr()),
                (BlockNode)visitor.VisitBlock(context.block())
                     );
            paths.Add(new PathInfo(IfData.Block, new Location(visitor.file, DocRange.GetRange(context.IF())), false));

            // Get the else-if data
            ElseIfData = null;
            if (context.else_if() != null)
            {
                ElseIfData = new IfData[context.else_if().Length];
                for (int i = 0; i < context.else_if().Length; i++)
                {
                    ElseIfData[i] = new IfData
                                    (
                        visitor.VisitExpr(context.else_if(i).expr()),
                        (BlockNode)visitor.VisitBlock(context.else_if(i).block())
                                    );
                    paths.Add(
                        new PathInfo(
                            ElseIfData[i].Block,
                            new Location(
                                visitor.file,
                                DocRange.GetRange(
                                    context.else_if(i).ELSE(),
                                    context.else_if(i).IF()
                                    )
                                ),
                            false
                            )
                        );
                }
            }

            // Get the else block
            ElseBlock = null;
            if (context.@else() != null)
            {
                ElseBlock = (BlockNode)visitor.VisitBlock(context.@else().block());
                paths.Add(new PathInfo(ElseBlock, new Location(visitor.file, DocRange.GetRange(context.@else().ELSE())), true));
            }
        }
Exemplo n.º 4
0
        public IfAction(ParseInfo parseInfo, Scope scope, DeltinScriptParser.IfContext ifContext)
        {
            // Get the if condition.
            if (ifContext.expr() != null)
            {
                Expression = parseInfo.GetExpression(scope, ifContext.expr());
            }
            else
            {
                parseInfo.Script.Diagnostics.Error("Expected expression.", DocRange.GetRange(ifContext.LEFT_PAREN()));
            }

            // Contains the path info of all blocks in the if/else-if/else list.
            var paths = new List <PathInfo>();

            // Get the if's block.
            if (ifContext.block() != null)
            {
                Block = new BlockAction(parseInfo, scope, ifContext.block());
            }
            else
            {
                parseInfo.Script.Diagnostics.Error("Expected block.", DocRange.GetRange(ifContext.IF()));
            }

            // Add the if block path info.
            paths.Add(new PathInfo(Block, DocRange.GetRange(ifContext.IF()), false));

            // Get the else-ifs.
            if (ifContext.else_if() != null)
            {
                ElseIfs = new ElseIf[ifContext.else_if().Length];
                for (int i = 0; i < ElseIfs.Length; i++)
                {
                    ElseIfs[i] = new ElseIf(parseInfo, scope, ifContext.else_if(i));
                    paths.Add(new PathInfo(Block, DocRange.GetRange(ifContext.else_if(i).ELSE(), ifContext.else_if(i).IF()), false));
                }
            }
            // If there is none, set `ElseIfs` to an empty array since it should not be null.
            else
            {
                ElseIfs = new ElseIf[0];
            }

            // If there is an else statement, get the else block.
            if (ifContext.@else() != null)
            {
                if (ifContext.block() == null)
                {
                    parseInfo.Script.Diagnostics.Error("Expected block.", DocRange.GetRange(ifContext.@else().block()));
                }
                else
                {
                    ElseBlock = new BlockAction(parseInfo, scope, ifContext.@else().block());
                }

                // Add the else path info.
                paths.Add(new PathInfo(ElseBlock, DocRange.GetRange(ifContext.@else().ELSE()), true));
            }
            Paths = paths.ToArray();
        }