-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.py
More file actions
331 lines (203 loc) · 7.97 KB
/
data.py
File metadata and controls
331 lines (203 loc) · 7.97 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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import re
from dataclasses import dataclass, field
from typing import Any
class TYPE():
value: Any
def external_representation(self):
return str(self.value)
def evaluate(self, env):
return self, env
@classmethod
def from_string(cls, string):
return cls(string)
class NUMBER(TYPE):
pass
@dataclass(frozen=True, init=True, repr=True)
class INTEGER(NUMBER):
value: int
def __repr__(self) -> str:
return f"INT[{str(self.value)}]"
def external_representation(self):
return str(self.value)
@classmethod
def from_string(cls, string):
return cls(int(string))
@dataclass(frozen=True, init=True, repr=True)
class FLOAT(NUMBER):
value: float
def __repr__(self) -> str:
return f"FLOAT[{str(self.value)}]"
@classmethod
def from_string(cls, string):
return cls(float(string))
@dataclass(frozen=True, init=True, repr=True)
class BOOLEAN(TYPE):
value: bool
def __repr__(self) -> str:
return "BOOL["+("t" if self.value else "f")+"]"
@classmethod
def from_string(cls, string):
return cls(True if string == "#t" else False)
@dataclass(frozen=True, init=True, repr=True)
class STRING(TYPE):
value: str
def __repr__(self) -> str:
return "STRING["+self.value+"]"
@dataclass(frozen=True, init=True, repr=True)
class SYMBOL(TYPE):
value: str
def __repr__(self) -> str:
return "SYMBOL["+self.value+"]"
def external_representation(self):
return f"{self.value}"
def evaluate(self, env):
return env.get_symbol(self.value), env
# ----------------
# COMPLEX
# -----------------
def evaluate_tail(tail, env):
tail_results = []
for tail_element in tail:
result, env = tail_element.evaluate(env)
tail_results.append(result)
return tail_results
def check_procedure_arguments(tail, num_arguments=None, arguments_with_type=None):
assert num_arguments is not None or arguments_with_type is not None
if num_arguments is not None:
if not isinstance(num_arguments, list):
num_arguments = [num_arguments]
assert len(tail) in num_arguments
elif arguments_with_type is not None:
assert len(tail) == len(arguments_with_type)
for tail_element, expected_type in zip(tail, arguments_with_type):
assert isinstance(tail_element, expected_type)
@dataclass(frozen=True, init=True, repr=True)
class LIST(TYPE):
parts: tuple # list = field(default_factory=list)
# def __init__(self, parts=None) -> None:
# self.parts = [] if parts is None else parts
def __repr__(self):
result = ""
for part in self.parts:
result += str(part)+" "
return "LIST("+result+")"
def __len__(self):
return len(self.parts)
def external_representation(self):
return "(" + " ".join([part.external_representation() for part in self.parts]) + ")"
@property
def head(self):
return self.parts[0] if self.parts else None
@property
def tail(self):
return self.parts[1:] if len(self.parts) > 0 else None
def evaluate(self, env):
from lili_builtins import BUILTIN_FUNCTIONS, SPECIAL_FORMS
head = self.head
tail = self.tail
if head is None:
return (None, env)
if isinstance(head, LIST):
head, env = head.evaluate(env) # evaluate_expression_tree(head, env)
# TODO make dispatch logic on Symbols
# TODO what if head is Atom or just a symbol like a?
# if head.value in SPECIAL_FORMS:
# result, env = SPECIAL_FORMS[head.value](self, env)
# elif head.value in BUILTIN_FUNCTIONS:
# result, env = BUILTIN_FUNCTIONS[head.value](self, env)
# else:
if isinstance(head, SYMBOL):
head, env = head.evaluate(env)
assert isinstance(head, PROCEDURE)
# TODO could also be just a variable
if isinstance(head, (FUNCTION, BUILTIN_FUNCTION)):
# evaluated_tail = evaluate_tail(tail, env)
# Function call gets the unevaluated tail as input!
# result, env = head.evaluate(self, evaluated_tail, env)
result, env = head.evaluate(tail, env)
elif isinstance(head, PRIMITIVE):
result, env = head.evaluate(tail, env)
return result, env
class PROCEDURE(TYPE):
pass
class FUNCTION(PROCEDURE):
def __init__(self, argument_list, body) -> None:
assert isinstance(argument_list, LIST)
self.argument_list = argument_list.parts
# TODO is argument list a list or a LIST?
# Make sure the argument list consists of symbols!
check_procedure_arguments(self.argument_list, arguments_with_type=[SYMBOL]*len(self.argument_list))
assert isinstance(body, (SYMBOL, LIST))
self.body = body
def external_representation(self):
rep = "lambda "
rep += "(" + " ".join([sym.external_representation() for sym in self.argument_list]) + ") "
rep += self.body.external_representation()
return rep
def evaluate(self, tail, env):
assert isinstance(tail, (list, tuple))
if len(tail) > 0:
for tail_element in tail:
assert isinstance(tail_element, TYPE)
evaluated_tail = evaluate_tail(tail, env)
assert len(evaluated_tail) == len(self.argument_list), "Function called with wrong argument list size!"
# Create call stack
env = env.nest_env()
# Create local scope
env.push_local()
for argument, argument_name in zip(evaluated_tail, self.argument_list):
env.define_symbol(argument_name.value, argument)
result, env = self.body.evaluate(env)
# Remove local scope
env.pop_local()
# Wrap up call stack
env = env.unnest_env()
return result, env
class BUILTIN_FUNCTION(PROCEDURE):
def __init__(self, name, body) -> None:
# assert isinstance(argument_list, LIST)
# self.argument_list = argument_list.parts
# TODO is argument list a list or a LIST?
# Make sure the argument list consists of symbols!
# check_procedure_arguments(self.argument_list, arguments_with_type=[SYMBOL]*len(self.argument_list))
# assert isinstance(body, (SYMBOL, LIST))
self.name = name
self.body = body
def external_representation(self):
return f"BUILTIN_FUNCTION({self.name})"
def evaluate(self, tail, env):
assert isinstance(tail, (list, tuple))
if len(tail) > 0:
for tail_element in tail:
assert isinstance(tail_element, TYPE)
evaluated_tail = evaluate_tail(tail, env)
# assert len(evaluated_tail) == len(self.argument_list), "Function called with wrong argument list size!"
# Create call stack
# env = env.nest_env()
# Create local scope
# env.push_local()
# for argument, argument_name in zip(evaluated_tail, self.argument_list):
# env.define_symbol(argument_name.value, argument)
result, env = self.body(evaluated_tail, env)
# Remove local scope
# env.pop_local()
# Wrap up call stack
# env = env.unnest_env()
return result, env
class MACRO(PROCEDURE):
pass
class PRIMITIVE(PROCEDURE):
def __init__(self, name, body) -> None:
# assert isinstance(argument_list, LIST)
# self.argument_list = argument_list.parts
# TODO is argument list a list or a LIST?
# Make sure the argument list consists of symbols!
# check_procedure_arguments(self.argument_list, arguments_with_type=[SYMBOL]*len(self.argument_list))
# assert isinstance(body, (SYMBOL, LIST))
self.name = name
self.body = body
def external_representation(self):
return f"PRIMITIVE({self.name})"
def evaluate(self, tail, env):
result, env = self.body(tail, env)
return result, env