-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
305 lines (260 loc) · 10.6 KB
/
Copy pathparser.py
File metadata and controls
305 lines (260 loc) · 10.6 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# code parser here
from error import WinkySyntaxError
from model import *
from tokens import *
class Parser:
def __init__(self , tokens):
self.tokens = tokens
self.curr = 0
def advance(self):
token = self.peek()
self.curr += 1
return token
def peek(self) -> Token:
return self.tokens[self.curr]
def isNext(self , expected_type : Token):
if self.next_token().token_type == expected_type:
return True
else: return False
def expect(self , expected_type):
if self.curr >= len(self.tokens):
WinkySyntaxError(f"Found {self.prev_token().lexeme!r} at the end of parsing." , self.prev_token().line)
if self.peek().token_type == expected_type:
token = self.advance()
return token
else: WinkySyntaxError(f"Expected {expected_type!r} but found {self.peek().lexeme!r}." , self.prev_token().line)
def match(self,expected):
if self.curr >= len(self.tokens):
return False
if self.peek().token_type != expected:
return False
self.curr += 1
return True
def prev_token(self) -> Token:
return self.tokens[self.curr -1]
def next_token(self):
if (self.curr +1) < len(self.tokens):
return self.tokens[self.curr +1]
else : WinkySyntaxError(f"Found {self.prev_token().lexeme!r} at the end of parsing." , self.prev_token().line)
# <primary> ::= <number> | <bool> | <string> | <identifier> | '('<expr>')' | <FuncCall>
# <number> ::= <digit>+
# <digit> ::= '0' | '1' | '2' | ... | '9'
# <bool> ::= 'true' | 'false'
def primary(self):
if self.match(TOK_INTEGER) : return Integer(int(self.prev_token().lexeme) , line=self.prev_token().line)
elif self.match(TOK_FLOAT) : return Float(float(self.prev_token().lexeme) , line=self.prev_token().line)
elif self.match(TOK_TRUE) : return Bool(True , line=self.prev_token().line)
elif self.match(TOK_FALSE) : return Bool(False , line=self.prev_token().line)
elif self.match(TOK_STRING) : return String(str(self.prev_token().lexeme[1:-1]) , line=self.prev_token().line)
elif self.match(TOK_LPAREN):
expr = self.OR()
if (not self.match(TOK_RPAREN)) : WinkySyntaxError("unexpected error : needed a ')'" , self.prev_token().line)
else : return Grouping(expr , line=self.prev_token().line)
else:
identifier = self.expect(TOK_IDENTIFIER)
if self.match(TOK_LPAREN):
args = self.args()
self.expect(TOK_RPAREN)
return FuncCall(identifier.lexeme,args , line=self.prev_token().line)
else:
return Identifier(identifier.lexeme , line=self.prev_token().line)
# <Expo> ::= <primary> ('^' <primary>)*
def Expo(self):
primary = self.primary()
while self.match(TOK_CARET):
op = self.prev_token()
# we want that the exponent operator be right associative so we call the rhs recursivly
right = self.Expo()
primary = BinOp(op , primary , right , line=self.prev_token().line)
return primary
# <unary> ::= ('+' | '-' | '~') <unary> | <Expo>
def unary(self):
if self.match(TOK_PLUS) or self.match(TOK_MINUS) or self.match(TOK_NOT):
op = self.prev_token()
right = self.unary()
return UnOp(op , right , line=self.prev_token().line)
return self.Expo()
# <Mod> ::= <Unary> ('%' <Unary>)*
def Mod(self):
unary = self.unary()
while self.match(TOK_MOD):
op = self.prev_token()
right = self.unary()
unary = BinOp(op , unary , right , line=self.prev_token().line)
return unary
# <term> ::= <Mod> (<mulop> <Mod>)*
# <mulop> ::= '*' | '/'
# term is about addition or subtraction
def term(self):
factor = self.Mod()
while self.match(TOK_STAR) or self.match(TOK_SLASH):
op = self.prev_token()
right = self.Mod()
factor = BinOp(op , factor , right , line=self.prev_token().line)
return factor
# <expr> ::= <term> (<addop> <term>)*
# <addop> ::= '+' | '-'
def expr(self):
term = self.term()
while self.match(TOK_PLUS) or self.match(TOK_MINUS):
op = self.prev_token()
right = self.term()
term = BinOp(op , term , right , line=self.prev_token().line)
return term
# <COMPA> ::= <expr> (('<' | '>' | '<=' | '>=') <expr>)*
def COMPA(self):
expr = self.expr()
while self.match(TOK_GT) or self.match(TOK_LT) or self.match(TOK_GE) or self.match(TOK_LE):
op = self.prev_token()
right = self.expr()
expr = BinOp(op , expr , right , line=self.prev_token().line)
return expr
# <EQUAL> ::= <COMPA> (('==' | '~=') <COMPA>)*
def EQUAL(self):
compa = self.COMPA()
while self.match(TOK_EQEQ) or self.match(TOK_NE):
op = self.prev_token()
righ = self.COMPA()
compa = BinOp(op , compa , righ , line=self.prev_token().line)
return compa
# <AND> ::= <EQUAL> ('and' <EQUAL>)*
def AND(self):
equal = self.EQUAL()
while self.match(TOK_AND):
op = self.prev_token()
right = self.EQUAL()
equal = LogicalOp(op , equal , right , line=self.prev_token().line)
return equal
# <OR> ::= <AND> ('or' <AND>)*
def OR(self):
And = self.AND()
while self.match(TOK_OR):
op = self.prev_token()
right = self.AND()
And = LogicalOp(op , And , right , line=self.prev_token().line)
return And
def print_stmt(self , end):
if self.match(TOK_PRINT) or self.match(TOK_PRINTLN):
val = self.OR()
return PrintStmt(val , line=self.prev_token().line ,end=end)
# <ifStmt> ::= "if" <test_expr> "then" <then_stmt> ("else" <else_stmt>)? "end"
def if_stmt(self):
self.expect(TOK_IF)
test_expr = self.OR()
self.expect(TOK_THEN)
then_stmt = self.stmts()
if self.peek().token_type == TOK_ELSE:
self.advance()
else_stmt = self.stmts()
else:
else_stmt = None
self.expect(TOK_END)
return IfStmt(test_expr , then_stmt , else_stmt , line=self.prev_token().line)
# <whileStmt> ::= "while" <text_expr> "do" <while_stmts> "end"
def while_stmt(self):
self.expect(TOK_WHILE)
test_expr = self.OR()
self.expect(TOK_DO)
while_stmts = self.stmts()
self.expect(TOK_END)
return WhileStmt(test_expr , while_stmts , line=self.prev_token().line)
# <forStmt> ::= "for" <assignment> , <expr> , (<expr>)? "do" <for_stmts> end
def for_stmt(self):
self.expect(TOK_FOR)
identifier = self.primary()
self.expect(TOK_ASSIGN)
start_exp = self.OR()
self.expect(TOK_COMMA)
end_expr = self.OR()
stepper_expr = None
if self.peek().token_type == TOK_COMMA:
self.expect(TOK_COMMA)
stepper_expr = self.OR()
self.expect(TOK_DO)
for_stmts = self.stmts()
return ForStmt(identifier, start_exp , end_expr , stepper_expr , for_stmts , line=self.prev_token().line)
def args(self):
args = []
while not self.peek().token_type == TOK_RPAREN:
arg = self.OR()
args.append(arg)
if not self.peek().token_type == TOK_RPAREN:
self.expect(TOK_COMMA)
return args
def params(self):
params = []
while not self.peek().token_type == TOK_RPAREN:
name = self.expect(TOK_IDENTIFIER)
params.append(Param(name.lexeme , line=self.prev_token().line))
if not self.peek().token_type == TOK_RPAREN:
self.expect(TOK_COMMA)
return params
#<func_decl> ::= "func" <name> "(" <params>? ")" <body_stmts> "end"
def func_decl(self):
self.expect(TOK_FUNC)
name = self.expect(TOK_IDENTIFIER)
self.expect(TOK_LPAREN)
params = self.params()
self.expect(TOK_RPAREN)
body_stmts = self.stmts()
self.expect(TOK_END)
return FuncDecl(name.lexeme , params , body_stmts , line=self.prev_token().line)
def local_assign(self):
self.expect(TOK_LOCAL)
left = self.OR()
self.expect(TOK_ASSIGN)
right = self.OR()
return LocalAssignment(left , right , line=self.prev_token().line)
def ret_stmt(self):
self.expect(TOK_RET)
expr = self.OR()
return RetStmt(expr , line=self.prev_token().line)
# <stmt> ::= print_stmt
# | if_stmt
# | while_stmt
# | for_stmt
# | func_decl
# | func_call
# | ret_stmt
# | local_assign
def stmt(self):
if self.peek().token_type == TOK_PRINT:
return self.print_stmt(end="")
elif self.peek().token_type == TOK_PRINTLN:
return self.print_stmt(end = "\n")
elif self.peek().token_type == TOK_IF:
return self.if_stmt()
elif self.peek().token_type == TOK_WHILE:
return self.while_stmt()
elif self.peek().token_type == TOK_FOR:
return self.for_stmt()
elif self.peek().token_type == TOK_FUNC:
return self.func_decl()
elif self.peek().token_type == TOK_RET:
return self.ret_stmt()
elif self.peek().token_type == TOK_LOCAL:
return self.local_assign()
else:
#handle identifier and function call here.
left = self.OR()
if self.match(TOK_ASSIGN):
right = self.OR()
return Assignment(left , right , line=self.prev_token().line)
else:
# handle functoin call statement here.
return FuncCallStmt(left)
def stmts(self):
'''
generate list of statements.
'''
stmts = []
while self.curr < len(self.tokens) and not self.peek().token_type == TOK_ELSE and not self.peek().token_type == TOK_END:
stmt = self.stmt()
stmts.append(stmt)
return Stmts(stmts , line=self.prev_token().line)
def program(self):
stmts = self.stmts()
return stmts
def parse(self):
program = self.program()
return program