optimized SyntaxTree class
This commit is contained in:
parent
fb51295974
commit
639546d80c
2 changed files with 15 additions and 3 deletions
|
|
@ -605,7 +605,7 @@ def parse(
|
||||||
name=name,
|
name=name,
|
||||||
custom_function_names=custom_function_names,
|
custom_function_names=custom_function_names,
|
||||||
variable_names=variable_names,
|
variable_names=variable_names,
|
||||||
).ast
|
).ast.maybe_resolvable_casted()
|
||||||
|
|
||||||
|
|
||||||
# pylint: enable=invalid-name
|
# pylint: enable=invalid-name
|
||||||
|
|
|
||||||
|
|
@ -47,13 +47,25 @@ class SyntaxTree(VariableDependency):
|
||||||
-------
|
-------
|
||||||
A resolvable if the AST contains a single type that is resolvable. None otherwise.
|
A resolvable if the AST contains a single type that is resolvable. None otherwise.
|
||||||
"""
|
"""
|
||||||
if len(self.ast) == 1 and isinstance(self.ast[0], Resolvable):
|
|
||||||
return self.ast[0]
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def maybe_resolvable_casted(self) -> "SyntaxTree":
|
||||||
|
"""
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
Optimized SyntaxTree if its deemed resolvable
|
||||||
|
"""
|
||||||
|
if len(self.ast) == 1 and isinstance(self.ast[0], Resolvable):
|
||||||
|
return ResolvedSyntaxTree(self.ast)
|
||||||
|
return self
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
@dataclass(frozen=True)
|
||||||
class ResolvedSyntaxTree(SyntaxTree):
|
class ResolvedSyntaxTree(SyntaxTree):
|
||||||
|
"""
|
||||||
|
SyntaxTree with optimized helper functions if it's known to be resolved.
|
||||||
|
"""
|
||||||
|
|
||||||
def resolve(
|
def resolve(
|
||||||
self,
|
self,
|
||||||
resolved_variables: Dict[Variable, Resolvable],
|
resolved_variables: Dict[Variable, Resolvable],
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue