-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterpreter.py
More file actions
192 lines (148 loc) · 5.42 KB
/
interpreter.py
File metadata and controls
192 lines (148 loc) · 5.42 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
190
191
192
from __future__ import annotations
import dataclasses
import math
from collections import ChainMap
from pprint import pprint
from typing import Any
from tatsu.walkers import NodeWalker
import unit_type
@dataclasses.dataclass(frozen=True)
class RENDER:
path: str
def flatten(lst):
elements = []
for elm in lst:
if isinstance(elm, list) or isinstance(elm, tuple):
elements += flatten(elm)
else:
elements.append(elm)
return elements
def assure_iterable(s):
if hasattr(s, "__iter__"):
return s
else:
return (s,)
@dataclasses.dataclass
class Function:
name: str
parameters: list[str]
expression: Any
builtin: bool = False
def sqrt(x):
return x**0.5
def log(x, base=math.e):
assert x.unit == unit_type.Unit()
return unit_type.UnitType(math.log(x.value, base))
def sin(x):
assert x.unit == unit_type.Unit()
return unit_type.UnitType(math.sin(x.value))
def cos(x):
assert x.unit == unit_type.Unit()
return unit_type.UnitType(math.cos(x.value))
def tan(x):
assert x.unit == unit_type.Unit()
return unit_type.UnitType(math.tan(x.value))
class Interpreter(NodeWalker):
def __init__(self):
super().__init__()
self.variables = ChainMap()
self.functions = {
"sqrt": Function("sqrt", ["x"], sqrt, True),
"sin": Function("sin", ["x"], sin, True),
"cos": Function("cos", ["x"], cos, True),
"tan": Function("tan", ["x"], tan, True),
"log": Function("log", ["x", "base"], log, True),
"ln": Function("log", ["x"], log, True),
}
def walk_object(self, node):
# print(f"object \t\t\t{type(node)=} \t \t {node=}")
return node
def walk__number(self, node):
# print(f"number \t\t\t{type(node)=} \t \t {node=}")
if node.ast == "":
return 1.0
return unit_type.UnitType(float(node.ast))
def walk__add(self, node):
# print(f"add \t\t\t{type(node)=} \t \t {node=}")
return self.walk(node.left) + self.walk(node.right)
def walk__subtract(self, node):
# print(f"subtract \t\t{type(node)=} \t \t {node=}")
return self.walk(node.left) - self.walk(node.right)
def walk__multiply(self, node):
# print(f"multiply \t\t{type(node)=} \t \t {node=}")
return self.walk(node.left) * self.walk(node.right)
def walk__divide(self, node):
# print(f"divide \t\t\t{type(node)=} \t \t {node=}")
return self.walk(node.left) / self.walk(node.right)
def walk__exponentiate(self, node):
# print(f"exponentiate \t{type(node)=} \t \t {node=}")
return self.walk(node.base) ** self.walk(node.exponent)
def walk__invert(self, node):
# print(f"invert \t\t\t{type(node)=} \t \t {node=}")
node = self.walk(node.value)
# print(f"inverted \t\t\t{type(node)=} \t \t {node=}")
return -node
def walk__unit(self, node):
# print(f"unit \t\t\t{type(node)=} \t \t {node=}")
unit = ""
for sub_unit, value in node.ast:
value = self.walk(value)
sub_unit = "".join(sub_unit)
unit = unit + sub_unit + str(value)
return unit_type.Unit.from_string(unit)
def walk__unit_number(self, node):
# print(f"unit_number \t{type(node)=} \t \t {node=}")
return self.walk(node.value) * self.walk(node.unit)
def walk__command(self, node):
match node.cmd:
case "exit" | "quit":
exit()
case "render":
return RENDER(node.path)
case "evaluate":
return self.walk(node.expression)
case "exclude":
return self.walk(node.expression)
case "newline":
return None
assert False
def walk__absolute(self, node):
return abs(self.walk(node.expr))
def walk__call(self, node):
function = self.functions[self.walk(node.function)]
if function.builtin:
return function.expression(*assure_iterable(self.walk(node.args)))
arguments = {}
for param, arg in zip(
function.parameters, assure_iterable(self.walk(node.args))
):
arguments[param] = arg
self.variables = self.variables.new_child(arguments)
# print(function.expression)
ret = self.walk(function.expression)
self.variables = self.variables.parents
# print(function.expression)
# print("asdffdgjlkfdgswkljhölkbdsfjkljdfglkjöfgsdkjsgdfljködfgslkjö:" + str(ret))
return ret
def walk__access(self, node):
return self.variables[self.walk(node.name)]
def walk__variable_definition(self, node):
self.variables[self.walk(node.name)] = self.walk(node.expression)
return None
def walk__function_definition(self, node):
name = self.walk(node.name)
args = self.walk(node.args)
self.functions[name] = Function(name, args, node.expression)
return None
def walk__ident(self, node):
pprint(node)
return self.walk(node.first) + "".join(node.rest)
def walk__definition_argument_list(self, node):
return list(
filter(
lambda s: s != ",",
flatten((self.walk(node.first), self.walk(node.rest))),
)
)
def walk__subexpression(self, node):
return self.walk(node.expr)