Exemplo n.º 1
0
        private bool ParseIfStatement(TokenReader reader, out Statement statement)
        {
            Token start = reader.Peek();
            statement = null;

            if (!this.Expect(reader, Keyword.If))
            {
                return false;
            }

            IfStatement ifStatement = new IfStatement(start);
            Expression condition = null;
            if (!this.ParseExpression(reader, out condition))
            {
                return false;
            }

            ifStatement.Condition = condition;
            if (!this.Expect(reader, Keyword.Then))
            {
                return false;
            }

            Statement trueStatement = null;
            if (!this.ParseStatement(reader, out trueStatement))
            {
                return false;
            }

            ifStatement.TrueStatement = trueStatement;
            Token tok = reader.Peek();
            if (tok.Is(Keyword.Else))
            {
                reader.Read();
                Statement falseStatement = null;
                if (!this.ParseStatement(reader, out falseStatement))
                {
                    return false;
                }

                ifStatement.FalseStatement = falseStatement;
            }

            statement = ifStatement;
            return true;
        }
Exemplo n.º 2
0
        private bool TryEmitIfStatement(IfStatement ifStatement, CompilerContext context, Scope scope, MethodImpl method)
        {
            TypeDefinition conditionType = null;
            if (!this.TryEmitExpression(ifStatement.Condition, context, scope, method, out conditionType))
            {
                return false;
            }

            if (!this.ValidateConditionType(ifStatement.Condition, conditionType))
            {
                return false;
            }

            string elseLabel = null;
            if (ifStatement.FalseStatement != null)
            {
                elseLabel = method.Module.GetNextJumpLabel();
            }

            string endLabel = method.Module.GetNextJumpLabel();
            if (elseLabel == null)
            {
                elseLabel = endLabel;
            }

            method.Statements.Add(new AsmStatement { Instruction = "test al,al" });
            method.Statements.Add(new AsmStatement { Instruction = "jz " + elseLabel });
            if (!this.TryEmitStatement(ifStatement.TrueStatement, context, scope, method))
            {
                return false;
            }

            if (ifStatement.FalseStatement != null)
            {
                method.Statements.Add(new AsmStatement { Instruction = "jmp " + endLabel });
                method.Statements.Add(new AsmStatement { Instruction = "nop", Label = elseLabel });
                if (!this.TryEmitStatement(ifStatement.FalseStatement, context, scope, method))
                {
                    return false;
                }
            }

            method.Statements.Add(new AsmStatement { Instruction = "nop", Label = endLabel });
            return true;
        }