From 28ad2e2184465489ad4bb9d5335107fbb65a0e3d Mon Sep 17 00:00:00 2001 From: Antoine Viallon Date: Tue, 23 May 2023 00:54:38 +0200 Subject: [PATCH] nodes: prepare Definition node --- compiler/nodes.py | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/compiler/nodes.py b/compiler/nodes.py index 38614c1..5f24e40 100644 --- a/compiler/nodes.py +++ b/compiler/nodes.py @@ -317,6 +317,7 @@ class Variable(Node): class Assignment(Node): def intermediate_representation(self) -> list[ir.IRItem]: assert self.variable is not None + assert self.value is not None result: list[ir.IRItem] = [] value = self.value.intermediate_representation() @@ -324,10 +325,10 @@ class Assignment(Node): dest = ir.IRVariable(location=self.location(), fq_identifier=self.variable.fully_qualified_name()) result += [ir.IRMove(location=self.location(), dest=dest.destination(), source=value[-1].destination())] - + return result - def __init__(self, identifier: Identifier, value: Value): + def __init__(self, identifier: Identifier, value: Value | None): super().__init__() self.identifier = identifier self.value = value @@ -341,8 +342,34 @@ class Assignment(Node): name = self.identifier.value variable = context.set_variable(name, value=self.value) self.variable = variable - logger.debug(f"Added variable {variable} to context {context.fully_qualified_name()}") + logger.debug(f"Assigned variable {variable.fully_qualified_name()}") + + +class Definition(Assignment): + + def __init__(self, identifier: Identifier, type_identifier: Identifier, value: Value | None, + *pseudo_nodes: PseudoNode): + super().__init__(identifier, value) + self.type_identifier = type_identifier + self.pseudo_nodes = list(pseudo_nodes) + + def intermediate_representation(self) -> list[ir.IRItem]: + if self.value is None: + return [ir.IRVoid(self.location())] + + return super(Definition, self).intermediate_representation() + + def _values(self) -> list[Node | Any]: + v = [self.identifier, self.type_identifier] + if self.value is not None: + v += [self.value] + v += self.pseudo_nodes + return v + + @property + def pure(self) -> bool: + return self.value is None Number = Float | Integer -Value = BinaryOperation | Number | Variable +Value = BinaryOperation | Number | Variable | Expression