semantic: fix pretty printing + allow adding child contexts
This commit is contained in:
parent
c656a98d3e
commit
b7ba27f29d
1 changed files with 28 additions and 5 deletions
|
|
@ -11,12 +11,21 @@ class Variable:
|
||||||
self.context = context
|
self.context = context
|
||||||
self.name = name
|
self.name = name
|
||||||
self.definitions = [value]
|
self.definitions = [value]
|
||||||
|
self._repr_guard: bool = False
|
||||||
|
|
||||||
def fully_qualified_name(self) -> str:
|
def fully_qualified_name(self) -> str:
|
||||||
return f"{self.context.fully_qualified_name()}.{self.name}"
|
return f"{self.context.fully_qualified_name()}.{self.name}"
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return f"{self.__class__.__name__}({self.name})"
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return f"{self.__class__.__name__}({self.name}) [definitions: {', '.join(repr(d) for d in self.definitions)}]"
|
if self._repr_guard:
|
||||||
|
return str(self)
|
||||||
|
self._repr_guard = True
|
||||||
|
definitions = [str(d.location().begin) for d in self.definitions]
|
||||||
|
self._repr_guard = False
|
||||||
|
return f"{str(self)} [definitions: {', '.join(definitions)}]"
|
||||||
|
|
||||||
|
|
||||||
class Context:
|
class Context:
|
||||||
|
|
@ -25,6 +34,7 @@ class Context:
|
||||||
def __init__(self, name: str | None = None, parent: Context | None = None):
|
def __init__(self, name: str | None = None, parent: Context | None = None):
|
||||||
self.parent = parent
|
self.parent = parent
|
||||||
self.variables: dict[str, Variable] = {}
|
self.variables: dict[str, Variable] = {}
|
||||||
|
self.child_contexts: dict[str, Context] = {}
|
||||||
self.name = str(Context._id_sequence)
|
self.name = str(Context._id_sequence)
|
||||||
if name is not None:
|
if name is not None:
|
||||||
self.name = f"{name}_{Context._id_sequence}"
|
self.name = f"{name}_{Context._id_sequence}"
|
||||||
|
|
@ -45,6 +55,9 @@ class Context:
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def add_context(self, context: Context) -> None:
|
||||||
|
self.child_contexts[context.name] = context
|
||||||
|
|
||||||
def set_variable(self, name: str, value: nodes.Value) -> Variable:
|
def set_variable(self, name: str, value: nodes.Value) -> Variable:
|
||||||
variable: Variable
|
variable: Variable
|
||||||
if name in self.variables:
|
if name in self.variables:
|
||||||
|
|
@ -57,13 +70,23 @@ class Context:
|
||||||
|
|
||||||
return variable
|
return variable
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __str__(self):
|
||||||
result = [f"{self.__class__.__name__}(id={self.name})"]
|
return f"{self.__class__.__name__}(id={repr(self.name)})"
|
||||||
|
|
||||||
|
def _pprint(self, depth: int = 0) -> str:
|
||||||
|
result = [str(self)]
|
||||||
if self.parent is not None:
|
if self.parent is not None:
|
||||||
result += [f"\tParent ID: {self.parent.name}"]
|
result += [f"\tParent ID: {self.parent.name}"]
|
||||||
if len(self.variables) > 0:
|
if len(self.variables) > 0:
|
||||||
result += [f"\tVariables ({len(self.variables)}):"]
|
result += [f"\tVariables ({len(self.variables)}):"]
|
||||||
for key, value in self.variables.items():
|
for key, value in self.variables.items():
|
||||||
definitions = (repr(e) for e in value.definitions)
|
result += [f"\t\t- {repr(value)}"]
|
||||||
result += [f"\t\t- {repr(key)} : {', '.join(definitions)}"]
|
if len(self.child_contexts) > 0:
|
||||||
|
result += [f"\tChild contexts ({len(self.child_contexts)}):"]
|
||||||
|
for key, value in self.child_contexts.items():
|
||||||
|
ctx_repr = value._pprint(depth=depth + 1).splitlines(keepends=False)
|
||||||
|
result += [f"\t\t{line}" for line in ctx_repr]
|
||||||
return "\n".join(result)
|
return "\n".join(result)
|
||||||
|
|
||||||
|
def __repr__(self) -> str:
|
||||||
|
return self._pprint()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue