-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathparser.py
More file actions
464 lines (420 loc) · 18 KB
/
parser.py
File metadata and controls
464 lines (420 loc) · 18 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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
from rply import ParserGenerator
import sys
from vython.errors import error, Errors as errors
from vython.lang.binary_operators import (Sum, Sub, Mul, Div, Mod, Pow, DivEu)
from vython.lang.affection_operators import (SumAffector, SubAffector,
DivEuAffector, MulAffector,
DivAffector, ModAffector,
PowAffector)
from vython.lang.expressions import ExpressionBase, Nothing
from vython.lang.functions import Print, Input, Int, Str, Float, Type, Boolean, CanBe
from vython.lang.variables import Variable, Variables, AffectionVar, ListVar
from vython.lang.unique_operators import Increment, Decrement
from vython.lang.comparators import Egal, Less, LessOrEgal, More, MoreOrEgal
from vython.lang.conditions import (If, IfElse, Else, ElseIf, IfElseIf,
IfElseIfElse, ElseIfs)
from vython.lang.logic_operators import And, Or, Not
from vython.lang.statements import Statement, StatementList
from vython.lang.loops import Loop, While
from vython.lang.types import List, MemberType
class Parser:
def __init__(self, tokens):
self.pg = ParserGenerator(
tokens,
precedence=[('left', ['NEWLINE']), ('left', ['EGAL']),
('left', ['AND', 'OR', 'NOT']),
('left', ['IS', 'LESS', 'MORE', 'LESSE', 'MOREE']),
('left', ['SUMAFF', 'SUBAFF']),
('left', ['MULAFF', 'DIVAFF', 'DIVEUAFF', 'MODAFF']),
('left', ['POWAFF']), ('left', ['SUM', 'SUB']),
('left', ['MUL', 'DIV', 'DIVEU', 'MOD']),
('left', ['POW'])])
self.var = Variables()
def parse(self):
@self.pg.production('program : statementlist')
def program(p):
return p[0].eval()
@self.pg.production('statementlist : statementlist NEWLINE statement')
@self.pg.production(
'statementlist : statementlist NEWLINE loop_statement')
@self.pg.production(
'statementlist : statementlist NEWLINE if_statement')
def statementlistexp(p):
return StatementList(p[2], p[0])
@self.pg.production('statementlist : statement')
@self.pg.production('statementlist : loop_statement')
@self.pg.production('statementlist : if_statement')
@self.pg.production('statementlist : statementlist NEWLINE')
def statementlist(p):
if p[0].gettokentype() == 'statement':
return StatementList(p[0])
else:
return StatementList(None, p[0])
@self.pg.production(
'loop_statement : LOOP INTEGER OPEN_CRO NEWLINE statementlist NEWLINE CLOSE_CRO'
)
def loop(p):
return Loop(int(p[1].value), p[4])
@self.pg.production(
'loop_statement : LOOP INTEGER NEWLINE OPEN_CRO NEWLINE statementlist NEWLINE CLOSE_CRO'
)
def loop2(p):
return Loop(int(p[1].value), p[5])
@self.pg.production(
'loop_statement : WHILE expression OPEN_CRO NEWLINE statementlist NEWLINE CLOSE_CRO'
)
def whileexp(p):
return While(p[1], p[4])
@self.pg.production(
'loop_statement : WHILE expression NEWLINE OPEN_CRO NEWLINE statementlist NEWLINE '
'CLOSE_CRO')
def whileexp2(p):
return While(p[1], p[5])
@self.pg.production(
'if_statement : IF expression OPEN_CRO NEWLINE statementlist NEWLINE CLOSE_CRO'
)
def ifexp(p):
return If(p[1], p[4])
@self.pg.production(
'if_statement : IF expression NEWLINE OPEN_CRO NEWLINE statementlist NEWLINE CLOSE_CRO'
)
def ifexp2(p):
return If(p[1], p[5])
@self.pg.production(
'else_statement : ELSE OPEN_CRO NEWLINE statementlist NEWLINE CLOSE_CRO'
)
def elseexp(p):
return Else(p[3])
@self.pg.production(
'else_statement : ELSE NEWLINE OPEN_CRO NEWLINE statementlist NEWLINE CLOSE_CRO'
)
def elseexp2(p):
return Else(p[4])
@self.pg.production(
'elseif_statement : ELSEIF expression OPEN_CRO NEWLINE statementlist NEWLINE '
'CLOSE_CRO')
def elseif(p):
return ElseIfs(ElseIf(p[1], p[4]))
@self.pg.production(
'elseif_statement : ELSEIF expression NEWLINE OPEN_CRO NEWLINE statementlist NEWLINE '
'CLOSE_CRO')
def elseif2(p):
return ElseIfs(ElseIf(p[1], p[5]))
@self.pg.production(
'elseif_statement : elseif_statement elseif_statement')
def elseif3(p):
return p[0].add(p[1])
@self.pg.production('if_statement : if_statement else_statement')
def ifelse(p):
if type(p[0]) == IfElse:
error(errors.UNEXPECTEDSYNTAX, "Alone Else", {"type": ""})
sys.exit(1)
elif type(p[0]) == IfElseIf:
return IfElseIfElse(If(p[0].ifcondition, p[0].ifstatementlist),
p[0].elseifs, p[1])
else:
return IfElse(p[0], p[1])
@self.pg.production('if_statement : if_statement elseif_statement')
def ifelseif(p):
if type(p[0]) == IfElse:
error(errors.UNEXPECTEDSYNTAX, "Alone ElseIf", {"type": ""})
sys.exit(1)
return IfElseIf(p[0], p[1])
@self.pg.production('statement : expression COMMENT')
@self.pg.production('statement : expression')
def statement(p):
return Statement(p[0])
@self.pg.production('statement : COMMENT')
def statement(p):
return Nothing()
@self.pg.production('expression : IDENTIFIER EGAL expression')
def programvar(p):
if type(p[2]) == List:
error(errors.EXPECTEDSYNTAX, "Expected hook around List.", {
"type": "token",
"token": p[0]
})
sys.exit(1)
var = self.var.get(p[0].value)
if var is not None:
if type(var) == ListVar:
error(errors.INVALIDTYPE, "Cannot have basic type.", {
"type": "token",
"token": p[0]
})
sys.exit(1)
return AffectionVar(var, p[2])
else:
var = Variable(p[0].value, p[2])
self.var.add(var)
return var
@self.pg.production('expression : IDENTIFIER EGAL CRO_OPEN CRO_CLOSE')
def programvar2(p):
var = self.var.get(p[0].value)
if var is not None:
if type(var) == Variable:
error(errors.INVALIDTYPE, "Cannot have complex type.", {
"type": "token",
"token": p[0]
})
sys.exit(1)
return AffectionVar(var, List())
else:
var = ListVar(p[0].value, List())
self.var.add(var)
return var
@self.pg.production(
'expression : IDENTIFIER EGAL CRO_OPEN expression CRO_CLOSE')
def programvar3(p):
var = self.var.get(p[0].value)
if var is not None:
if type(var) == Variable:
error(errors.INVALIDTYPE, "Cannot have complex type.", {
"type": "token",
"token": p[0]
})
sys.exit(1)
return AffectionVar(var, List(p[3]))
else:
var = ListVar(p[0].value, List(p[3]))
self.var.add(var)
return var
@self.pg.production(
'expression : IDENTIFIER POINT IDENTIFIER OPEN_PAREN CLOSE_PAREN')
def membervar(p):
var = self.var.get(p[0].value)
if var is not None:
return MemberType(p[2].value, var)
else:
error(errors.NOTDECLARED, "Variable is not declared.", {
"type": "token",
"token": p[0]
})
sys.exit(1)
@self.pg.production(
'expression : IDENTIFIER POINT IDENTIFIER OPEN_PAREN expression CLOSE_PAREN'
)
def membervar2(p):
var = self.var.get(p[0].value)
if var is not None:
return MemberType(p[2].value, var, [p[4]])
else:
error(errors.NOTDECLARED, "Variable is not declared.", {
"type": "token",
"token": p[0]
})
sys.exit(1)
@self.pg.production('expression : expression VIRGULE expression')
def list(p):
return List(p[0], p[2])
@self.pg.production(
'expression : CANBE OPEN_PAREN expression VIRGULE STRING CLOSE_PAREN'
)
def programfunc2(p):
func = p[0]
exp = p[2]
if func.gettokentype() == 'CANBE':
return CanBe(exp, p[4].value[1:-1])
@self.pg.production(
'expression : INT OPEN_PAREN expression CLOSE_PAREN')
@self.pg.production(
'expression : FLOATF OPEN_PAREN expression CLOSE_PAREN')
@self.pg.production(
'expression : BOOL OPEN_PAREN expression CLOSE_PAREN')
@self.pg.production(
'expression : STR OPEN_PAREN expression CLOSE_PAREN')
@self.pg.production(
'expression : TYPE OPEN_PAREN expression CLOSE_PAREN')
@self.pg.production(
'expression : PRINT OPEN_PAREN expression CLOSE_PAREN')
@self.pg.production('expression : ENTER OPEN_PAREN STRING CLOSE_PAREN')
def programfunc1(p):
func = p[0]
exp = p[2]
if func.gettokentype() == "INT":
return Int(exp)
elif func.gettokentype() == "FLOATF":
return Float(exp)
elif func.gettokentype() == "BOOL":
return Boolean(exp)
elif func.gettokentype() == "STR":
return Str(exp)
elif func.gettokentype() == "TYPE":
return Type(exp)
elif func.gettokentype() == "PRINT":
return Print(exp)
else:
return Input(exp.value)
@self.pg.production('expression : EXIT OPEN_PAREN CLOSE_PAREN')
@self.pg.production('expression : ENTER OPEN_PAREN CLOSE_PAREN')
@self.pg.production('expression : PRINT OPEN_PAREN CLOSE_PAREN')
def programfunc0(p):
func = p[0]
if func.gettokentype() == "EXIT":
sys.exit(0)
elif func.gettokentype() == "ENTER":
return Input()
else:
return Print()
@self.pg.production('expression : OPEN_PAREN expression CLOSE_PAREN')
def expressionparen(p):
return p[1]
@self.pg.production('expression : IDENTIFIER INCREMENT')
@self.pg.production('expression : IDENTIFIER DECREMENT')
def uniqueop(p):
var = self.var.get(p[0].value)
if var is not None:
if p[1].gettokentype() == "INCREMENT":
return Increment(var)
else:
return Decrement(var)
else:
error(errors.NOTDECLARED, "Variable is not declared.", {
"type": "token",
"token": p[0]
})
sys.exit(1)
@self.pg.production('expression : IDENTIFIER SUMAFF expression')
@self.pg.production('expression : IDENTIFIER SUBAFF expression')
@self.pg.production('expression : IDENTIFIER MULAFF expression')
@self.pg.production('expression : IDENTIFIER DIVAFF expression')
@self.pg.production('expression : IDENTIFIER MODAFF expression')
@self.pg.production('expression : IDENTIFIER POWAFF expression')
@self.pg.production('expression : IDENTIFIER DIVEUAFF expression')
def affectionop(p):
var = self.var.get(p[0].value)
op = p[1]
if var is not None:
if op.gettokentype() == 'SUMAFF':
return SumAffector(var, p[2])
elif op.gettokentype() == 'SUBAFF':
return SubAffector(var, p[2])
elif op.gettokentype() == 'MULAFF':
return MulAffector(var, p[2])
elif op.gettokentype() == 'MODAFF':
return ModAffector(var, p[2])
elif op.gettokentype() == 'POWAFF':
return PowAffector(var, p[2])
elif op.gettokentype() == 'DIVEUAFF':
return DivEuAffector(var, p[2])
else:
return DivAffector(var, p[2])
else:
error(errors.NOTDECLARED, "Variable is not declared.", {
"type": "token",
"token": p[0]
})
sys.exit(1)
@self.pg.production('expression : expression SUM expression')
@self.pg.production('expression : expression SUB expression')
@self.pg.production('expression : expression MUL expression')
@self.pg.production('expression : expression DIV expression')
@self.pg.production('expression : expression MOD expression')
@self.pg.production('expression : expression POW expression')
@self.pg.production('expression : expression DIVEU expression')
def binaryop(p):
left = p[0]
right = p[2]
operator = p[1]
if operator.gettokentype() == 'SUM':
return Sum(left, right)
elif operator.gettokentype() == 'SUB':
return Sub(left, right)
elif operator.gettokentype() == 'MUL':
return Mul(left, right)
elif operator.gettokentype() == 'MOD':
return Mod(left, right)
elif operator.gettokentype() == 'POW':
return Pow(left, right)
elif operator.gettokentype() == 'DIVEU':
return DivEu(left, right)
else:
return Div(left, right)
@self.pg.production('expression : expression AND expression')
@self.pg.production('expression : expression OR expression')
def logicoperators2(p):
op = p[1]
e1 = p[0]
e2 = p[2]
if op.gettokentype() == "AND":
return And(e1, e2)
else:
return Or(e1, e2)
@self.pg.production('expression : NOT expression')
def logicoperator1(p):
return ExpressionBase(Not(p[1]).eval(), "boolean")
@self.pg.production('expression : expression IS expression')
@self.pg.production('expression : expression LESS expression')
@self.pg.production('expression : expression LESSE expression')
@self.pg.production('expression : expression MORE expression')
@self.pg.production('expression : expression MOREE expression')
def comparators(p):
c = p[1]
e1 = p[0]
e2 = p[2]
if c.gettokentype() == "IS":
return Egal(e1, e2)
elif c.gettokentype() == "LESS":
return Less(e1, e2)
elif c.gettokentype() == "MORE":
return More(e1, e2)
elif c.gettokentype() == "LESSE":
return LessOrEgal(e1, e2)
else:
return MoreOrEgal(e1, e2)
@self.pg.production('expression : SUB expression')
@self.pg.production('expression : SUM expression')
def uniqueop(p):
ope = p[0]
exp = p[1]
if ope.gettokentype() == 'SUM':
return Sum(ExpressionBase(0, "integer"), exp)
else:
return Sub(ExpressionBase(0, "integer"), exp)
@self.pg.production(
'expression : IDENTIFIER CRO_OPEN INTEGER CRO_CLOSE')
def expressionlist(p):
var = self.var.get(p[0].value)
if var is not None:
return var.get(int(p[2].value))
else:
error(errors.NOTDECLARED, "Variable is not declared.", {
"type": "token",
"token": p[0]
})
sys.exit(1)
@self.pg.production('expression : FLOAT')
@self.pg.production('expression : INTEGER')
@self.pg.production('expression : STRING')
@self.pg.production('expression : BOOLEAN')
@self.pg.production('expression : IDENTIFIER')
def expression(p):
if p[0].gettokentype() == 'FLOAT':
return ExpressionBase(float(p[0].value), "float")
elif p[0].gettokentype() == 'STRING':
return ExpressionBase(str(p[0].value)[1:-1], "string")
elif p[0].gettokentype() == 'BOOLEAN':
if p[0].value == "false":
return ExpressionBase(False, "boolean")
return ExpressionBase(True, "boolean")
elif p[0].gettokentype() == 'IDENTIFIER':
var = self.var.get(p[0].value)
if var is not None:
return ExpressionBase(var.value, var.kind, var)
else:
error(errors.NOTDECLARED, "Variable is not declared.", {
"type": "token",
"token": p[0]
})
sys.exit(1)
else:
return ExpressionBase(int(p[0].value), "integer")
@self.pg.error
def error_handle(token):
print("Syntax unexcepted : \n - Position :", token.getsourcepos(),
"\n - Token : Valeur =", token.getstr(), "| Type =",
token.gettokentype())
sys.exit(1)
def get_parser(self):
parser = self.pg.build()
return parser