From 4929efa7b05bbe8040d63f5f1b4e9c8a122363c1 Mon Sep 17 00:00:00 2001 From: Antoine Viallon Date: Fri, 12 May 2023 01:27:16 +0200 Subject: [PATCH] nodes: rework variable and fix its intermediate representation --- compiler/nodes.py | 16 ++++++++++++---- compiler/parser.py | 2 +- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/compiler/nodes.py b/compiler/nodes.py index 941ed9a..e5a6b2f 100644 --- a/compiler/nodes.py +++ b/compiler/nodes.py @@ -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 diff --git a/compiler/parser.py b/compiler/parser.py index 62077d1..ff967b7 100644 --- a/compiler/parser.py +++ b/compiler/parser.py @@ -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")