-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbeatle.py
executable file
·288 lines (214 loc) · 9.65 KB
/
beatle.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
#!/usr/bin/env python3
import argparse
import os
import pathlib
import readline
import sys
from utils import ApeError, pformat, to_sexpr
import parser
import scanner
import importer
import macroexpander
import typechecker
import evaluator
PHASES = ['SCAN', 'PARSE', 'IMPORTS', 'MACROS', 'TYPES', 'EVAL']
PROMPT_APE = "\U0001F98D "
PROMPT_LAM = "λ> "
PROMPT_ARR = "> "
PROMPT = PROMPT_ARR
def add_phase_arguments(parser):
phases = parser.add_argument_group(title='phases of compilation')
# These options *set* the phases, so they are mutually exclusive.
ph_mutex = phases.add_mutually_exclusive_group()
ph_mutex.add_argument('-p', '--phases', nargs='+', metavar="PHASE", choices=PHASES, default=PHASES)
ph_mutex.add_argument('--scan', action='store_const', dest='phases', const=PHASES[:1],
help='shorthand for --phase ' + ' '.join(PHASES[:1]))
ph_mutex.add_argument('--parse', action='store_const', dest='phases', const=PHASES[:2],
help='shorthand for --phase ' + ' '.join(PHASES[:2]))
ph_mutex.add_argument('--imports', action='store_const', dest='phases', const=PHASES[:3],
help='shorthand for --phase ' + ' '.join(PHASES[:3]))
ph_mutex.add_argument('--macros', action='store_const', dest='phases', const=PHASES[:4],
help='shorthand for --phase ' + ' '.join(PHASES[:4]))
ph_mutex.add_argument('--types', action='store_const', dest='phases', const=PHASES[:5],
help='shorthand for --phase ' + ' '.join(PHASES[:5]))
ph_mutex.add_argument('--eval', action='store_const', dest='phases', const=PHASES[:6],
help='shorthand for --phase ' + ' '.join(PHASES[:6]))
# The rest of the options *add to* the phases, so any combination can
# be added.
# opt_flags = phases.add_mutually_exclusive_group()
# opt_flags.add_argument('-O1', action='append_const', dest='phases',
# const='BASIC_OPT',
# help='basic optimisations')
# opt_flags.add_argument('-O2', action='append_const', dest='phases',
# const='ADV_OPT',
# help='advanced optimisations')
def parse_args():
parser = argparse.ArgumentParser(description='ApeVM Beatle Compiler')
parser.add_argument('-v', '--verbose', action='count', default=0,
help='enables verbose output mode. the more \'-v\'s, the more output.')
parser.add_argument('-s', '--stacktrace', action='store_true', default=False,
help='enable printing of stacktraces on errors that '
'are potentially user errors')
parser.add_argument('--tokens', type=argparse.FileType('r'), default=open('tokens.json'),
help='a file with a list of tokens and their regexes')
parser.add_argument('--keywords', type=argparse.FileType('r'), default=open('keywords.json'),
help='a file with a list of literal tokens (keywords)')
subparsers = parser.add_subparsers(metavar='COMMAND', help='the following commands are built into beatle')
# Unfortunately you can't set this in the add_subparsers command anymore
subparsers.required = True
repl_parser = subparsers.add_parser('repl', aliases=['r'], help='type beatle code directly into a read-eval-print loop')
add_phase_arguments(repl_parser)
com_parser = subparsers.add_parser('compile', aliases=['c'], help='compile a single file')
com_parser.add_argument('input', metavar='INPUT_FILE', default=sys.stdin, nargs='?', type=argparse.FileType('r'))
com_parser.add_argument('-o', '--output', type=argparse.FileType('w'), default=None)
add_phase_arguments(com_parser)
# build_parser = subparsers.add_parser('build', aliases=['b'], help='build a file along with all its dependencies')
# build_parser.add_argument('input', metavar='INPUT_FILE', default=sys.stdin, nargs='?', type=argparse.FileType('r'))
repl_parser.set_defaults(func=cmd_repl)
com_parser.set_defaults(func=cmd_compile)
# build_parser.set_defaults(func=cmd_build)
return parser.parse_args()
def validate_phases(args):
if args.verbose >= 2:
print('args:')
print(pformat(vars(args)))
if 'PARSE' in args.phases and 'SCAN' not in args.phases:
print('Unworkable --phase arguments: '
'PARSE phase requires SCAN phase')
return False
if 'IMPORTS' in args.phases and 'PARSE' not in args.phases:
print('Unworkable --phase arguments: '
'IMPORTS phase requires SCAN and PARSE phases')
return False
if 'MACROS' in args.phases and 'IMPORTS' not in args.phases:
print('Unworkable --phase arguments: '
'MACROS phase requires SCAN, PARSE and IMPORTS phases')
return False
if 'TYPES' in args.phases and 'MACROS' not in args.phases:
print('Unworkable --phase arguments: '
'TYPES phase requires SCAN, PARSE, IMPORTS and MACROS phases')
return False
if 'EVAL' in args.phases and 'TYPES' not in args.phases:
print('Unworkable --phase arguments: '
'EVAL phase requires SCAN, PARSE, IMPORTS, MACROS and TYPES phases')
return False
return True
def scan(input_text, args, regexp=None):
if regexp is None:
regexp = scanner.Regexp.from_files(args.keywords, args.tokens)
tokens = scanner.scan(args.verbose, regexp, input_text)
if args.verbose >= 2:
print('tokens:')
print(pformat(tokens))
return tokens, regexp
def parse(tokens, args, input_text, initial_production):
ast = parser.any_input(tokens, input_text, initial_production=initial_production)
if args.verbose >= 1:
print('ast:')
print(to_sexpr(ast, indent=None if args.verbose == 1 else 4))
return ast
def imports(ast, args, input_text, regexp):
try:
base_search_path = os.path.dirname(args.input.name)
except AttributeError:
base_search_path = os.path.abspath('.')
ast = importer.process(ast, base_search_path, (args, regexp, scan, parse))
if args.verbose >= 1:
print('import-expanded ast:')
print(to_sexpr(ast, indent=None if args.verbose == 1 else 4))
return ast
def macros(ast, args, input_text):
ast = macroexpander.process(ast)
if args.verbose >= 1:
print('macro-expanded ast:')
print(to_sexpr(ast, indent=None if args.verbose == 1 else 4))
return ast
def types(ast, args, input_text):
ast = typechecker.infer(ast)
if args.verbose >= 1:
print('type-annotated ast:')
print(to_sexpr(ast, indent=None if args.verbose == 1 else 4))
return ast
def evaluate(ast, args, input_text):
result = evaluator.evaluate(ast)
if args.verbose >= 1:
print('bytecode:')
print(to_sexpr(result, indent=None if args.verbose == 1 else 4))
return result
# simple usage:
# ./beatle.py compile ./examples/stack.b --> creates stack.bo which contains bytecode and interface
# ./beatle.py compile ./examples/queue.b --> creates queue.bo which contains bytecode and interface
# ./beatle.py compile ./examples/two-stack-queue.b --> reads stack.bo and queue.bo and creates two-stack-queue.bo which contains bytecode and interface
# real usage:
# ./beatle.py build ./examples/two-stack-queue.b --> does all of the above
# repl usage:
# ./beatle.py repl
def cmd_repl(args):
if not validate_phases(args):
sys.exit(1)
if 'SCAN' in args.phases:
regexp = scanner.Regexp.from_files(args.keywords, args.tokens)
readline.read_init_file(pathlib.Path.home() / '.inputrc')
while True:
try:
input_text = input(PROMPT)
except KeyboardInterrupt:
print()
continue
except EOFError:
print()
break
if input_text == "":
continue
try:
result = process_phases(input_text, args, regexp=regexp, initial_production='single_input')
if result is not None:
print(result)
except ApeError:
pass
def cmd_build(args):
raise
def cmd_compile(args):
if not validate_phases(args):
sys.exit(1)
input_text = args.input.read()
if args.verbose >= 2:
print('file:')
print(input_text)
try:
process_phases(input_text, args, regexp=None, initial_production='file_input')
except ApeError:
sys.exit(1)
def process_phases(input_text, args, regexp=None, initial_production='file_input'):
if 'SCAN' not in args.phases:
return
try:
tokens, regexp = scan(input_text, args, regexp=regexp)
if 'PARSE' not in args.phases:
return tokens
ast = parse(tokens, args, input_text, initial_production=initial_production)
if 'IMPORTS' not in args.phases:
return ast
ast = imports(ast, args, input_text, regexp)
if 'MACROS' not in args.phases:
return ast
ast = macros(ast, args, input_text)
if 'TYPES' not in args.phases:
return ast
ast = types(ast, args, input_text)
if 'EVAL' not in args.phases:
return ast
result = evaluate(ast, args, input_text)
return result
except ApeError as exc:
print(exc.format_with_context(input_text, args.stacktrace))
while exc.__cause__ is not None:
print('\nThis was caused by the following error:\n')
print(exc.__cause__.format_with_context(input_text, args.stacktrace))
exc = exc.__cause__
raise
def main():
args = parse_args()
args.func(args)
if __name__ == '__main__':
main()