nodes: rework variable and fix its intermediate representation
This commit is contained in:
parent
15e8c2bee3
commit
4929efa7b0
2 changed files with 13 additions and 5 deletions
|
|
@ -244,11 +244,15 @@ class Identifier(Literal):
|
||||||
self.value: str
|
self.value: str
|
||||||
|
|
||||||
|
|
||||||
class Variable(Literal):
|
class Variable(Node):
|
||||||
def __init__(self, location: SourceLocation, identifier: Identifier):
|
def __init__(self, identifier: Identifier):
|
||||||
super().__init__(location, None)
|
super().__init__()
|
||||||
|
self.value: semantic.Variable | None = None
|
||||||
self.identifier = identifier
|
self.identifier = identifier
|
||||||
|
|
||||||
|
def _values(self) -> list[Node | Any]:
|
||||||
|
return [self.identifier]
|
||||||
|
|
||||||
def semantic_analysis(self, context: semantic.Context):
|
def semantic_analysis(self, context: semantic.Context):
|
||||||
variable = context.get_variable(self.identifier.value)
|
variable = context.get_variable(self.identifier.value)
|
||||||
if variable is None:
|
if variable is None:
|
||||||
|
|
@ -258,7 +262,11 @@ class Variable(Literal):
|
||||||
logger.debug(f"Linked variable reference to var {variable}")
|
logger.debug(f"Linked variable reference to var {variable}")
|
||||||
|
|
||||||
def intermediate_representation(self) -> list[ir.IRItem]:
|
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
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -86,7 +86,7 @@ class Parser:
|
||||||
@tracer.trace_method
|
@tracer.trace_method
|
||||||
def variable(self, mandatory: bool = False) -> Variable:
|
def variable(self, mandatory: bool = False) -> Variable:
|
||||||
if ident := self.identifier(mandatory=False):
|
if ident := self.identifier(mandatory=False):
|
||||||
return Variable(location=ident.location(), identifier=ident)
|
return Variable(identifier=ident)
|
||||||
elif mandatory:
|
elif mandatory:
|
||||||
raise UnexpectedTokenError(self.token, "variable identifier")
|
raise UnexpectedTokenError(self.token, "variable identifier")
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue