コード例 #1
0
        private void IfStatement()
        {
            Label labelElse = new Label(this);
            Label labelEnd = new Label(this);

            Stream.Expect(TokenType.Word, "if");
            Stream.Expect(TokenType.Delimiter, "(");

            TernaryExpression();

            Stream.Expect(TokenType.Delimiter, ")");

            Instructions.Add(new Instruction(Opcode.Push, new Variant(1)));
            Instructions.Add(new Instruction(Opcode.CompareEqual));
            Instructions.Add(new Instruction(Opcode.IfFalse, new Variant(0)));
            labelElse.PatchHere();

            BlockStatement();

            Instructions.Add(new Instruction(Opcode.Jump, new Variant(0)));
            labelEnd.PatchHere();

            labelElse.Mark();
            if (Stream.Accept(TokenType.Word, "else"))
                BlockStatement();

            labelEnd.Mark();

            labelElse.Fix();
            labelEnd.Fix();
        }
コード例 #2
0
        private void ForStatement()
        {
            Stream.Expect(TokenType.Word, "for");
            Stream.Expect(TokenType.Delimiter, "(");

            // TODO Fix this
            TernaryExpression();

            Stream.Expect(TokenType.Delimiter, ";");

            Label condition = new Label(this);
            TernaryExpression();
            Instructions.Add(new Instruction(Opcode.Push, new Variant(1)));
            Instructions.Add(new Instruction(Opcode.CompareEqual));
            Instructions.Add(new Instruction(Opcode.IfFalse, new Variant(0)));
            condition.PatchHere();
        }
コード例 #3
0
ファイル: Compiler.Expressions.cs プロジェクト: DatZach/Xi
        private void TernaryExpression()
        {
            LogicalAndOr();

            while (Stream.Accept(TokenType.Delimiter, "?"))
            {
                Label labelElse = new Label(this);
                Label labelEnd = new Label(this);

                Instructions.Add(new Instruction(Opcode.Push, new Variant(1)));
                Instructions.Add(new Instruction(Opcode.CompareEqual));
                Instructions.Add(new Instruction(Opcode.IfFalse, new Variant(0)));
                labelElse.PatchHere();

                LogicalAndOr();
                Instructions.Add(new Instruction(Opcode.Jump, new Variant(0)));
                labelEnd.PatchHere();

                Stream.Expect(TokenType.Delimiter, ":");

                labelElse.Mark();
                LogicalAndOr();
                labelEnd.Mark();

                labelElse.Fix();
                labelEnd.Fix();
            }
        }