-
Notifications
You must be signed in to change notification settings - Fork 0
/
hcparse.py
executable file
·404 lines (297 loc) · 6.83 KB
/
hcparse.py
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
#!/usr/bin/env python3
from ply import yacc
from hcexceptions import HCParseError
from hclex import tokens
import hcast as ast
# Phase 1 parsing:
# Creates list of lines
def p_line_list(p):
"lines : line NL lines"
p[1].lineno = p.lineno(1)
p[0] = p[3]
p[0].insert(0, p[1])
def p_null_lines(p):
"lines : optws opt_comment"
p[0] = []
def p_empty_line(p):
"lines : optws opt_comment NL lines"
p[0] = p[4]
def p_opt_comment(p):
"""opt_comment : COMMENT
|"""
pass
def p_line(p):
"line : optws stmt"
p[0] = p[2]
p[0].indent = p[1]
def p_opt_ws(p):
"optws : WS"
p[0] = p[1]
def p_no_ws(p):
"optws :"
p[0] = ""
# Terminal Wrappers
def p_keyword_init(p):
"init : INIT optws"
pass
def p_keyword_forever(p):
"forever : FOREVER optws"
pass
def p_keyword_while(p):
"while : WHILE optws"
pass
def p_keyword_if(p):
"if : IF optws"
pass
def p_keyword_else(p):
"else : ELSE optws"
pass
def p_keyword_output(p):
"output : OUTPUT optws"
pass
def p_keyword_input(p):
"input : INPUT optws"
pass
def p_keyword_true(p):
"true : TRUE optws"
pass
def p_keyword_false(p):
"false : FALSE optws"
pass
# Operators
def p_operator_at(p):
"at : AT optws"
pass
def p_operator_equals(p):
"equals : EQUALS optws"
pass
def p_operator_add_equals(p):
"add_equals : ADD_EQUALS optws"
pass
def p_operator_sub_equals(p):
"sub_equals : SUB_EQUALS optws"
pass
def p_operator_mul_equals(p):
"mul_equals : MUL_EQUALS optws"
pass
def p_operator_add(p):
"add : ADD optws"
pass
def p_operator_increment(p):
"increment : DBL_ADD optws"
pass
def p_operator_subtract(p):
"subtract : SUBTRACT optws"
pass
def p_operator_decrement(p):
"decrement : DBL_SUB optws"
pass
def p_operator_multiply(p):
"multiply : MULTIPLY optws"
pass
def p_open_bracket(p):
"open_bracket : OPEN_BRACKET optws"
pass
def p_close_bracket(p):
"close_bracket : CLOSE_BRACKET optws"
pass
def p_operator_eq(p):
"cmp_eq : DBL_EQUALS optws"
pass
def p_operator_ne(p):
"cmp_ne : NOT_EQUALS optws"
pass
def p_operator_lt(p):
"cmp_lt : LESS_THAN optws"
pass
def p_operator_le(p):
"cmp_le : LESS_THAN_OR_EQUAL optws"
pass
def p_operator_gt(p):
"cmp_gt : GREATER_THAN optws"
pass
def p_operator_ge(p):
"cmp_ge : GREATER_THAN_OR_EQUAL optws"
pass
def p_operator_not(p):
"not : BANG optws"
pass
def p_operator_and(p):
"and : DBL_AND optws"
pass
def p_operator_or(p):
"or : DBL_OR optws"
pass
def p_identifier(p):
"name : IDENTIFIER optws"
p[0] = p[1]
def p_number(p):
"number : NUMBER optws"
p[0] = p[1]
# Statements
def p_declare_init(p):
"stmt : init name at number"
p[0] = ast.InitialValueDeclaration(p[4], name=p[2])
def p_declare_init_with_value(p):
"stmt : init name equals number at number"
p[0] = ast.InitialValueDeclaration(p[6], name=p[2], value=p[4])
def p_declare_init_without_name(p):
"stmt : init number at number"
p[0] = ast.InitialValueDeclaration(p[4], value=p[2])
def p_forever(p):
"stmt : forever"
p[0] = ast.Forever()
def p_while(p):
"stmt : while expr"
p[0] = ast.While(p[2])
def p_if(p):
"stmt : if expr"
p[0] = ast.If(p[2])
def p_else(p):
"stmt : else"
p[0] = ast.Else()
def p_output(p):
"stmt : output expr"
p[0] = ast.Output(p[2])
def p_expr_as_stmt(p):
"stmt : expr_assign"
p[0] = ast.ExprLine(p[1])
# Expressions - Assignments
def p_assign(p):
"expr_assign : l_expr equals expr_assign"
p[0] = ast.Assignment(p[1], p[3])
def p_add_assign(p):
"expr_assign : l_expr add_equals expr_assign"
p[0] = ast.Assignment(p[1], ast.Add(ast.VariableRef(p[1]), p[3]))
def p_sub_assign(p):
"expr_assign : l_expr sub_equals expr_assign"
p[0] = ast.Assignment(p[1], ast.Subtract(ast.VariableRef(p[1]), p[3]))
def p_mul_assign(p):
"expr_assign : l_expr mul_equals expr_assign"
p[0] = ast.Assignment(p[1], ast.Multiply(ast.VariableRef(p[1]), p[3]))
def p_no_assign(p):
"expr_assign : expr"
p[0] = p[1]
# Expressions - Logical Or
def p_or(p):
"expr : expr or expr_and"
p[0] = ast.LogicalOr(p[1], p[3])
def p_no_or(p):
"expr : expr_and"
p[0] = p[1]
# Expressions - Logical And
def p_and(p):
"expr_and : expr_and and expr_eq"
p[0] = ast.LogicalAnd(p[1], p[3])
def p_no_and(p):
"expr_and : expr_eq"
p[0] = p[1]
# Expressions - Equality Operators
def p_eq(p):
"expr_eq : expr_eq cmp_eq expr_ineq"
p[0] = ast.CompareEq(p[1], p[3])
def p_ne(p):
"expr_eq : expr_eq cmp_ne expr_ineq"
p[0] = ast.CompareNe(p[1], p[3])
def p_expr_ineq(p):
"expr_eq : expr_ineq"
p[0] = p[1]
# Expressions - Inequality Operators
def p_le(p):
"expr_ineq : expr_ineq cmp_le expr_s"
p[0] = ast.CompareLe(p[1], p[3])
def p_ge(p):
"expr_ineq : expr_ineq cmp_ge expr_s"
p[0] = ast.CompareGe(p[1], p[3])
def p_lt(p):
"expr_ineq : expr_ineq cmp_lt expr_s"
p[0] = ast.CompareLt(p[1], p[3])
def p_gt(p):
"expr_ineq : expr_ineq cmp_gt expr_s"
p[0] = ast.CompareGt(p[1], p[3])
def p_expr_s(p):
"expr_ineq : expr_s"
p[0] = p[1]
# Expressions - Additive Operators
def p_add(p):
"expr_s : expr_s add expr_m"
p[0] = ast.Add(p[1], p[3])
def p_sub(p):
"expr_s : expr_s subtract expr_m"
p[0] = ast.Subtract(p[1], p[3])
def p_expr_m(p):
"expr_s : expr_m"
p[0] = p[1]
# Expressions - Multiplication
def p_mul(p):
"expr_m : expr_m multiply expr_unary"
p[0] = ast.Multiply(p[1], p[3])
def p_expr_unary(p):
"expr_m : expr_unary"
p[0] = p[1]
# Expressions - Prefix Unary Operators
def p_unary_minus(p):
"expr_unary : subtract expr_unary"
p[0] = ast.Subtract(ast.Number(0), p[2])
def p_logical_not(p):
"expr_unary : not expr_unary"
p[0] = ast.LogicalNot(p[2])
def p_increment(p):
"expr_unary : increment l_expr"
p[0] = ast.Increment(p[2])
def p_decrement(p):
"expr_unary : decrement l_expr"
p[0] = ast.Decrement(p[2])
def p_expr_v(p):
"expr_unary : expr_v"
p[0] = p[1]
# Expressions - Single Values
def p_input(p):
"expr_v : input"
p[0] = ast.Input()
def p_num(p):
"expr_v : number"
p[0] = ast.Number(p[1])
def p_var(p):
"expr_v : name"
p[0] = ast.VariableRef(p[1])
def p_true(p):
"expr_v : true"
p[0] = ast.Boolean(True)
def p_false(p):
"expr_v : false"
p[0] = ast.Boolean(False)
def p_brackets(p):
"expr_v : open_bracket expr close_bracket"
p[0] = p[2]
# Left Expressions (may be assigned to)
def p_l_name(p):
"l_expr : name"
p[0] = p[1]
def p_l_brackets(p):
"l_expr : open_bracket l_expr close_bracket"
p[0] = p[2]
def p_error(p):
if not p:
raise HCParseError("Syntax error at eof")
raise HCParseError(f"Syntax error at {repr(p.value)} "
f"on line {p.lineno}, col {p.colno}")
def main():
import sys
import argparse
ap = argparse.ArgumentParser(description="Compile .hc files")
ap.add_argument("input", default=None)
args = ap.parse_args()
program = None
if args.input is None:
program = sys.stdin.read()
else:
with open(args.input) as f:
program = f.read()
result = parser.parse(program, tracking=True)
print(result)
if __name__ == "__main__":
parser = yacc.yacc()
main()
else:
parser = yacc.yacc(debug=False)