-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp2l_supported.py
47 lines (38 loc) · 1.13 KB
/
p2l_supported.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
import ast
from instructions import *
from p2l_internals import *
class SupportedOperations(object):
def get_or_fail(self, op):
for class_name, result in self.value.items():
if isinstance(op, class_name):
# should return AbstractOp type
assert isinstance(result, AbstractOp)
return result
raise CompilationError(
"Couldn't find {} in {}".format(
op,
self.__class__
)
)
class COMPARE_OPERATIONS(SupportedOperations):
value = {
ast.Gt: CompareGreater(),
ast.GtE: CompareGreaterOrEqual(),
ast.Eq: CompareEqual()
}
class BINARY_OPERATIONS(SupportedOperations):
value = {
ast.Add: IntAddition(),
ast.Sub: IntSubtraction(),
ast.Mult: IntMultiplication(),
ast.Div: IntDivision()
}
BUILTIN_FUNCTIONS = {
'PRINT' : DebugPrint(),
'FIRST' : ExtractFirst(),
'SECOND': ExtractSecond(),
'ATOM' : IsInteger()
}
def search_in_builtin(func_name):
function = BUILTIN_FUNCTIONS.get(func_name, None)
return function