summaryrefslogtreecommitdiffstats
path: root/code/Interpreter/Parser.java
blob: 6b552996c1ab9ab1c486c0b373f05877b267ea70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package Interpreter;

import java.util.ArrayList;
import java.util.List;

public class Parser {
    private final List<Token> tokens;
    private int currentToken = 0;

    Parser(List<Token> tokens){
        this.tokens=tokens;
    }

    List<Statement> parse(){
        List<Statement> statements =  new ArrayList<>();
        while (!checkEOF()){
            statements.add(declaration());
        }
        return statements;
        
    }

    private Statement declaration(){
        try{
            if (matchAndAdvance(TokenType.VAR)){
                if (matchOrError(TokenType.DEFINE, ":: Required for variable definition")){
                    if (matchOrError(TokenType.IDENTIFIER,"Expected variable name.")){
                        Token varName = getPreviousToken();
                        return new Statement.VariableDeclaration(varName);
                    } 
                }
            }
            return statement();
        } catch (Error e){
            currentToken++;
            return null;
        }
    }

    private Statement statement(){
        if (matchAndAdvance(TokenType.PRINT)){
            Expression expression = expression();
            return new Statement.PrintStatement(expression);
        }
        return expressionStatement();
    }



    private Statement expressionStatement(){
        Expression expression = assignment();
        return new Statement.ExpressionStatement(expression);
    }

    private Expression assignment(){
        Expression variable = expression();
        if (matchAndAdvance(TokenType.EQUALS)){
            Expression assignedvalue = expression();

            if (variable instanceof Expression.Variable){
                return new Expression.AssignmentExpression(((Expression.Variable)variable).name,assignedvalue);
            }
            throw error("Incorrect assignment operation");
        }
        return variable;
    }

    private Expression expression(){
        Expression createdExpression = equality();
        return createdExpression;
    }

    private Expression equality(){
        Expression createdExpression = comparison();
        while (matchAndAdvance(TokenType.EQUALITY)){
            Token op = getPreviousToken();
            Expression right = comparison();
            createdExpression = new Expression.Binary(createdExpression, op, right);
        }
        return createdExpression;
    }

    private Expression comparison(){
        Expression createdExpression = term();
        while (matchAndAdvance(TokenType.GREATER)||matchAndAdvance(TokenType.LESS)){
            Token op = getPreviousToken();
            Expression right = term();
            createdExpression = new Expression.Binary(createdExpression, op, right);
        }
        return createdExpression;
    }

    private Expression term(){
        Expression createdExpression = factor();
        while (matchAndAdvance(TokenType.PLUS)||matchAndAdvance(TokenType.MINUS)){
            Token op = getPreviousToken();
            Expression right = factor();
            createdExpression = new Expression.Binary(createdExpression, op, right);
        }
        return createdExpression;
    }

    private Expression factor(){
        Expression createdExpression = primary();
        while (matchAndAdvance(TokenType.STAR)||matchAndAdvance(TokenType.SLASH)){
            Token op = getPreviousToken();
            Expression right = primary();
            createdExpression = new Expression.Binary(createdExpression, op, right);
        }
        return createdExpression;
    }

    private Expression primary(){
        if (matchAndAdvance(TokenType.NUMBER)){
            return new Expression.Literal(getPreviousToken());
        }

        if (matchAndAdvance(TokenType.IDENTIFIER)) {

            return new Expression.Variable(getPreviousToken());
          }

        if (matchAndAdvance(TokenType.LEFT_PAREN)){
            Expression expr = expression();
            if (matchAndAdvance(TokenType.RIGHT_PAREN)){
                return new Expression.BracketedExpression(expr);
            }
            else{
                throw error("Expected ')");
            }
        }
        throw error("Expected Expression");
    }

    private void advanceToken(){
        if (!checkEOF()) {
            currentToken++;
        };
    }

    private boolean matchAndAdvance(TokenType type){
        if (checkToken(type)) {
            advanceToken();
            return true;
        }
        return false;
    }

    private boolean matchOrError(TokenType type,String errorMessage){
        if (matchAndAdvance(type)){
            return true;
        }
        throw error(errorMessage);
    }

    private boolean checkToken(TokenType type){
        if (checkEOF()) return false;
        return getCurrentToken().type == type; 
    }

    private boolean checkEOF(){
        return tokens.get(currentToken).type==TokenType.EOF;
    }

    private Token getCurrentToken(){
        return tokens.get(currentToken);
    }

    private Token getPreviousToken(){
        return tokens.get(currentToken - 1);
    }

    private Error error(String message){
        Language.displayError(message);
        return new Error();
    }


}