🐐 Unpacking arrays
This commit is contained in:
@@ -431,6 +431,35 @@ class DesignaIndex(Node):
|
||||
return vtable, ValNul()
|
||||
|
||||
|
||||
class DesignaDestructure(Node):
|
||||
def __init__(self, variables: list, value: Node) -> None:
|
||||
self.ids = variables
|
||||
self.value = value
|
||||
|
||||
def __eq__(self, other):
|
||||
return type(self) == type(other) and self.ids == other.ids and self.value == other.value
|
||||
|
||||
def __repr__(self) -> str:
|
||||
ids_string = ", ".join(repr(i) for i in self.ids)
|
||||
value_string = repr(self.value).replace('\n', '\n ')
|
||||
return f"DesignaDestructure(\n [{ids_string}],\n {value_string}\n)"
|
||||
|
||||
def print(self):
|
||||
ids_str = ", ".join(i.print() for i in self.ids)
|
||||
return f"DESIGNA {ids_str} VT {self.value.print()}"
|
||||
|
||||
def _eval(self, vtable):
|
||||
vtable, val = self.value.eval(vtable)
|
||||
if not isinstance(val, ValList):
|
||||
raise CentvrionError("Cannot destructure non-array value")
|
||||
if len(val.value()) != len(self.ids):
|
||||
raise CentvrionError(
|
||||
f"Destructuring mismatch: {len(self.ids)} targets, {len(val.value())} values")
|
||||
for id_node, item in zip(self.ids, val.value()):
|
||||
vtable[id_node.name] = item
|
||||
return vtable, ValNul()
|
||||
|
||||
|
||||
class Defini(Node):
|
||||
def __init__(self, name: ID, parameters: list[ID], statements: list[Node]) -> None:
|
||||
self.name = name
|
||||
|
||||
Reference in New Issue
Block a user