-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser_structures.py
368 lines (274 loc) · 12.5 KB
/
parser_structures.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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import logging
from parser_exceptions import *
from typing import Any, List, Callable, Dict, Union, TypeVar
from copy import deepcopy, copy
logger = logging.getLogger(__name__)
class Entity:
"""Entity, a supertype for all citizens of this language, who support basic operations
Entities have to support two basic operations, truthy which determines the truth value of the entity, and eval, which evaluates the entity to a integer. Partial functions cannot be evaluated.
"""
def __init__(self):
super().__init__()
self.context: Context = {}
def truthy(self, context: Dict[str, "Entity"]) -> bool:
"""Determines the truth value of this Entity.
In general, null like values should be false, and all other values should be true
"""
raise NotImplementedException(
"Cannot evaluate truthness on abstract Entity")
return False
def eval(self, context: Dict[str, "Entity"]) -> "Entity":
"""Evaluates this entity within a context.
"""
raise NotImplementedException(
"Entity superclasses cannot be evaluated")
def __repr__(self):
return "Entity()"
Context = Dict[str, Entity]
class Expression(Entity):
"""Expression Entity, used to describe expressions.
Expressions do not define a new environment, they just get evaluated in an already existing environment. Therefore it needs an existing environment (as dict) to evaluate. It replaces the arguments with the value of the context, and then evaluates to another Entity.
"""
bound_context = None
# operation str may be changed to direct reference to a copy of this function for environment purposes
def __init__(self, operation: str = None, arguments: List[Entity] = None):
super().__init__()
if (operation == None):
operation = ""
if (arguments == None):
arguments = []
self.operation = operation
self.arguments = arguments
"""Adds a parameter to this expression
This adds parameters which will then be then evaluated in context of the operation of this expression.
"""
def add_parameter(self, parameter: Entity):
self.arguments.append(parameter)
def eval(self, context: Dict[str, Entity]) -> Entity:
logger.debug("Evaluating operation %s", self.operation)
current_context = copy(context)
if self.bound_context is not None:
current_context.update(self.bound_context)
current_context.update(self.context)
function = deepcopy(current_context[self.operation])
# make difference in evaluation between conditional statements and others
if self.operation != "cond":
for argument in self.arguments:
try:
evaluated_arg = None
if type(argument) is Name:
evaluated_arg = argument.bind( # type: ignore
current_context) # type: ignore
else:
evaluated_arg = argument.eval(current_context)
if type(function) is Record:
try:
return function.pairs[evaluated_arg.name].eval(current_context) # type: ignore
except AttributeError:
raise InvalidArgumentException(
"A record can only be evaluated as function when given a name")
function.add_parameter(evaluated_arg) # type: ignore
except KeyError:
raise InvalidArgumentException(
f'Argument {argument} is not defined in the context.')
except AttributeError:
raise InvalidOperationException(
f'Operation {self.operation} is no valid function.')
value = function.eval(current_context)
return value
else:
try:
function.add_parameter(self.arguments[0].eval( # type: ignore
current_context))
function.add_parameter(self.arguments[1]) # type: ignore
function.add_parameter(self.arguments[2]) # type: ignore
except AttributeError:
raise InvalidOperationException(
f'Operation {self.operation} is no valid function.')
return function.eval(current_context)
def __repr__(self) -> str:
return f'Expression(operation: {self.operation}, arguments: {self.arguments})'
def __str__(self) -> str:
return f'{self.operation} {" ".join(map(lambda x: x.__str__(), self.arguments))}'
class Name(Expression):
"""Name Entity, used to describe names.
Names evaluate to its Entity value given in the context
"""
bound_context: Union[Context, None] = None
def __init__(self, name: str):
super().__init__()
self.name = name
def truthy(self, context: Context):
a = context[self.name].eval(
copy(context)).truthy(copy(context))
return a
"""Returns a new Name variable with the specified bounded context.
Returns a new Name, which has a reference to the bounded context it originated, to provide proper name resolution.
"""
def bind(self, context: Context) -> Entity:
new_name = Name(self.name)
new_name.bound_context = context
return new_name
def eval(self, context: Context) -> Entity:
if self.bound_context is None:
return context[self.name].eval(context)
else:
return self.bound_context[self.name].eval(self.bound_context)
def __repr__(self) -> str:
return f'Name(name: {self.name})'
def __str__(self) -> str:
return self.name
class Integer(Expression):
"""Integer Entity, used to describe integers.
Integers are evaluated to False if they are 0, and True otherwise.
"""
value: int
def __init__(self, value: int):
super().__init__()
self.value = value
def truthy(self, context: Context) -> bool:
return self.value != 0
def eval(self, context: Context) -> Entity:
return self
def __repr__(self):
return f'Integer(value: {self.value})'
def __str__(self) -> str:
return str(self.value)
class Function(Entity):
"""Function Entity, used to describe functions.
Functions are per default evaluated to True.
They are very similar to Records, basically providing an environment for the expression which makes out the body of the function, which when evaluated returns the result of the function.
"""
parameters: Dict[str, Union[Entity, None]]
expression: Union[Expression, None]
def __init__(self, parameters=None, expression: Expression = None):
super().__init__()
if (parameters is None):
parameters = {}
self.parameters = parameters
self.expression = expression
def number_of_parameters(self):
return len(self.parameters)
def add_parameter(self, parameter: Entity):
"""Sets parameter value for a parameter of this function
Fills parameters per position.
Example: Function with two parameters, x and y.
First call to this function sets parameter x, second call sets parameter y. Used for partial functions.
"""
for key, value in self.parameters.items():
if (value is None):
self.parameters[key] = parameter
return
def eval(self, context: Context) -> Entity:
if (self.expression is None):
raise InvalidOperationException("Expression for function not set")
for x in self.parameters.items():
if (x[1] is None):
return self
newcontext = copy(context)
newcontext.update(self.context)
newcontext.update(self.parameters) # type: ignore
a = self.expression.eval(newcontext)
return a
def truthy(self, context: Context) -> bool:
try:
a = self.eval(context).truthy(context)
return a
except (NotEnoughArgumentsException, InvalidArgumentException):
logger.warning("CAN NOT EVAL EXPRESSION IN BOOLEAN POSITION!")
return False
def __repr__(self) -> str:
return f'Function(parameters: {self.parameters}, expression: {self.expression.__repr__()})'
def __str__(self) -> str:
return f'{" -> ".join(map(lambda item: f"{item[0]} ({item[1]})", self.parameters.items()))} -> {self.expression.__str__()})'
def __deepcopy__(self, memodict):
return Function(deepcopy(self.parameters), deepcopy(self.expression))
class BasicFunction(Function):
"""Basic function entity, evaluates Basic Functions like add or sub
Contains logic to evaluate basic functions within our Entity class scheme
"""
basic_operations: Dict[str, Callable[..., Any]] = {
"add": lambda x, y: x + y,
"sub": lambda x, y: x - y,
"mult": lambda x, y: x * y,
"div": lambda x, y: x / y,
"cond": lambda x, y, z: y if x else z
}
parameters: List[Entity]
def __init__(self, function_name: str):
super().__init__()
self.function_name = function_name
self.parameters = []
def number_of_parameters(self):
return 3 if self.function_name == "cond" else 2
def add_parameter(self, parameter: Entity):
self.parameters.append(parameter)
def truthy(self, context: Context) -> bool:
return self.eval(context).truthy(context)
def eval(self, context: Context) -> Entity:
logger.debug("eval " + self.function_name)
# Check argument length matches parameter count
if len(self.parameters) > self.number_of_parameters():
raise TooManyArgumentsException(
f'Too many arguments for this function. Expected: {self.number_of_parameters()} Got: {len(self.parameters)}')
if len(self.parameters) < self.number_of_parameters():
return self
if (self.function_name != "cond"):
# evaluate all parameters
try:
evaluated_params = map(lambda x: x.eval(
context).value, self.parameters) # type: ignore
except AttributeError:
raise InvalidArgumentException(
"Need integer argument for basic arithmetic functions")
# evaluate basic function
r = Integer(BasicFunction.basic_operations[self.function_name](
*evaluated_params))
return r
else:
# evaluate basic function
if self.parameters[0].truthy(context):
a = self.parameters[1].eval(context)
return a
else:
a = self.parameters[2].eval(context)
return a
def __repr__(self) -> str:
return f'BasicFunction(function_name: {self.function_name}, Parameters: {self.parameters})'
def __str__(self) -> str:
return f'{self.function_name} {" ".join(map(lambda x: x.__str__(), self.parameters))})'
def __deepcopy__(self, memodict):
return BasicFunction(self.function_name)
class Record(Entity):
""" Record Entity, used to describe records (as first class citizens of this language it is an entity as anything else).
Records are environments consisting of key - value pairs, with string as keys and other entities.
Records are evaluated as False in boolean comparisons if they are empty, and True otherwise.
"""
pairs: Dict[str, Entity]
def __init__(self):
super().__init__()
self.pairs = {}
def truthy(self, context: Context) -> bool:
return bool(self.pairs)
def eval(self, context: Context) -> Entity:
if len(self.pairs) == 0:
return self
value = Record()
for item in self.pairs.items():
a = item[1].eval(context)
value.pairs[item[0]] = a
return value
def __repr__(self):
return f'Record(pairs: {self.pairs})'
def __str__(self):
return f'{{{", ".join(map(lambda x: f"{x[0]}={x[1]}", self.pairs.items()))}}}'
S = TypeVar("S")
T = TypeVar("T")
def flat_dict(dict_list: List[Dict[S, T]]) -> Dict[S, T]:
""" Maps a list of dicts to a single dict containing all its values.
Values later in the list (i.E. with higher index values) will overwrite Values defined by earlier dicts with the same key.
"""
result = {}
for dict in dict_list:
result.update(dict)
return result