Skip to content

Commit

Permalink
feat: add accessor for Tuple
Browse files Browse the repository at this point in the history
  • Loading branch information
Jmgr committed Nov 29, 2024
1 parent 87a1965 commit a48bafb
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 4 deletions.
22 changes: 22 additions & 0 deletions nada_dsl/ast_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,28 @@ def to_mir(self):
}


@dataclass
class TupleAccessorASTOperation(ASTOperation):
"""AST representation of a tuple accessor operation."""

index: int
source: int

def child_operations(self):
return [self.source]

def to_mir(self):
return {
"TupleAccessor": {
"id": self.id,
"index": self.index,
"source": self.source,
"type": self.ty,
"source_ref_index": self.source_ref.to_index(),
}
}


@dataclass
class NTupleAccessorASTOperation(ASTOperation):
"""AST representation of a n tuple accessor operation."""
Expand Down
2 changes: 2 additions & 0 deletions nada_dsl/compiler_frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
InputASTOperation,
LiteralASTOperation,
MapASTOperation,
TupleAccessorASTOperation,
NTupleAccessorASTOperation,
NadaFunctionASTOperation,
NadaFunctionArgASTOperation,
Expand Down Expand Up @@ -298,6 +299,7 @@ def process_operation(
NewASTOperation,
RandomASTOperation,
NadaFunctionArgASTOperation,
TupleAccessorASTOperation,
NTupleAccessorASTOperation,
ObjectAccessorASTOperation,
),
Expand Down
59 changes: 55 additions & 4 deletions nada_dsl/nada_types/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
AST_OPERATIONS,
BinaryASTOperation,
MapASTOperation,
TupleAccessorASTOperation,
NTupleAccessorASTOperation,
NewASTOperation,
ObjectAccessorASTOperation,
Expand Down Expand Up @@ -130,6 +131,12 @@ def to_mir(self):
}


def _generate_accessor(ty: Any, accessor: Any) -> DslType:
if hasattr(ty, "ty") and ty.ty.is_literal(): # TODO: fix
raise TypeError("Literals are not supported in accessors")
return ty.instantiate(accessor)


@dataclass
class Tuple(Generic[T, U], DslType):
"""The Tuple type"""
Expand Down Expand Up @@ -160,15 +167,59 @@ def generic_type(cls, left_type: U, right_type: T) -> TupleType:
"""Returns the generic type for this Tuple"""
return TupleType(left_type=left_type, right_type=right_type)

@property
def left(self) -> DslType:
accessor = TupleAccessor(
index=0,
child=self,
source_ref=SourceRef.back_frame(),
)

return _generate_accessor(self.left_type, accessor)

@property
def right(self) -> DslType:
accessor = TupleAccessor(
index=1,
child=self,
source_ref=SourceRef.back_frame(),
)

return _generate_accessor(self.right_type, accessor)

def type(self):
"""Metatype for Tuple"""
return TupleType(self.left_type, self.right_type)


def _generate_accessor(ty: Any, accessor: Any) -> DslType:
if hasattr(ty, "ty") and ty.ty.is_literal(): # TODO: fix
raise TypeError("Literals are not supported in accessors")
return ty.instantiate(accessor)
@dataclass
class TupleAccessor:
"""Accessor for Tuple"""

child: Tuple
index: int
source_ref: SourceRef

def __init__(
self,
child: Tuple,
index: int,
source_ref: SourceRef,
):
self.id = next_operation_id()
self.child = child
self.index = index
self.source_ref = source_ref

def store_in_ast(self, ty: object):
"""Store this accessor in the AST."""
AST_OPERATIONS[self.id] = TupleAccessorASTOperation(
id=self.id,
source=self.child.child.id,
index=self.index,
source_ref=self.source_ref,
ty=ty,
)


class NTupleType(NadaType):
Expand Down

0 comments on commit a48bafb

Please sign in to comment.