-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
70 lines (53 loc) · 1.53 KB
/
utils.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
from nodes import (
Binary,
BinaryOp,
Bool,
Call,
First,
Function,
If,
Int,
Let,
Parameter,
Print,
Second,
Str,
Tuple,
Var,
)
def identity(x):
return x
def with_parameters(object_data: dict):
parameters = object_data.get("parameters", [])
mapped_parameters = list(map(lambda parameter: Parameter(**parameter), parameters))
return {**object_data, "parameters": mapped_parameters}
def with_parameter(object_data: dict):
parameter = object_data["name"]
mapped_parameter = Parameter(**parameter)
return {**object_data, "name": mapped_parameter}
def with_binary_operation(object_data: dict):
op = object_data["op"]
mapped_op = BinaryOp(op)
return {**object_data, "op": mapped_op}
SUPPORTED_NODES = {
Var.kind: (Var, identity),
Function.kind: (Function, with_parameters),
Call.kind: (Call, identity),
Let.kind: (Let, with_parameter),
Str.kind: (Str, identity),
Int.kind: (Int, identity),
Bool.kind: (Bool, identity),
If.kind: (If, identity),
Binary.kind: (Binary, with_binary_operation),
Tuple.kind: (Tuple, identity),
First.kind: (First, identity),
Second.kind: (Second, identity),
Print.kind: (Print, identity),
}
def to_ast_node(object_data: dict):
if kind := object_data.get("kind"):
if node_handler := SUPPORTED_NODES.get(kind):
node, handler = node_handler
node_arguments = handler(object_data)
return node(**node_arguments)
return object_data