/
generator_anoky.py
125 lines (89 loc) · 3.5 KB
/
generator_anoky.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
import sys
import ast
import msgpack
from anoky.common.errors import CompilerError
from anoky.common.record import Record
from anoky.common.old_args_parser import SysArgsParser
#from anoky.parsers.LycParser import LycParser
from anoky.expansion.expander import DefaultExpander
from anoky.generation.generator import DefaultGenerator
from anoky.parsers.anoky_parser import AnokyParser
from anoky.streams.file_stream import FileStream
from anoky.syntax.lisp_printer import indented_lisp_printer
import anoky.common.options as Options
import astpp
from astformatter import ASTFormatter
def unicode_encoder(obj):
return (str(obj)+"\n").encode()
def parse_args(argv):
options = Record({'verbose': False,
'execute': False},)
args = SysArgsParser(argv)
while args.next() is not None:
arg = args()
if arg == '--verbose':
options.verbose = True
Options.PRINT_TREE_TRANSDUCER_OUTPUTS = True
Options.PRINT_ARRANGEMENT_OUTPUTS = True
elif arg == '--binary':
options.binary = True
options.encoder = msgpack.packb
elif arg == '--lex':
if 'encoder' not in options:
options.binary = False
options.encoder = unicode_encoder
if args.peek() is None or args.peek().startswith("--"):
options.output = sys.stdout.buffer
else:
output_name = args.next()
if output_name == 'stdout': options.output = sys.stdout.buffer
if output_name == 'stderr': options.output = sys.stderr.buffer
else: options.output = open(output_name, "wb")
elif arg.lower().endswith('.aky'):
if 'filename' in options:
print("Multiple filenames found!")
exit(-1)
options['filename'] = arg
elif arg == '--execute':
options.execute = True
else:
print("Unexpected option, '%s'" % arg)
exit(-1)
return options
def expand(options):
parser = AnokyParser()
try:
if "filename" not in options:
print("No filename specified.")
return
filename = options.filename
stream = FileStream(filename)
file_node = parser.parse(stream)
# print(indented_lisp_printer(file_node))
expander = DefaultExpander()
ec = expander.expand_unit(file_node)
# print("\n〰〰〰〰〰〰 After macro expansion 〰〰〰〰〰〰")
# print(indented_lisp_printer(file_node))
generator = DefaultGenerator()
py_module = generator.generate_unit(file_node,
# provide expansion context to generation context
EC=ec)
# print("\n〰〰〰〰〰〰 Generated Python code 〰〰〰〰〰〰\n")
# astpp.parseprint(py_module)
# print("\n〰〰〰〰〰〰 Python retrosource 〰〰〰〰〰〰\n")
print(ASTFormatter().format(py_module))
if options.execute:
ast.fix_missing_locations(py_module)
compiled_module = compile(py_module,
filename="<ast>",
mode="exec")
exec(compiled_module)
except CompilerError as e:
print(e.trace)
def main():
options = parse_args(sys.argv)
expand(options)
# node = Form(Identifier("bla bla"), Identifier("arg"))
# print(lisp_printer(node))
if __name__ == "__main__":
main()