nodes: rework variable and fix its intermediate representation

This commit is contained in:
Antoine Viallon 2023-05-12 01:27:16 +02:00
parent 15e8c2bee3
commit 4929efa7b0
Signed by: aviallon
GPG key ID: D126B13AB555E16F
2 changed files with 13 additions and 5 deletions

View file

@ -244,11 +244,15 @@ class Identifier(Literal):
self.value: str
class Variable(Literal):
def __init__(self, location: SourceLocation, identifier: Identifier):
super().__init__(location, None)
class Variable(Node):
def __init__(self, identifier: Identifier):
super().__init__()
self.value: semantic.Variable | None = None
self.identifier = identifier
def _values(self) -> list[Node | Any]:
return [self.identifier]
def semantic_analysis(self, context: semantic.Context):
variable = context.get_variable(self.identifier.value)
if variable is None:
@ -258,7 +262,11 @@ class Variable(Literal):
logger.debug(f"Linked variable reference to var {variable}")
def intermediate_representation(self) -> list[ir.IRItem]:
result = [ir.IRVariable(location=self.location(), fq_identifier=self.value.fully_qualified_name())]
assert self.value is not None
dest = ir.IRRegister(location=self.location())
immediate = ir.IRVariable(location=self.location(), fq_identifier=self.value.fully_qualified_name())
result = [ir.IRMove(location=self.location(), dest=dest, source=immediate)]
return result

View file

@ -86,7 +86,7 @@ class Parser:
@tracer.trace_method
def variable(self, mandatory: bool = False) -> Variable:
if ident := self.identifier(mandatory=False):
return Variable(location=ident.location(), identifier=ident)
return Variable(identifier=ident)
elif mandatory:
raise UnexpectedTokenError(self.token, "variable identifier")