Fixing declaration without variable

This commit is contained in:
tonitch 2025-03-20 00:52:05 +01:00
parent e7eca3b46d
commit 6f7829dadf
Signed by: tonitch
GPG Key ID: A78D6421F083D42E
4 changed files with 21 additions and 6 deletions

3
.gitignore vendored
View File

@ -1,4 +1,7 @@
__pycache__/
venv/
*.pyc
rapport.aux
rapport.fdb_latexmk
rapport.fls

View File

@ -2,6 +2,8 @@ texte nom = "anthony";
entier age = 23;
booléen majeur = vrai;
booléen ingénieur;
majeur = faux;
afficher nom, age, majeur;

View File

@ -33,7 +33,7 @@ class Variables:
def checkType(self, value, typ) -> bool:
return type(value) == self.types[typ]
return value is None or type(value) == self.types[typ]
def default(self, typ):
if typ == "entier":
@ -52,17 +52,27 @@ class Variables:
def get(self, name):
assert name in self.variables, "la variable {name} n'éxiste pas"
if self.trace:
print(f"{trace_format}accède {name}{reset_format}", file=sys.stdout)
print(f"{trace_format}accède {name}{reset_format}", file=sys.stderr)
return self.variables[name]
def declare(self, typ, name, value=None):
assert name not in self.variables, "la variable {name} existe déjà"
self.variables[name] = self.Variable(typ, value)
if self.trace:
print(f"{trace_format}déclare {name} = {value}{reset_format}", file=sys.stdout)
print(f"{trace_format}déclare {name} = {value}{reset_format}", file=sys.stderr)
def assign(self, name, value):
assert name in self.variables, "la variable n'éxiste pas"
self.variables[name].set(value)
if self.trace:
print(f"{trace_format}modifie {name} = {value}{reset_format}", file=sys.stdout)
print(f"{trace_format}modifie {name} = {value}{reset_format}", file=sys.stderr)
def dump(self):
name_len = max(map(len, self.variables.keys()))
var_len = max(map(len,map(str, self.variables.values())))
print(f"{'' * name_len}{'' * var_len}", file=sys.stderr)
print(f"{'Name':>{name_len}}{'Value':<{var_len}}", file=sys.stderr)
print(f"{'' * name_len}{'' * var_len}", file=sys.stderr)
for name, var in self.variables.items():
print(f"{name:>{name_len}}{str(var):<{var_len}}", file=sys.stderr)
print(f"{'' * name_len}{'' * var_len}", file=sys.stderr)

4
spf.py
View File

@ -22,7 +22,7 @@ class SPFInterpreter(lark.visitors.Interpreter):
def declaration(self, el):
type = el.children[0].value
name = el.children[1].value
value = self.visit_children(el.children[3])[0] if el.children[2].value == "=" else None
value = self.visit_children(el.children[3])[0] if len(el.children) >= 3 else None
self.variables.declare(type, name, value)
def assignation(self, el):
@ -70,7 +70,7 @@ def main():
interpreted = interpreter.visit(parsed)
if args.dump:
interpreted.dump()
interpreter.dump()
if __name__ == "__main__":