nodes: prepare Definition node

This commit is contained in:
Antoine Viallon 2023-05-23 00:54:38 +02:00
parent 81316ead45
commit 28ad2e2184
Signed by: aviallon
GPG key ID: D126B13AB555E16F

View file

@ -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