-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathast_nodes.py
More file actions
164 lines (139 loc) · 4.73 KB
/
ast_nodes.py
File metadata and controls
164 lines (139 loc) · 4.73 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
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
class ASTNode:
def __init__(self, line, column):
self.line = line
self.column = column
class ProgramNode(ASTNode):
def __init__(self, statements, line=0, column=0):
super().__init__(line, column)
self.statements = statements
class VarDeclarationNode(ASTNode):
def __init__(self, var_type, var_name, expression=None, line=0, column=0):
super().__init__(line, column)
self.var_type = var_type
self.var_name = var_name
self.expression = expression
class AssignmentNode(ASTNode):
def __init__(self, var_name, expression, line=0, column=0):
super().__init__(line, column)
self.var_name = var_name
self.expression = expression
class IfNode(ASTNode):
def __init__(self, condition, then_block, else_block=None, line=0, column=0):
super().__init__(line, column)
self.condition = condition
self.then_block = then_block
self.else_block = else_block
class WhileNode(ASTNode):
def __init__(self, condition, body, line=0, column=0):
super().__init__(line, column)
self.condition = condition
self.body = body
class DoWhileNode(ASTNode):
def __init__(self, condition, body):
super().__init__()
self.condition = condition
self.body = body
class ForNode(ASTNode):
def __init__(self, init, condition, update, body, line=0, column=0):
super().__init__(line, column)
self.init = init
self.condition = condition
self.update = update
self.body = body
class ForeachNode(ASTNode):
def __init__(self, var_type, var_name, collection, body):
super().__init__()
self.var_type = var_type
self.var_name = var_name
self.collection = collection
self.body = body
class SwitchNode(ASTNode):
def __init__(self, expression, cases):
super().__init__()
self.expression = expression
self.cases = cases
class SwitchCaseNode(ASTNode):
def __init__(self, labels, statements):
super().__init__()
self.labels = labels
self.statements = statements
class BinaryOpNode(ASTNode):
def __init__(self, left, operator, right, line=0, column=0):
super().__init__(line, column)
self.left = left
self.operator = operator
self.right = right
class LiteralNode(ASTNode):
def __init__(self, value, line=0, column=0):
super().__init__(line, column)
self.value = value
self.type = self._infer_type(value)
def _infer_type(self, value):
if isinstance(value, int):
return "int"
elif isinstance(value, float):
return "float"
elif isinstance(value, str):
return "string"
elif isinstance(value, bool):
return "bool"
return "unknown"
class IdentifierNode(ASTNode):
def __init__(self, name, line=0, column=0):
super().__init__(line, column)
self.name = name
self.type = None
self.value = None
self.initialized = False
class ClassNode:
def __init__(self, name, members, line, column):
self.name = name
self.members = members
self.line = line
self.column = column
class MethodNode:
def __init__(self, name, return_type, parameters, body, line, column):
self.name = name
self.return_type = return_type
self.parameters = parameters
self.body = body
self.line = line
self.column = column
class ParameterNode:
def __init__(self, name, param_type, line, column):
self.name = name
self.param_type = param_type
self.line = line
self.column = column
class BlockNode(ASTNode):
def __init__(self, statements, line, column):
super().__init__(line, column)
self.statements = statements # Lista de nodos (sentencias)
class TypeNode:
def __init__(self, name):
self.name = name
class BinaryOpNode(ASTNode):
def __init__(self, op, left, right):
self.op = op
self.left = left
self.right = right
class UnaryOpNode(ASTNode):
def __init__(self, op, operand):
self.op = op
self.operand = operand
class ConditionalExpressionNode(ASTNode):
def __init__(self, condition, true_expr, false_expr):
self.condition = condition
self.true_expr = true_expr
self.false_expr = false_expr
class AssignmentNode(ASTNode):
def __init__(self, target, op, value):
self.target = target # VariableNode
self.op = op # '=', '+=', etc.
self.value = value
class VariableNode(ASTNode):
def __init__(self, name):
self.name = name
class ConstantNode(ASTNode):
def __init__(self, value):
self.value = value