-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaeonius.py
More file actions
189 lines (139 loc) · 4.66 KB
/
Copy pathaeonius.py
File metadata and controls
189 lines (139 loc) · 4.66 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
from aeonius_parser import parse
from language.context import Context
from language.grammar import Aeonius
import sys
import inspect
import graphviz
with open("prelude.py", "r") as f:
prelude = f.read()
def get_importing_module():
for frame_info in inspect.stack():
module = inspect.getmodule(frame_info[0])
if module and module.__name__ != __name__:
return module
def importCode(code,module):
exec(code,module.__dict__)
def help():
print(" aeonius ")
print(" A functional extension for Python ")
print("===========================================")
print()
print("Valid arguments:")
print("-h: Get help")
print("-d: Whether to run on debug mode. In debug")
print("mode, the compiled python code is printed")
print("to stdout instead of to a file")
print("-g: Wheather to generate the language graph.")
print("This graph will be generated instead of the")
print("python program")
print("--input: The input aeonius file")
print("--output: The python/graph file to write the")
print("parsed program to")
def parse_args(single_flags, valid_args):
result = {}
argv = []
for s in single_flags:
result[s[1:]] = False
# Remove flags that don't take any arguments
# and process them now
for str in sys.argv:
if str in single_flags:
# Remove initial '-' from string
result[str[1:]] = True
else:
argv = argv + [str]
# User provided arguments start at index 1 (0 is the process name)
# In this format arguments are passed as (note we have removed flags that
# take no arguments)
# program --flag1 value1 --flag2 value2 ...
# In this schema, the values are stored in odd positions and flags
# in even positions.
# As such, we iterate over the even indices of the argv list
for i in range(2, len(argv), 2):
if argv[i - 1] in valid_args:
# Remove first '--' chars from argv
result[argv[i - 1][2:]] = argv[i]
else:
print(f"Invalid argument {argv[i - 1]}")
exit(1)
return result
def transpile(input, debug):
parsed = parse(input + prelude)
context = Context()
context.symbols = Context.stdlib_symbols
(valid, reasons) = parsed.validate(context)
if not valid:
print("Semantic error in code")
for reason in reasons:
print(reason)
exit(-1)
with open("aeonius_stdlib.py", "r") as f:
stdlib = f.read()
context = Context()
context.symbols = Context.stdlib_symbols
if debug:
return (parsed.to_python(context),context)
else:
return (stdlib + parsed.to_python(context),context)
def main():
single = [
"-h",
"-d",
"-g"
]
valid_arguments = [
"--input",
"--output"
]
args = parse_args(single, valid_arguments)
if args["h"]:
help()
return
with open(args["input"], "r") as f:
data = f.read()
if args["g"]:
with open("prelude.py","r") as p:
parsed = parse(p.read() + data)
dot = graphviz.Digraph()
parsed.append_to_graph(dot)
dot.render(args["output"], view=False, format='png')
return
parsed = transpile(data, args["d"])[0]
if (args["d"]):
print(parsed)
else:
with open(args["output"], "w") as g:
g.write(parsed)
def import_main():
args = parse_args(single, valid_arguments)
with open(args["input"], "r") as f:
data = f.read()
parsed = parse(data + prelude)
exec(parsed.to_python(Context()))
def include(module):
with open(module.__file__, "r") as f:
data = f.read()
parsed,context = transpile(data, False)
return importCode(parsed,module.__name__,1)
def includeAE(module):
with open(module.__file__, "r") as f:
data = f.read()
parsed = parse(data + prelude)
context = Context()
context.symbols = Context.stdlib_symbols
(valid, reasons) = parsed.validate(context)
if not valid:
print("Semantic error in code")
for reason in reasons:
print(reason)
exit(-1)
context = Context()
context.symbols = Context.stdlib_symbols
with open("aeonius_stdlib.py", "r") as f:
stdlib = f.read()
parsed.snippets=list(filter(lambda x:isinstance(x, Aeonius),parsed.snippets))
importCode(stdlib + parsed.to_python(context),module)
if __name__ == "__main__":
main()
else:
aeonius_code=includeAE(get_importing_module())