Exemplo n.º 1
0
        private Node VariableDeclaration()
        {
            bool local = false;
            if (LookAhead(1) == TokenType.VAR)
            {
                Match(TokenType.VAR);
                local = true;
            }

            Node result = Variable();

            if (LookAhead(1) == TokenType.ASSIGN)
            {
                SourcePosition pos = Match(TokenType.ASSIGN).Position;
                Node value = Expression();
                result = new AssignNode(pos, result, value, local);
            }

            if (LookAhead(1) == TokenType.END_STATEMENT)
            {
                Match(TokenType.END_STATEMENT);
            }

            return result;
        }
Exemplo n.º 2
0
        private Node SumExpression()
        {
            // term ((PLUS^|MINUS^) term)*
            Node termExpression = Term();
            TokenType next = LookAhead(1);
            if (LookAhead(2) != TokenType.STRING_LITERAL)
            {
                while (next == TokenType.PLUS ||
                        next == TokenType.PLUS_PLUS ||
                        next == TokenType.PLUS_ASSIGN ||
                        next == TokenType.MINUS ||
                        next == TokenType.MINUS_MINUS ||
                        next == TokenType.MINUS_ASSIGN)
                {
                    if (next == TokenType.PLUS)
                    {
                        termExpression = new AddOpNode(Match(TokenType.PLUS).Position, termExpression, Term());
                    }
                    else if (next == TokenType.PLUS_PLUS)
                    {
                        Node increment = new IntegerNode(Match(TokenType.PLUS_PLUS).Position, 1);
                        Node addNode = new AddOpNode(increment.Position, termExpression, increment);
                        termExpression = new AssignNode(termExpression.Position, termExpression, addNode, false);
                    }
                    else if (next == TokenType.PLUS_ASSIGN)
                    {
                        Token plusAssign = Match(TokenType.PLUS_ASSIGN);
                        Node increment = Expression();
                        Node addNode = new AddOpNode(plusAssign.Position, termExpression, increment);
                        termExpression =
                            new AssignNode(termExpression.Position, termExpression, addNode, false);

                    }
                    else if (next == TokenType.MINUS)
                    {
                        termExpression =
                            new SubtractOpNode(Match(TokenType.MINUS).Position, termExpression, Term());
                    }
                    else if (next == TokenType.MINUS_MINUS)
                    {
                        Node deincrement = new IntegerNode(Match(TokenType.MINUS_MINUS).Position, 1);
                        Node subtractNode = new SubtractOpNode(deincrement.Position, termExpression, deincrement);
                        termExpression =
                            new AssignNode(termExpression.Position, termExpression, subtractNode, false);
                    }
                    else if (next == TokenType.MINUS_ASSIGN)
                    {
                        Token minusAssign = Match(TokenType.MINUS_ASSIGN);
                        Node deincrement = Expression();
                        Node subtractNode = new SubtractOpNode(minusAssign.Position, termExpression, deincrement);
                        termExpression =
                            new AssignNode(termExpression.Position, termExpression, subtractNode, false);
                    }

                    next = LookAhead(1);
                }
            }
            return termExpression;
        }