-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompile.py
executable file
·368 lines (267 loc) · 7.99 KB
/
compile.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
#!/usr/bin/env python
from pathlib import Path
from nodes import *
from ply.lex import TOKEN
import sys
import assemble
import ply.lex as lex
import ply.yacc as yacc
"""
This module scans and parses cbc scripts
"""
"""
Lex definitions (scanner)
"""
tokens = (
'WHILE', 'DO', 'END',
'IF', 'THEN', 'ELSE',
'COMMAND',
'LPAREN', 'RPAREN', 'COMMA',
'FUNC_ID', 'ARG', 'ID', 'NUM',
'ASSIGN', 'ADD_ASSIGN', 'MUL_ASSIGN', 'MOD_ASSIGN', 'SUB_ASSIGN', 'DIV_ASSIGN',
'ADD', 'SUB', 'MUL', 'DIV', 'MOD',
'AND', 'OR', 'NOT',
'LT', 'GT', 'LTE', 'GTE', 'EQ', 'NE',
'TRUE', 'FALSE'
)
states = (
('funcy', 'exclusive'),
)
t_COMMAND = r'`[^`]*`'
# selector = r'(@[prae](\[[^\]]*\])?)|([A-Za-z0-9_][A-Za-z0-9_]*)'
# t_ID = r'\$(' + selector + r')(:[A-Za-z0-9_][A-Za-z0-9_]*)?'
t_ID = r'((@[prae](\[[^\]]*\])?)|(\$[A-Za-z0-9_][A-Za-z0-9_]*))(:[A-Za-z0-9_][A-Za-z0-9_]*)?'
non_zero_num = r'-?[1-9][0-9]*'
t_NUM = r'0' + r'|' + non_zero_num
# assignment operators
t_ASSIGN = r'='
t_ADD_ASSIGN = r'\+='
t_SUB_ASSIGN = r'-='
t_MUL_ASSIGN = r'\*='
t_DIV_ASSIGN = r'/='
t_MOD_ASSIGN = r'%='
# comparison operators
t_LT = r'<'
t_LTE = r'<='
t_GT = r'>'
t_GTE = r'>='
t_EQ = r'=='
t_NE = r'!='
# math operators
t_ADD = r'\+'
t_SUB = r'-'
t_MUL = r'\*'
t_DIV = r'/'
t_MOD = r'%'
# bool operators
def t_AND(t): 'and'; return t;
def t_OR(t): 'or'; return t;
def t_NOT(t): 'not'; return t;
def t_TRUE(t): 'true'; return t;
def t_FALSE(t): 'false'; return t;
# Ignore whitespace
t_ignore = " \t"
def t_WHILE(t): 'while'; return t;
def t_DO(t): 'do'; return t;
def t_IF(t): 'if'; return t;
def t_THEN(t): 'then'; return t;
def t_ELSE(t): 'else'; return t;
def t_END(t): 'end'; return t;
def t_FUNC_ID(t):
r'[A-Za-z][A-Za-z0-9_]*'
t.lexer.begin('funcy')
return t
def t_COMMENT(t):
r'\#.*'
pass
def t_newline(t):
r'\n+'
t.lexer.lineno += t.value.count("\n")
def t_error(t):
# print("Illegal character '%s'" % t.value[0])
t.lexer.skip(1)
"""
Macros
"""
t_funcy_INITIAL_LPAREN = r'\('
double_quote_string = r'\"([^\\\n]|(\\.))*?\"'
single_quote_string = r'\'([^\\\n]|(\\.))*?\''
argument = fr'({double_quote_string})|' \
fr'({single_quote_string})|' \
fr'({t_ID})|' \
fr'(~?{non_zero_num})'
# string constant recognition from PLY ansi C example
# modified to include single quotes
@TOKEN(argument)
def t_funcy_ARG(t):
# FIXME can't escape a string that begins with $ or ~
import ast
if t.value[0] not in ['$', '~']:
t.value = str(ast.literal_eval(t.value))
return t
t_funcy_COMMA = r','
t_funcy_ignore = ""
def t_funcy_error(t):
# print("Illegal character '%s'" % t.value[0])
t.lexer.skip(1)
def t_funcy_INITIAL_RPAREN(t):
r'\)'
t.lexer.begin('INITIAL')
return t
"""
Yacc definitions (parser)
"""
precedence = (
('left', 'OR'),
('left', 'AND'),
('nonassoc', 'GT', 'LT', 'LTE', 'GTE', 'EQ', 'NE'),
('left', 'ADD', 'SUB'),
('left', 'MUL', 'DIV', 'MOD'),
('right', 'NOT')
)
def p_start(t):
"""start : statements"""
t[0] = t[1]
def p_statements(t):
"""
statements : statements statement
| statement
"""
if len(t) == 2:
t[0] = [t[1]]
else:
t[0] = t[1] + [t[2]]
def p_statement(t):
"""statement : command
| while_statement
| if_statement
| assign_statement
| func
"""
t[0] = t[1]
def p_assign_statement(t):
"""
assign_statement : id ASSIGN root_expression
| id ADD_ASSIGN root_expression
| id SUB_ASSIGN root_expression
| id DIV_ASSIGN root_expression
| id MUL_ASSIGN root_expression
| id MOD_ASSIGN root_expression
"""
# print "assign_statement"
t[0] = AssignNode(t[1], t[2], t[3])
def p_root_expression(t):
"""root_expression : expression"""
t[0] = ExpressionNode(t[1])
def p_not_expression(t):
"""expression : NOT expression
"""
t[0] = [t[1], t[2]]
def p_expression(t):
"""expression : expression ADD expression %prec ADD
| expression SUB expression %prec SUB
| expression MUL expression %prec MUL
| expression DIV expression %prec DIV
| expression MOD expression %prec MOD
| expression GT expression %prec GT
| expression GTE expression %prec GTE
| expression LT expression %prec LT
| expression LTE expression %prec LTE
| expression NE expression %prec NE
| expression EQ expression %prec EQ
| expression OR expression %prec OR
| expression AND expression %prec AND
| LPAREN expression RPAREN
| id
| const
| command
| func
"""
if len(t) == 2:
if type(t[1]) in [MacroNode, CommandNode]:
t[1].context = "expression"
t[0] = t[1]
elif t[1] == '(':
t[0] = t[2]
else:
t[0] = [t[2], t[1], t[3]]
# print t[0]
def p_id(t):
""" id : ID """
ident = t[1]
if ident[0] == '@':
ident = '$' + ident
if ':' in ident:
selector, objective = ident.split(':')
t[0] = {'selector': selector, 'objective': objective}
else:
t[0] = {'selector': ident}
def p_const(t):
"""const : NUM
| TRUE
| FALSE"""
t[0] = {'true': '1', 'false': '0'}.get(t[1], t[1])
def p_func(t):
"""func : FUNC_ID LPAREN args RPAREN"""
t[0] = MacroNode(t[1], t[3])
def p_command(t):
"""command : COMMAND"""
t[0] = CommandNode(t[1][1:-1])
def p_args(t):
"""args : arg
| args COMMA arg
"""
if len(t) == 2:
t[0] = [t[1]]
else:
t[0] = t[1] + [t[3]]
# FIXME: don't use a single generic terminal
def p_arg(t):
"""arg : ARG"""
t[0] = t[1]
# print "found ARG: {}".format(t[0])
def p_if_statement(t):
"""if_statement : IF root_expression THEN statements END"""
t[0] = IfNode(condition=t[2], body=t[4])
def p_if_else_statement(t):
"""if_statement : IF root_expression THEN statements ELSE statements END"""
t[0] = IfNode(condition=t[2], body=t[4], else_body=t[6])
def p_while_statement(t):
"""while_statement : WHILE root_expression DO statements END"""
t[0] = WhileNode(t[2], t[4])
def p_error(t):
if t:
print("Syntax error at {} on line {}".format(t.value, t.lineno))
else:
print("Syntax error: Unexpected end of input (Perhaps a missing 'end')")
def compile_cbc(input_file):
assemblies = [Node(parser.parse(Path(input_file).read_text())).expand()]
seen_includes = set()
while macros.includes:
include = macros.includes.pop()
if include not in seen_includes:
seen_includes.add(include)
assemblies.append(Node(parser.parse(Path(include).read_text())).expand())
return concat_assembly(assemblies)
def concat_assembly(assemblies):
assembly_headers = [x.splitlines()[0] for x in assemblies]
new_assembly_parts = [f'.MIXED_{assembly_headers[0][1:]}']
for header in assembly_headers:
new_assembly_parts.append(f'jmp {header[1:]}')
for body in assemblies:
new_assembly_parts.append(body)
return '\n'.join(new_assembly_parts)
if __name__ == '__main__':
if len(sys.argv) != 3:
print("Incorrect number of arguments")
print(f"usage: {sys.argv[0]} <input_file> <structure_name>")
print(f"example: {sys.argv[0]} example.cbc example_structure")
exit(1)
input_file_name = sys.argv[1]
output_file_name = sys.argv[2]
lexer = lex.lex()
parser = yacc.yacc(debug=1)
print(f"compiling {input_file_name}")
assembly_file = compile_cbc(input_file_name)
Path(input_file_name.rsplit('.', maxsplit=1)[0] + '.cba').write_text(assembly_file)
assemble.assemble(assembly_file, output_file_name).write_file()