Fixing declaration without variable
This commit is contained in:
parent
e7eca3b46d
commit
6f7829dadf
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,4 +1,7 @@
|
||||
__pycache__/
|
||||
venv/
|
||||
*.pyc
|
||||
|
||||
rapport.aux
|
||||
rapport.fdb_latexmk
|
||||
rapport.fls
|
||||
|
@ -2,6 +2,8 @@ texte nom = "anthony";
|
||||
entier age = 23;
|
||||
booléen majeur = vrai;
|
||||
|
||||
booléen ingénieur;
|
||||
|
||||
majeur = faux;
|
||||
|
||||
afficher nom, age, majeur;
|
||||
|
@ -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
4
spf.py
@ -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__":
|
||||
|
Loading…
x
Reference in New Issue
Block a user