-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparserClass.py
More file actions
375 lines (338 loc) · 13.6 KB
/
Copy pathparserClass.py
File metadata and controls
375 lines (338 loc) · 13.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
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
import astClass
class Parser:
def __init__(self):
pass
def expect(self, kind):
next = self.showNext()
if next.kind == kind:
self.acceptIt()
else:
print("EXPECT ERROR : syntaxe error line : " + str(self.showNext().position))
print("Expected : ", kind, " but have : ", next.kind)
exit()
return next.value
def showNext(self):
return self.tokens[0]
def acceptIt(self):
self.tokens = self.tokens[1:]
return self.tokens
def parse(self, tokens):
self.tokens = tokens
ast = self.parse_program()
return ast
def parse_program(self):
program = astClass.Program()
self.expect("SETUP")
self.expect("LBRACE")
program.setup = self.parse_setup()
self.expect("RBRACE")
self.expect("PROGRAM")
self.expect("LBRACE")
program.main = self.parse_main()
self.expect("RBRACE")
return program
def parse_setup(self):
setup = astClass.Setup()
while self.showNext().kind in ["PIN", "ADC", "SPI", "I2C", "SERIAL", "SERVO"]:
setup.instances.append(self.parse_instantiations())
return setup
def parse_main(self):
main = astClass.Main()
while self.showNext().kind in ["INT", "CHAR", "FLOAT"]:
main.dcls.append(self.parse_type_declarations())
self.expect("LOOP")
self.expect("LBRACE")
while self.showNext().kind in ["IDENTIFIER", "IF", "WHILE", "SET", "READ", "SERIAL", "I2C", "SPI", "SERVO"]:
main.body.append(self.parse_statements())
self.expect("RBRACE")
return main
def parse_statements(self):
CMP = self.showNext().kind
if CMP == "IDENTIFIER":
statement = self.parse_assignation()
elif CMP == "IF":
statement = self.parse_if()
elif CMP == "WHILE":
statement = self.parse_while()
elif CMP == "SET":
statement = self.parse_set()
elif CMP == "READ":
statement = self.parse_read()
elif CMP == "SERIAL":
statement = self.parse_serial()
elif CMP == "I2C":
statement = self.parse_i2c()
elif CMP == "SPI":
statement = self.parse_spi()
elif CMP == "SERVO":
statement = self.parse_servo()
return statement
def parse_spi(self):
spi = astClass.SendSpi()
self.expect("SPI")
self.expect("LPAREN")
spi.data = astClass.Ident(self.expect("IDENTIFIER"))
self.expect("TO")
spi.slave = astClass.Ident(self.expect("IDENTIFIER"))
self.expect("RPAREN")
return spi
def parse_i2c(self):
i2c = astClass.SendI2c()
self.expect("I2C")
self.expect("LPAREN")
i2c.data = astClass.Ident(self.expect("IDENTIFIER"))
self.expect("TO")
i2c.slave = astClass.Ident(self.expect("IDENTIFIER"))
self.expect("RPAREN")
return i2c
def parse_serial(self):
serial = astClass.SendSerial()
self.acceptIt()
self.expect("LPAREN")
serial.data = astClass.Ident(self.expect("IDENTIFIER"))
self.expect("RPAREN")
return serial
def parse_read(self):
read = astClass.Read()
self.acceptIt()
read.data = astClass.Ident(self.expect("IDENTIFIER"))
return read
def parse_set(self):
set_ = astClass.Set()
self.acceptIt()
set_.pin = astClass.Ident(self.expect("IDENTIFIER"))
self.expect("TO")
CMP = self.showNext().kind
if CMP in ["HIGH", "LOW"]:
self.acceptIt()
set_.level = CMP
return set_
def parse_servo(self):
servo = astClass.SendServo()
self.expect("SERVO")
self.expect("LPAREN")
servo.name = astClass.Ident(self.expect("IDENTIFIER"))
self.expect("AT")
CMP = self.showNext().kind
if CMP in ["INTEGER_LIT", "CHAR_LIT", "FLOAT_LIT", "IDENTIFIER"]:
if CMP == "IDENTIFIER":
servo.data = astClass.Ident(self.expect("IDENTIFIER"))
elif CMP == "INTEGER_LIT":
servo.data = astClass.IntLit(self.expect("INTEGER_LIT"))
self.expect("RPAREN")
return servo
def parse_block(self):
block = []
while self.showNext().kind in ["IDENTIFIER", "IF", "WHILE", "SET", "READ", "SERIAL", "I2C", "SPI", "SERVO"]:
block.append(self.parse_statements())
return block
def parse_if(self):
if_ = astClass.If()
self.acceptIt()
self.expect("LPAREN")
if_.cond = self.parse_binary()
self.expect("RPAREN")
self.expect("LBRACE")
while self.showNext().kind in ["IDENTIFIER", "IF", "WHILE", "SET", "READ", "SERIAL", "I2C", "SPI", "SERVO"]:
if_.body = self.parse_block()
self.expect("RBRACE")
if self.showNext().kind == "ELSE":
self.acceptIt()
self.expect("LBRACE")
while self.showNext().kind in ["IDENTIFIER", "IF", "WHILE", "SET", "READ", "SERIAL", "I2C", "SPI", "SERVO"]:
if_.else_ = self.parse_block()
self.expect("RBRACE")
return if_
def parse_while(self):
while_ = astClass.While()
self.acceptIt()
self.expect("LPAREN")
while_.cond = self.parse_binary()
self.expect("RPAREN")
self.expect("LBRACE")
while self.showNext().kind in ["IDENTIFIER", "IF", "WHILE", "SET", "READ", "SERIAL", "I2C", "SPI", "SERVO"]:
while_.body = self.parse_block()
self.expect("RBRACE")
return while_
def parse_assignation(self):
assign = astClass.Assign()
assign.lhs = astClass.Ident(self.expect("IDENTIFIER"))
assign.op = astClass.OpLit(self.expect("ASSIGN"))
assign.rhs = self.parse_binary()
return assign
def parse_binary(self):
binary = astClass.Binary()
CMP = self.showNext().kind
if CMP in ["INTEGER_LIT", "CHAR_LIT", "FLOAT_LIT", "IDENTIFIER"]:
if CMP == "IDENTIFIER":
binary.lhs = astClass.Ident(self.expect("IDENTIFIER"))
elif CMP == "CHAR_LIT":
binary.lhs = astClass.CharLit(self.expect("CHAR_LIT"))
elif CMP == "FLOAT_LIT":
binary.lhs = astClass.FloatLit(self.expect("FLOAT_LIT"))
elif CMP == "INTEGER_LIT":
binary.lhs = astClass.IntLit(self.expect("INTEGER_LIT"))
CMP = self.showNext()
if CMP.kind in ["ADD", "SUB", "MUL", "DIV", "LT", "LTE", "GT", "GTE"]:
binary.op = astClass.OpLit(CMP.value)
self.acceptIt()
CMP = self.showNext().kind
if CMP in ["INTEGER_LIT", "CHAR_LIT", "FLOAT_LIT", "IDENTIFIER"] and self.tokens[1].kind not in ["ADD", "SUB", "MUL", "DIV"]:
if CMP == "IDENTIFIER":
binary.rhs = astClass.Ident(self.expect("IDENTIFIER"))
elif CMP == "CHAR_LIT":
binary.rhs = astClass.CharLit(self.expect("CHAR_LIT"))
elif CMP == "FLOAT_LIT":
binary.rhs = astClass.FloatLit(self.expect("FLOAT_LIT"))
elif CMP == "INTEGER_LIT":
binary.rhs = astClass.IntLit(self.expect("INTEGER_LIT"))
else:
binary.rhs = self.parse_binary()
return binary
def parse_type_declarations(self):
CMP = self.showNext().kind
if CMP == "INT":
declaration = self.parse_int_declarations()
if CMP == "CHAR":
declaration = self.parse_char_declarations()
if CMP == "FLOAT":
declaration = self.parse_float_declarations()
return declaration
def parse_int_declarations(self):
var = astClass.Declaration()
var.type = astClass.Ident(self.expect("INT"))
var.name = astClass.Ident(self.expect("IDENTIFIER"))
self.expect("ASSIGN")
var.value = astClass.IntLit(self.expect("INTEGER_LIT"))
return var
def parse_char_declarations(self):
var = astClass.Declaration()
var.type = astClass.Ident(self.expect("CHAR"))
var.name = astClass.Ident(self.expect("IDENTIFIER"))
self.expect("ASSIGN")
var.value = astClass.CharLit(self.expect("CHAR_LIT"))
return var
def parse_float_declarations(self):
var = astClass.Declaration()
var.type = astClass.Ident(self.expect("FLOAT"))
var.name = astClass.Ident(self.expect("IDENTIFIER"))
self.expect("ASSIGN")
var.value = astClass.FloatLit(self.expect("FLOAT_LIT"))
return var
def parse_instantiations(self):
CMP = self.showNext().kind
if CMP == "PIN":
self.acceptIt()
instance = self.parse_pins_declaration()
if CMP == "ADC":
self.acceptIt()
instance = self.parse_adcs_declaration()
if CMP == "SPI":
self.acceptIt()
instance = self.parse_spis_declaration()
if CMP == "I2C":
self.acceptIt()
instance = self.parse_i2cs_declaration()
if CMP == "SERIAL":
self.acceptIt()
instance = self.parse_serial_declaration()
if CMP == "SERVO":
self.acceptIt()
instance = self.parse_servos_declaration()
return instance
def parse_pins_declaration(self):
pin = astClass.PinInstance()
self.expect("LBRACE")
while self.showNext().kind == "new":
pin.dcls.append(self.parse_pin_declaration())
self.expect("RBRACE")
return pin
def parse_adcs_declaration(self):
adc = astClass.AdcInstance()
self.expect("LBRACE")
while self.showNext().kind == "new":
adc.dcls.append(self.parse_adc_declaration())
self.expect("RBRACE")
return adc
def parse_spis_declaration(self):
spi = astClass.SpiInstance()
self.expect("LBRACE")
spi.name = astClass.Ident(self.expect("IDENTIFIER"))
self.expect("COLON")
spi.SCK = astClass.IntLit(self.expect("INTEGER_LIT"))
self.expect("COMMA")
spi.MISO = astClass.IntLit(self.expect("INTEGER_LIT"))
self.expect("COMMA")
spi.MOSI = astClass.IntLit(self.expect("INTEGER_LIT"))
while self.showNext().kind == "new":
spi.dcls.append(self.parse_spi_declaration())
self.expect("RBRACE")
return spi
def parse_i2cs_declaration(self):
i2cs = astClass.I2cInstance()
self.expect("LBRACE")
i2cs.name = astClass.Ident(self.expect("IDENTIFIER"))
self.expect("COLON")
i2cs.SCK = astClass.IntLit(self.expect("INTEGER_LIT"))
self.expect("COMMA")
i2cs.SCL = astClass.IntLit(self.expect("INTEGER_LIT"))
while self.showNext().kind == "new":
i2cs.dcls.append(self.parse_i2c_declaration())
self.expect("RBRACE")
return i2cs
def parse_servos_declaration(self):
servos = astClass.ServoInstance()
self.expect("LBRACE")
while self.showNext().kind == "new":
servos.dcls.append(self.parse_servo_declaration())
self.expect("RBRACE")
return servos
def parse_pin_declaration(self):
declaration = astClass.Pin()
self.expect("new")
declaration.name = astClass.Ident(self.expect("IDENTIFIER"))
self.expect("COLON")
declaration.nb = astClass.IntLit(self.expect("INTEGER_LIT"))
self.expect("COMMA")
if self.showNext().kind in ["OUTPUT", "INPUT"]:
declaration.level = astClass.Ident(self.showNext().kind)
self.acceptIt()
return declaration
def parse_adc_declaration(self):
declaration = astClass.Adc()
self.expect("new")
declaration.name = astClass.Ident(self.expect("IDENTIFIER"))
self.expect("COLON")
declaration.nb = astClass.AnalogLit(self.expect("ANALOG_LIT"))
self.expect("COMMA")
if self.showNext().kind in ["OUTPUT", "INPUT"]:
declaration.level = astClass.Ident(self.showNext().kind)
self.acceptIt()
return declaration
def parse_spi_declaration(self):
declaration = astClass.Componant()
self.expect("new")
declaration.name = astClass.Ident(self.expect("IDENTIFIER"))
self.expect("COLON")
declaration.nb = astClass.IntLit(self.expect("INTEGER_LIT"))
return declaration
def parse_i2c_declaration(self):
declaration = astClass.Componant()
self.expect("new")
declaration.name = astClass.Ident(self.expect("IDENTIFIER"))
self.expect("COLON")
declaration.nb = astClass.AdressLit(self.expect("ADRESS_LIT"))
return declaration
def parse_serial_declaration(self):
declaration = astClass.Serial()
self.expect("LBRACE")
declaration.bauds = astClass.AdressLit(self.expect("INTEGER_LIT"))
self.expect("RBRACE")
return declaration
def parse_servo_declaration(self):
declaration = astClass.Servo()
self.expect("new")
declaration.name = astClass.Ident(self.expect("IDENTIFIER"))
self.expect("COLON")
declaration.pin = astClass.IntLit(self.expect("INTEGER_LIT"))
return declaration