예제 #1
0
        //
        // ifStatement: 'if' '(' expression ')' '{' statements '}' ('else' '{' statements '}' )?
        public void CompileIfStatement(int depth)
        {
            string startLabel = "LIF_" + IfLabelCounter;
            string endLabel   = "LIF_" + IfLabelCounter;

            // compile: 'if'
            var ifToken = Eat("if");

            // compile: '('
            var leftParenToken = Eat("(");

            // compile: expression
            CompileExpression(depth + 1, AssignmentType.NONE);

            // write: not , if-goto startLabel
            _vmWriter.WriteUnaryOp(new Token {
                Value = "~"
            });
            _vmWriter.WriteIfGoto(startLabel);

            // compile: ')'
            var rightParenToken = Eat(")");

            // compile: '{'
            var leftBraceToken = Eat("{");

            // compile: statements
            CompileStatements(depth + 1);

            // write: goto endLabel
            _vmWriter.WriteGoto(endLabel);

            // write: label startLabel
            _vmWriter.WriteLabel(startLabel);

            // compile: '}'
            var rightBraceToken = Eat("}");

            // compile: ('else' '{' statements '}' )?
            if (_tokenizer.CurrentToken.Value == "else")
            {
                // compile: 'else'
                var elseToken = Eat("else");

                // compile: '{'
                leftBraceToken = Eat("{");

                // compile: statements
                CompileStatements(depth + 1);

                // compile: '}'
                rightBraceToken = Eat("}");
            }

            // write: endLabel
            _vmWriter.WriteLabel(endLabel);
        }