This repository was archived by the owner on Apr 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 391
Expand file tree
/
Copy pathparser.py
More file actions
412 lines (365 loc) · 15.3 KB
/
Copy pathparser.py
File metadata and controls
412 lines (365 loc) · 15.3 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# Parser for TrumpScript
# 1/16/2016
from ast import *
from trumpscript.constants import *
class Parser:
def __init__(self):
self._token_to_function_map = {
T_Word: self.handle_word,
T_Make: self.handle_make,
T_LBrace: self.handle_brace,
T_LParen: self.handle_paren,
T_If: self.handle_if,
T_Mod: self.handle_mod,
T_While: self.handle_while,
T_Print: self.handle_print,
T_True: self.handle_true,
T_False: self.handle_false,
T_Not: self.handle_not,
T_Quote: self.handle_quote,
T_Num: self.handle_num
}
def _get_value_from_word_token(self, tokens):
token = self.consume(tokens, T_Word)
return Name(id=token["value"], ctx=Load())
def parse(self, tokens) -> AST:
filtered = self.filter_tokens(tokens)
# Build the entirety of the Abstract Syntax tree
return self.handle_module(filtered)
@staticmethod
def _temporary_error(msg="error", error_value="error"):
print(msg)
# TODO: get real errors srsly
return error_value
@staticmethod
def filter_tokens(tokens) -> list:
tokens = list(tokens)
t = "type"
t_null = {t: -1, "value": "NAN", "line": "NAN"}
i = 0
variables = set()
token = t_null
tokens.append(t_null)
while i < len(tokens) - 1:
prev = token
token = tokens[i]
nxt = tokens[i + 1]
# Check if we need to drop a junk word
if token[t] == T_Word:
if token["value"] not in variables:
if prev[t] == T_Make:
variables.add(token["value"])
elif nxt[t] == T_Is:
variables.add(token["value"])
elif nxt[t] == T_Input:
variables.add(token["value"])
else:
tokens.pop(i)
i -= 1 # Just back that up a touch
# TODO: make this not necessary
if token[t] == T_Less or token[t] == T_Greater or token[t] == T_And or token[t] == T_Or:
tokens.pop(i)
i -= 1 # Just back that up a touch
i += 1
return tokens
@staticmethod
def peek(tokens):
return tokens[0]["type"]
@staticmethod
def on_line(tokens):
return tokens[0]["line"]
def consume(self, tokens, t_type) -> dict:
if self.peek(tokens) == t_type:
return tokens.pop(0)
else:
print("failed to consume " + str(t_type) + ", got " + str(self.peek(tokens)) + " instead on line " + str(self.on_line(tokens)) + ".")
# Mod
def handle_module(self, tokens) -> Module:
body_list = []
while len(tokens) > 1:
# print("module main loop")
state, tokens = self.handle_anything(tokens)
if isinstance(state, expr):
state = Expr(value=state)
body_list.append(state)
for statement in body_list:
# print(statement)
fix_missing_locations(statement)
return Module(body=body_list)
# Obnoxious coverage
def handle_anything(self, tokens):
start = self.peek(tokens)
try:
return self._token_to_function_map[start](tokens)
except KeyError:
tokens.pop(0)
return Pass(), tokens
# Stmt
def handle_brace(self, tokens) -> (stmt, list):
brace_contents = []
cur_token = tokens.pop(0)
level = -1
while cur_token["type"] != T_RBrace or level > 0:
# TODO edge case error
if cur_token["type"] == T_LBrace:
#we got the left brace of the actual structure
#the first time
#print("level up")
level += 1
if cur_token["type"] == T_RBrace:
#print("level down")
level -= 1
brace_contents.append(cur_token)
cur_token = tokens.pop(0)
brace_contents.append(cur_token)
self.consume(brace_contents, T_LBrace)
statements = []
while self.peek(brace_contents) != T_RBrace:
if self.peek(brace_contents) == T_End:
# TODO: real error functionality please
print("Called handle_brace on a non-terminated brace")
res, contents = self.handle_anything(brace_contents)
if isinstance(res, expr):
res = Expr(value=res) #TODO: does it make sense to cast expressions to statements here?
statements.append(res)
return statements, tokens
# Expr
def handle_paren(self, tokens) -> (expr, list):
paren_contents = []
cur_token = tokens.pop(0)
while cur_token["type"] != T_RParen :
paren_contents.append(cur_token)
cur_token = tokens.pop(0)
paren_contents.append(cur_token)
self.consume(paren_contents, T_LParen)
expression, contents = self.handle_anything(paren_contents)
if self.peek(contents) != T_RParen :
#TODO: real erros
print("passed in parenthetical with more than one expression")
self.consume(contents, T_RParen)
return expression, tokens
def handle_mod(self,tokens) -> (expr, list):
valid_tokens = [T_LParen, T_Num]
self.consume(tokens, T_Mod)
followup = self.peek(tokens)
if followup == T_Word:
right = self._get_value_from_word_token(tokens)
ar = Call(func=Name(id="float", ctx=Load()), args=[right], keywords=[])
return Call(func=Name(id="int", ctx=Load()), args=[ar], keywords=[]), tokens
elif followup in valid_tokens:
right, tokens = self._token_to_function_map[followup](tokens)
ar = Call(func=Name(id="float", ctx=Load()), args=[right], keywords=[])
return Call(func=Name(id="int", ctx=Load()), args=[ar], keywords=[]), tokens
#return Num(right['value']), tokens
# Assign
def handle_make(self, tokens) -> (stmt, list):
valid_tokens = [T_LParen, T_True, T_False, T_Not, T_Quote, T_Num, T_Mod]
self.consume(tokens, T_Make)
if self.peek(tokens) != T_Word :
#TODO: write this fucking error
print("Ooh, what you making there? It sure isn't a variable, that's for goddamn sure!")
variable = self.consume(tokens, T_Word)
followup = self.peek(tokens) # Check the type of the next token to see if it's acceptable
if followup == T_Word:
val = self._get_value_from_word_token(tokens)
elif followup in valid_tokens:
val, tokens = self._token_to_function_map[followup](tokens)
else:
val = self._temporary_error(msg="make_error")
target = Name(id=variable["value"], ctx=Store())
return Assign(targets=[target], value=val), tokens
# Both Assign and EQ because why the hell not guys
# Note that this does not have type signature because it can be expr or stmt (yeah it blows))
def handle_is(self, left, tokens):
valid_tokens = [T_LParen, T_True, T_False, T_Not, T_Quote, T_Num, T_Mod]
self.consume(tokens, T_Is)
followup = self.peek(tokens) # Check the type of the next token to see if it's acceptable
if followup == T_Word:
right = self._get_value_from_word_token(tokens)
elif followup in valid_tokens:
right, tokens = self._token_to_function_map[followup](tokens)
else:
right = self._temporary_error(msg="is_error")
if self.peek(tokens) == T_Question:
self.consume(tokens, T_Question)
first = None
if left['type'] == T_Num:
first = Num(left['value'])
elif left['type'] == T_Quote:
first = Str(left['value'])
else:
first = Name(id=left["value"], ctx=Load())
return Compare(left=first, ops=[Eq()], comparators=[right]), tokens
else:
if left["type"] == T_Word:
target = Name(id=left["value"], ctx=Store())
return Assign(targets=[target], value=right), tokens
else:
target = self._temporary_error(msg="is_error")
return Pass(), tokens
def handle_ineq(self, left, tokens):
valid_tokens = [T_LParen, T_True, T_False, T_Quote, T_Num]
token_to_argument_map = {T_Less: Lt, T_Greater: Gt}
cmpop = None
op = self.peek(tokens)
try:
cmpop = token_to_argument_map[op]()
self.consume(tokens, op)
except KeyError:
cmpop = self._temporary_error(msg='ineq_error')
followup = self.peek(tokens)
if followup == T_Word:
right = self._get_value_from_word_token(tokens)
elif followup in valid_tokens:
right, tokens = self._token_to_function_map[followup](tokens)
else:
right = self._temporary_error(msg="ineq_error")
first = None
if left['type'] == T_Num:
first = Num(left['value'])
elif left['type'] == T_Quote:
first = Str(left['value'])
else:
first = Name(id=left["value"], ctx=Load())
return Compare(left=first, ops=[cmpop], comparators=[right]), tokens
# Print
def handle_print(self, tokens) -> (stmt, list):
valid_tokens = [T_Quote, T_LParen, T_Num, T_True, T_False, T_Word, T_Mod, T_Not]
self.consume(tokens, T_Print)
followup = self.peek(tokens)
if followup in valid_tokens:
output, tokens = self._token_to_function_map[followup](tokens)
else:
print(followup)
output = self._temporary_error(msg="print_error")
return Call(func=Name(id="print", ctx=Load()), args=[output], keywords=[]), tokens
def handle_input(self, left, tokens) -> (stmt, list):
valid_tokens = [T_Word]
self.consume(tokens, T_Input)
if left['type'] == T_Word:
target = Name(id=left['value'], ctx=Store())
fu = Call(func=Name(id="input", ctx=Load()), args=[], keywords=[])
return Assign([target], fu), tokens
# While
def handle_while(self, tokens) -> (stmt, list):
self.consume(tokens, T_While)
conditional, tokens = self.handle_paren(tokens)
body, tokens = self.handle_brace(tokens)
return While(test=conditional,body=body, orelse=[]), tokens
# If
def handle_if(self, tokens) -> (stmt, list):
self.consume(tokens, T_If)
conditional, tokens = self.handle_paren(tokens)
body, tokens = self.handle_brace(tokens)
if len(tokens) > 0 and self.peek(tokens) == T_Else:
orelse, tokens = self.handle_else(tokens)
else:
orelse = []
return If(test=conditional,body=body, orelse=orelse), tokens
# orelse piece of if
def handle_else(self, tokens) -> (stmt, list):
self.consume(tokens, T_Else)
if self.peek(tokens) == T_If:
return self.handle_if(tokens)
else:
return self.handle_brace(tokens)
# BinOp(s)
# TODO: what the fuck is going on here? I wrote this at 9am after nigh 24 hours of wakefulness
def handle_binop(self, left, op, tokens):
valid_tokens = [T_LParen, T_Quote, T_Num, T_True, T_False]
tokens.pop(0)
nxt = self.peek(tokens)
if nxt == T_Word:
right = self._get_value_from_word_token(tokens)
elif nxt in valid_tokens:
right, tokens = self._token_to_function_map[nxt](tokens)
else:
right = self._temporary_error(msg="binop_error")
return BinOp(left=left, op=op, right=right), tokens
# Not
def handle_not(self, tokens):
valid_tokens = [T_LParen, T_Word, T_False, T_True]
self.consume(tokens, T_Not)
nxt = self.peek(tokens)
if nxt in valid_tokens:
result, tokens = self._token_to_function_map[nxt](tokens)
else:
result = self._temporary_error(msg="not_error")
return UnaryOp(op=Not(),operand=result), tokens
# Num
def handle_num(self, tokens):
token_to_argument_map = {T_Plus: Add, T_Minus: Sub, T_Times: Mult, T_Over: Div}
token = self.consume(tokens, T_Num)
nxt = self.peek(tokens)
if nxt == T_Is:
return self.handle_is(token, tokens)
if nxt == T_Less or nxt == T_Greater:
return self.handle_ineq(token, tokens)
else:
num = Num(token["value"])
if nxt in token_to_argument_map:
return self.handle_binop(num, token_to_argument_map[nxt](), tokens)
else:
return num, tokens
# TODO: check for a longer expression
# Str
def handle_quote(self, tokens):
token = self.consume(tokens, T_Quote)
text = token["value"]
# TODO: check for a longer expression
nxt = self.peek(tokens)
if nxt == T_Is:
return self.handle_is(token, tokens)
elif nxt == T_Less or nxt == T_Greater:
return self.handle_ineq(token, tokens)
else:
return Str(text), tokens
def handle_boolop(self, left, op, tokens):
valid_tokens = [T_LParen, T_True, T_False]
tokens.pop(0)
nxt = self.peek(tokens)
if nxt == T_Word:
right = self._get_value_from_word_token(tokens)
elif nxt in valid_tokens:
right, tokens = self._token_to_function_map[nxt](tokens)
else:
right = self._temporary_error(msg="binop_error")
return BoolOp(op=op(), values=[left,right]), tokens
# Name
def handle_word(self, tokens):
token_to_argument_map = {T_Plus: Add, T_Minus: Sub, T_Times: Mult, T_Over: Div}
token = self.consume(tokens, T_Word)
nxt = self.peek(tokens)
if nxt == T_Is:
return self.handle_is(token, tokens)
elif nxt == T_Less or nxt == T_Greater:
return self.handle_ineq(token, tokens)
elif nxt == T_Or or nxt == T_And:
word_var = Name(id=token["value"], ctx=Load())
return self.handle_boolop(word_var,Or if nxt == T_Or else And, tokens)
elif nxt == T_Input:
return self.handle_input(token, tokens)
else:
word_var = Name(id=token["value"], ctx=Load())
if nxt in token_to_argument_map:
return self.handle_binop(word_var, token_to_argument_map[nxt](), tokens)
else:
return word_var, tokens
# True
def handle_true(self, tokens):
token = self.consume(tokens, T_True)
nxt = self.peek(tokens)
if nxt == T_Or or nxt == T_And:
print("boolop")
word_var = NameConstant(value=True)
return self.handle_boolop(word_var, Or if nxt == T_Or else And, tokens)
return NameConstant(value=True), tokens
# False
def handle_false(self, tokens):
token = self.consume(tokens, T_False)
nxt = self.peek(tokens)
if nxt == T_Or or nxt == T_And:
print("boolop")
word_var = NameConstant(value=False)
return self.handle_boolop(word_var, Or if nxt == T_Or else And, tokens)
return NameConstant(value=False), tokens