-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharbre_abstrait.py
216 lines (157 loc) · 5.67 KB
/
arbre_abstrait.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
"""
Affiche une chaine de caractère avec une certaine identation
"""
def afficher(s, indent=0):
print(" " * indent + s)
class Program:
def __init__(self, listeInstructions, listeFunctions=None):
self.listeInstructions = listeInstructions
self.listeFunctions = listeFunctions
def afficher(self, indent=0):
afficher("<program>", indent)
if self.listeFunctions:
self.listeFunctions.afficher(indent + 1)
self.listeInstructions.afficher(indent + 1)
afficher("</program>", indent)
class Instructions:
def __init__(self, instructions=None):
self.instructions = instructions if instructions else []
def afficher(self, indent=0):
afficher("<instructions>", indent)
for instruction in self.instructions:
instruction.afficher(indent + 1)
afficher("</instructions>", indent)
class Functions:
def __init__(self):
self.functions = []
def afficher(self, indent=0):
afficher("<functions>", indent)
for function in self.functions:
function.afficher(indent + 1)
afficher("</functions>", indent)
def append(self, expr):
self.functions.append(expr)
class Function:
def __init__(self, fct, args=None):
self.fct = fct
self.args = args
def afficher(self, indent=0):
afficher(f"<function name=\"{self.fct}\"{('' if self.args else '/')}>", indent)
if self.args:
self.args.afficher(indent + 1)
afficher("</function>", indent)
class Return:
def __init__(self, exp):
self.exp = exp
def afficher(self, indent=0):
afficher(f"<return>", indent)
self.exp.afficher(indent + 1)
afficher("</return>", indent)
class Args:
def __init__(self):
self.listArgs = []
def afficher(self, indent=0):
afficher(f"<arguments>", indent)
for arg in self.listArgs:
arg.afficher(indent + 1)
afficher("</arguments>", indent)
def append(self, expr):
self.listArgs.append(expr)
class Declaration:
def __init__(self, type, name, value=None):
self.type = type
self.name = name
self.value = value
def afficher(self, indent=0):
afficher(f"<declaration name=\"{self.name}\" type=\"{self.type}\"{('' if self.value else '/')}>", indent)
if self.value:
self.value.afficher(indent + 1)
afficher("</declaration>", indent)
class DeclarationFunction:
def __init__(self, type, name, instructions, declarationArgs=None):
self.type = type
self.name = name
self.instructions = instructions
self.declarationArgs = declarationArgs
def afficher(self, indent=0):
afficher(f'<func_declaration name="{self.name}" type="{self.type}">', indent)
if self.declarationArgs:
self.declarationArgs.afficher(indent + 1)
self.instructions.afficher(indent + 1)
afficher("</func_declaration>", indent)
def append(self, expr):
self.declarationArgs.append(expr)
class ArgsDeclaration:
def __init__(self):
self.declarations = []
def afficher(self, indent=0):
afficher(f"<args_declaration>", indent)
for i in range(len(self.declarations)):
self.declarations[i].afficher(indent + 1)
afficher("</args_declaration>", indent)
def append(self, expr):
self.declarations.append(expr)
class Assignment:
def __init__(self, variable, value):
self.variable = variable
self.value = value
def afficher(self, indent=0):
afficher(f'<assignment name="{self.variable}">', indent)
self.value.afficher(indent + 1)
afficher("</assignment>", indent)
class Operation:
def __init__(self, op, exp1, exp2=None):
self.exp1 = exp1
self.op = op
self.exp2 = exp2
def afficher(self, indent=0):
afficher(f'<operation operator="{self.op}">', indent)
self.exp1.afficher(indent + 1)
if self.exp2:
self.exp2.afficher(indent + 1)
afficher("</operation>", indent)
class While:
def __init__(self, cond, instructions):
self.cond = cond
self.instructions = instructions
def afficher(self, indent=0):
afficher(f"<while>", indent)
self.cond.afficher(indent + 1)
self.instructions.afficher(indent + 1)
afficher("</while>", indent)
class If:
def __init__(self, cond, instructions, elseInstruction=None):
self.cond = cond
self.instructions = instructions
self.elseInstruction = elseInstruction
def afficher(self, indent=0):
afficher(f"<if>", indent)
self.cond.afficher(indent + 1)
self.instructions.afficher(indent + 1)
if self.elseInstruction:
self.elseInstruction.afficher(indent + 1)
afficher("</if>", indent)
class Else:
def __init__(self, instructions):
self.instructions = instructions
def afficher(self, indent=0):
afficher(f"<else>", indent)
self.instructions.afficher(indent + 1)
afficher("</else>", indent)
class Integer:
DEFAULT_VALUE = 0
def __init__(self, valeur):
self.valeur = valeur
def afficher(self, indent=0):
afficher(f'<integer value="{str(self.valeur)}"/>', indent)
class Variable:
def __init__(self, valeur):
self.valeur = valeur
def afficher(self, indent=0):
afficher(f'<variable value="{str(self.valeur)}"/>', indent)
class Boolean:
DEFAULT_VALUE = 0
def __init__(self, valeur):
self.valeur = valeur
def afficher(self, indent=0):
afficher(f'<boolean value="{str(self.valeur)}"/>', indent)