Ejemplo n.º 1
0
        public override void EnterIfElse(FlyParser.IfElseContext context)
        {
            // TODO: Implement in CodeGenerator
            var endPositions = new List <int>();

            EnterExpression(context.ifExpr);
            Code.Instructions.Add(OpCode.JMP_FALSE);
            // Create an empty parameter which should be the end of the statement
            var ifEndPos = Code.Instructions.FillableInt();

            Code.Instructions.Block(() =>
            {
                foreach (var statement in context._if)
                {
                    EnterStatement(statement);
                }
            });
            Code.Instructions.Add(OpCode.JMP);
            endPositions.Add(Code.Instructions.FillableInt());

            Code.Instructions.FillInt(ifEndPos, Code.Instructions.Count);
            if (context._elifExpr.Count > 0)
            {
                for (var i = 0; i < context._elifExpr.Count; i++)
                {
                    var expr  = context._elifExpr[i];
                    var block = context._elifSb[i];

                    EnterExpression(expr);
                    Code.Instructions.Add(OpCode.JMP_FALSE);
                    var elifEndPos = Code.Instructions.FillableInt();
                    Code.Instructions.Block(() =>
                    {
                        foreach (var stmt in block.statement())
                        {
                            EnterStatement(stmt);
                        }
                    });

                    Code.Instructions.Add(OpCode.JMP);
                    endPositions.Add(Code.Instructions.FillableInt());
                    Code.Instructions.FillInt(elifEndPos, Code.Instructions.Count);
                }
            }

            if (context._else.Count > 0)
            {
                Code.Instructions.Block(() =>
                {
                    foreach (var statement in context._else)
                    {
                        EnterStatement(statement);
                    }
                });
            }

            endPositions.ForEach(x => Code.Instructions.FillInt(x, Code.Instructions.Count));
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Exit a parse tree produced by <see cref="FlyParser.ifElse"/>.
 /// <para>The default implementation does nothing.</para>
 /// </summary>
 /// <param name="context">The parse tree.</param>
 public virtual void ExitIfElse([NotNull] FlyParser.IfElseContext context)
 {
 }