-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathast_internal.py
More file actions
51 lines (36 loc) · 1.28 KB
/
Copy pathast_internal.py
File metadata and controls
51 lines (36 loc) · 1.28 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
from abc import ABC
from dataclasses import dataclass
class Expression(ABC):
def to_json(self):
if isinstance(self, VariableNode):
return {"type": self.type, "value": self.value}
elif isinstance(self, LambdaAbstractionNode):
return {"type": self.type, "param": self.param, "body": self.body.to_json()}
elif isinstance(self, LambdaApplicationNode):
return {"type": self.type, "func": self.left.to_json(), "arg": self.right.to_json()}
@dataclass
class VariableNode(Expression):
value: str
type: str = "Variable"
def __str__(self):
return f"{self.value}"
def __repr__(self):
return f"VariableNode({self.value})"
@dataclass
class LambdaAbstractionNode(Expression):
param: str
body: Expression
type: str = "Lambda"
def __str__(self):
return f"fn {self.param}.{repr(self.body)}"
def __repr__(self):
return f"LambdaAbstractionNode('{self.param}', '{repr(self.body)}')"
@dataclass
class LambdaApplicationNode(Expression):
left: Expression
right: Expression
type: str = "Application"
def __str__(self):
return f"({self.left} {self.right})"
def __repr__(self):
return f"LambdaApplicationNode({repr(self.left)}, {repr(self.right)})"