-
Notifications
You must be signed in to change notification settings - Fork 0
/
goneinterp.py
134 lines (104 loc) · 4.7 KB
/
goneinterp.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
class Interpreter(object):
def __init__(self, name="module"):
# Dictionary of currently defined variables
self.vars = {}
# List of Python modules to search for external decls
external_libs = ['math', 'os']
self.external_libs = [__import__(a) for a in external_libs]
def run(self, ircode):
'''
Run intermediate code in the interpreter. ircode is a list
of instruction tuples. Each instruction (opcode, *args) is
dispatched to a method self.run_opcode(*args)
'''
for op in ircode:
opcode = op[0]
opname, typename = opcode.split("_")
matches = [a for a in self.__dir__() if a.startswith("gen_" + opname + "_")]
ran = False
for match in matches:
if typename in match.split("_")[2:]:
getattr(self, match)(*op[1:])
ran = True
break
if not ran:
if hasattr(self, "run_" + opcode):
getattr(self, "run_" + opcode)(*op[1:])
else:
print("Warning: No run_" + opcode + "() method")
def gen_alloc_int_float_string_bool(self, name):
self.vars[name] = None
def gen_store_int_float_string_bool(self, target, name):
self.vars[name] = self.vars[target]
def gen_load_int_float_string_bool(self, name, target):
self.vars[target] = self.vars[name]
def gen_sub_int_float(self, left, right, target):
self.vars[target] = self.vars[left] - self.vars[right]
def gen_mul_int_float(self, left, right, target):
self.vars[target] = self.vars[left] * self.vars[right]
def gen_div_int_float(self, left, right, target):
self.vars[target] = self.vars[left] / self.vars[right]
def gen_add_int_float_string(self, left, right, target):
self.vars[target] = self.vars[left] + self.vars[right]
def gen_lt_int_float(self, left, right, target):
self.vars[target] = self.vars[left] < self.vars[right]
def gen_gt_int_float(self, left, right, target):
self.vars[target] = self.vars[left] > self.vars[right]
def gen_lte_int_float(self, left, right, target):
self.vars[target] = self.vars[left] <= self.vars[right]
def gen_gte_int_float(self, left, right, target):
self.vars[target] = self.vars[left] >= self.vars[right]
def gen_eq_int_float_bool_string(self, left, right, target):
self.vars[target] = self.vars[left] == self.vars[right]
def gen_neq_int_float_bool_string(self, left, right, target):
self.vars[target] = self.vars[left] != self.vars[right]
def gen_and_bool(self, left, right, target):
self.vars[target] = self.vars[left] and self.vars[right]
def gen_or_bool(self, left, right, target):
self.vars[target] = self.vars[left] or self.vars[right]
def gen_uadd_int_float(self, source, target):
self.vars[target] = self.vars[source]
def gen_usub_int_float(self, source, target):
self.vars[target] = -1 * self.vars[source]
def gen_not_bool(self, source, target):
self.vars[target] = not self.vars[source]
def gen_literal_int_float_string_bool(self, value, target):
self.vars[target] = value
def gen_print_int_float_string(self, source):
print(self.vars[source])
def run_print_bool(self, source):
print(str(self.vars[source]).lower())
def run_extern_func(self, name, ret_type, *arg_types):
found = False
for lib in self.external_libs:
if name in lib.__dict__:
self.vars[name] = lib.__dict__[name]
found = True
if not found:
raise RuntimeError("Error: extern '{}' not found".format(name))
def run_call_func(self, name, target, *func_args):
self.vars[target] = self.vars[name](*[self.vars[a] for a in func_args])
def main():
import gonelex
import goneparse
import gonecheck
import gonecode
import sys
import time
from errors import subscribe_errors, errors_reported
lexer = gonelex.make_lexer()
parser = goneparse.make_parser()
with subscribe_errors(lambda msg: sys.stdout.write(msg + "\n")):
program = parser.parse(open(sys.argv[1]).read())
# Check the program
gonecheck.check_program(program)
# If no errors occurred, generate code
if not errors_reported():
code = gonecode.generate_code(program)
interpreter = Interpreter()
start = time.clock()
interpreter.run(code.code)
print(":::: FINISHED ::::")
print("execution time: {0:.15f}s".format(time.clock() - start))
if __name__ == '__main__':
main()