80 lines
3.0 KiB
Python
80 lines
3.0 KiB
Python
import sys
|
|
|
|
trace_format = '\033[1m -> '
|
|
reset_format = '\033[0m'
|
|
|
|
class Variables:
|
|
|
|
class Variable:
|
|
types = { "entier": int,
|
|
"texte": str,
|
|
"booléen": bool,
|
|
"liste": list }
|
|
|
|
def __init__(self, typ, value = None):
|
|
assert typ in self.types.keys(), "Ce type de variable est inconnu"
|
|
self.type = typ
|
|
assert self.checkType(value, typ), f"Le type n'est pas équivalent: { value } n'est pas {typ}"
|
|
self.value = value if (value is not None) else self.default(typ)
|
|
|
|
def set(self, value):
|
|
assert self.checkType(value, self.type), f"Le type n'est pas équivalent: {value} n'est pas {self.type}"
|
|
self.value = value
|
|
|
|
def __str__(self):
|
|
if self.type == "booléen":
|
|
return "Vrai" if self.value else "Faux"
|
|
return f"{self.value}"
|
|
|
|
def __repr__(self):
|
|
if self.type == "texte":
|
|
return f"\"{self.value}\""
|
|
return f"{self.value}"
|
|
|
|
def checkType(self, value, typ) -> bool:
|
|
return value is None or type(value) == self.types[typ]
|
|
|
|
def default(self, typ):
|
|
if typ == "entier":
|
|
return 0
|
|
if typ == "texte":
|
|
return ""
|
|
if typ == "booléen":
|
|
return False
|
|
if typ == "liste":
|
|
return []
|
|
|
|
def __init__(self, trace=False):
|
|
self.variables = {}
|
|
self.trace = trace
|
|
|
|
def get(self, name):
|
|
assert name in self.variables, f"la variable {name} n'éxiste pas"
|
|
if self.trace:
|
|
print(f"{trace_format}accède {name}{reset_format}", file=sys.stderr)
|
|
return self.variables[name].value
|
|
|
|
def declare(self, typ, name, value=None):
|
|
assert name not in self.variables, f"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.stderr)
|
|
|
|
def assign(self, name, value):
|
|
assert name in self.variables, f"la variable {name} n'éxiste pas"
|
|
self.variables[name].set(value)
|
|
if self.trace:
|
|
print(f"{trace_format}modifie {name} = {value}{reset_format}", file=sys.stderr)
|
|
|
|
def dump(self):
|
|
name_len = max(map(len, self.variables.keys()))
|
|
name_len = name_len if name_len >= len("name") else len("name")
|
|
var_len = max(map(len,map(str, self.variables.values())))
|
|
var_len = var_len if var_len >= len("value") else len("value")
|
|
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)
|