diff --git a/.gitignore b/.gitignore index 6e7b779..95e724d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,7 @@ +__pycache__/ venv/ +*.pyc + rapport.aux rapport.fdb_latexmk rapport.fls diff --git a/examples/simple.spf b/examples/simple.spf index 61f4245..55cecf7 100644 --- a/examples/simple.spf +++ b/examples/simple.spf @@ -2,6 +2,8 @@ texte nom = "anthony"; entier age = 23; booléen majeur = vrai; +booléen ingénieur; + majeur = faux; afficher nom, age, majeur; diff --git a/modules/Variables.py b/modules/Variables.py index 08a073c..c3ff847 100644 --- a/modules/Variables.py +++ b/modules/Variables.py @@ -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) diff --git a/spf.py b/spf.py index d076365..2c877d4 100755 --- a/spf.py +++ b/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__":