-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathparser.py
More file actions
231 lines (173 loc) · 7.1 KB
/
parser.py
File metadata and controls
231 lines (173 loc) · 7.1 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
from language.constants import TokenType, COMPARISON_TOKENS
from language.emitter import Emitter
from language.errors.parseing_error import ExpectedTokenError, InvalidTokenError, UndefinedVariableError
from language.lexer import Lexer, Token
class Variable:
def __init__(self, name, value):
self.name = name
self.value = value
@property
def type(self):
return type(self.value)
class Parser:
def __init__(self, lexer: Lexer, emitter: Emitter):
self.lexer: Lexer = lexer
self.emitter: Emitter = emitter
self.cur_token: Token = lexer.get_token()
self.peek_token: Token = lexer.get_token()
self.symbols = set()
def check_token(self, kind):
return kind == self.cur_token.kind
# Return true if the next token matches.
def check_peek(self, kind):
return kind == self.peek_token.kind
def match(self, kind):
if not self.check_token(kind):
raise ExpectedTokenError(kind, self.cur_token)
self.next_token()
def next_token(self):
self.cur_token = self.peek_token
self.peek_token = self.lexer.get_token()
def nl(self, throw=True):
if throw:
self.match(TokenType.NEWLINE)
while self.check_token(TokenType.NEWLINE):
self.next_token()
def primary(self):
if self.check_token(TokenType.NUMBER):
self.emitter.emit(self.cur_token.text)
self.next_token()
elif self.check_token(TokenType.IDENT):
if self.cur_token.text not in self.symbols:
raise UndefinedVariableError(self.cur_token.text)
self.emitter.emit(self.cur_token.text)
self.next_token()
else:
raise InvalidTokenError(self.cur_token)
def unary(self):
if self.check_token(TokenType.PLUS) or self.check_token(TokenType.MINUS):
self.emitter.emit(self.cur_token.text)
self.next_token()
self.primary()
def term(self):
if self.check_peek(TokenType.MODULO):
self.emitter.emit("(int)")
self.unary()
while self.check_token(TokenType.ASTERISK) or self.check_token(TokenType.SLASH) or \
self.check_token(TokenType.MODULO) or self.check_token(TokenType.EXP):
self.emitter.emit(self.cur_token.text)
if self.check_token(TokenType.EXP):
self.emitter.emit("**") # Use the ** operator for exponentiation
else:
self.emitter.emit(self.cur_token.text)
self.next_token()
if self.check_token(TokenType.MODULO):
self.emitter.emit("(int)")
self.unary()
if self.check_token(TokenType.EXP):
self.emitter.emit(")")
def expression(self):
self.term()
while self.check_token(TokenType.PLUS) or self.check_token(TokenType.MINUS):
self.emitter.emit(self.cur_token.text)
self.next_token()
self.term()
def comparison(self):
self.expression()
# Must be at least one comparison operator and another expression.
if self.cur_token.kind in COMPARISON_TOKENS:
self.emitter.emit(self.cur_token.text)
self.next_token()
self.expression()
else:
raise ExpectedTokenError("comparison operator", self.cur_token)
# Can have 0 or more comparison operator and expressions.
while self.cur_token.kind in COMPARISON_TOKENS:
self.emitter.emit(self.cur_token.text)
self.next_token()
self.expression()
def control_statement(self, keyword):
self.next_token()
self.emitter.emit(f"{keyword}(")
self.comparison()
self.nl(throw=False)
self.match(TokenType.LBRACE)
self.emitter.emit_line("){")
self.nl(throw=False)
# Zero or more statements in the loop body.
while not self.check_token(TokenType.RBRACE):
self.statement()
self.match(TokenType.RBRACE)
self.emitter.emit_line("}")
def statement(self):
if self.check_token(TokenType.PRINT):
self.next_token()
self.emitter.emit(f"std::cout")
while not self.check_token(TokenType.NEWLINE):
if self.check_token(TokenType.STRING):
self.emitter.emit(f"<<\"{self.cur_token.text}\"")
self.next_token()
else:
self.emitter.emit("<<")
self.expression()
self.emitter.emit_line(";")
elif self.check_token(TokenType.IF):
self.control_statement("if")
self.nl(throw=False)
if self.check_token(TokenType.ELSE):
self.next_token()
self.emitter.emit("else ")
if self.check_token(TokenType.IF):
return self.statement()
else:
self.nl(throw=False)
self.match(TokenType.LBRACE)
self.emitter.emit_line("{")
self.nl(throw=False)
while not self.check_token(TokenType.RBRACE):
self.statement()
self.match(TokenType.RBRACE)
self.emitter.emit_line("}")
else:
return
elif self.check_token(TokenType.WHILE):
self.control_statement("while")
elif self.check_token(TokenType.LET):
self.next_token()
if self.cur_token.text not in self.symbols:
self.symbols.add(self.cur_token.text)
self.emitter.header_line("float " + self.cur_token.text + ";")
self.emitter.emit(self.cur_token.text + " = ")
self.match(TokenType.IDENT)
self.match(TokenType.EQ)
self.expression()
self.emitter.emit_line(";")
elif self.check_token(TokenType.INPUT):
self.next_token()
self.emitter.emit("std::cin")
while not self.check_token(TokenType.NEWLINE):
if self.cur_token.text not in self.symbols:
self.symbols.add(self.cur_token.text)
self.emitter.header_line("float " + self.cur_token.text + ";")
self.emitter.emit(f">>{self.cur_token.text}")
self.match(TokenType.IDENT)
self.emitter.emit_line(";")
elif self.check_token(TokenType.RETURN):
self.next_token()
self.emitter.emit("return ")
self.expression()
self.emitter.emit_line(";")
else:
raise InvalidTokenError(self.cur_token)
# Newline.
self.nl()
def program(self):
self.emitter.header_line("#include <iostream>")
self.emitter.header_line("int main(int argc, char *argv[]){")
while self.check_token(TokenType.NEWLINE):
self.next_token()
while not self.check_token(TokenType.EOF):
self.statement()
self.emitter.emit_line("return 0;")
self.emitter.emit_line("}")
self.emitter.write_file()