-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast_nodes.py
executable file
·66 lines (47 loc) · 2.91 KB
/
ast_nodes.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
import collections
"""An abstract syntax tree is the tree of actual semantic operations the program executes."""
class IntegerLiteral(collections.namedtuple("IntegerLiteral", "value")):
"""Represents an integer in the code."""
def to_dict(self):
"""We have a dictionary converting method to be able to convert this into json."""
return {"type": "IntegerLiteral", "value": int(self.value)}
class NArgumentInstruction(collections.namedtuple("NArgumentInstruction", ("op", "args"))):
"""Represents a TIS-Py00 instruction with an arbitrary number of arguments."""
def to_dict(self):
"""We have a dictionary converting method to be able to convert this into json."""
return {"type": "NArgumentInstruction", "op": self.op, "args": [arg_node.to_dict() for arg_node in self.args]}
class NodeMarker(collections.namedtuple("NodeMarker", ("id", "ast_subtree"))):
"""Represents the code that a single node has."""
def to_dict(self):
"""We have a dictionary converting method to be able to convert this into json."""
return {"type": "NodeMarker", "id": int(self.id), "value": self.ast_subtree.to_dict()}
class PortLiteral(collections.namedtuple("PortLiteral", "name")):
"""Represents a port in an instruction."""
def to_dict(self):
"""We have a dictionary converting method to be able to convert this into json."""
return {"type": "PortLiteral", "value": self.name}
class RegisterLiteral(collections.namedtuple("RegisterLiteral", "name")):
"""Represents a register in an instruction."""
def to_dict(self):
"""We have a dictionary converting method to be able to convert this into json."""
return {"type": "RegisterLiteral", "value": self.name}
class Label(collections.namedtuple("Label", "name")):
"""Represents a label definition."""
def to_dict(self):
"""We have a dictionary converting method to be able to convert this into json."""
return {"type": "Label", "value": self.name}
class LabelReference(collections.namedtuple("LabelReference", "name")):
"""Represents a label reference."""
def to_dict(self):
"""We have a dictionary converting method to be able to convert this into json."""
return {"type": "LabelReference", "value": self.name}
class SingleNodeAST(collections.namedtuple("SingleNodeAST", "ast")):
"""Represents a single node and its code."""
def to_dict(self):
"""We have a dictionary converting method to be able to convert this into json."""
return {"type": "SingleNodeAST", "ast": [ast_node.to_dict() for ast_node in self.ast]}
class ASTRoot(collections.namedtuple("ASTRoot", "nodes")):
"""Represents a program with nodes and their code."""
def to_dict(self):
"""We have a dictionary converting method to be able to convert this into json."""
return {"type": "ASTRoot", "nodes": {key: val.to_dict() for key, val in self.nodes.items()}}