Skip to content

Commit

Permalink
feat: add accessors for NTuple and Object (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jmgr authored Nov 18, 2024
1 parent c9ad05a commit b7959ee
Show file tree
Hide file tree
Showing 10 changed files with 277 additions and 92 deletions.
44 changes: 44 additions & 0 deletions nada_dsl/ast_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,3 +376,47 @@ def to_mir(self):
"source_ref_index": self.source_ref.to_index(),
}
}


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

index: int
source: int

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

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


@dataclass
class ObjectAccessorASTOperation(ASTOperation):
"""AST representation of an object accessor operation."""

key: str
source: int

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

def to_mir(self):
return {
"ObjectAccessor": {
"id": self.id,
"key": self.key,
"source": self.source,
"type": self.ty,
"source_ref_index": self.source_ref.to_index(),
}
}
4 changes: 4 additions & 0 deletions nada_dsl/compiler_frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
InputASTOperation,
LiteralASTOperation,
MapASTOperation,
NTupleAccessorASTOperation,
NadaFunctionASTOperation,
NadaFunctionArgASTOperation,
NadaFunctionCallASTOperation,
NewASTOperation,
ObjectAccessorASTOperation,
RandomASTOperation,
ReduceASTOperation,
UnaryASTOperation,
Expand Down Expand Up @@ -296,6 +298,8 @@ def process_operation(
NewASTOperation,
RandomASTOperation,
NadaFunctionArgASTOperation,
NTupleAccessorASTOperation,
ObjectAccessorASTOperation,
),
):
processed_operation = ProcessOperationOutput(operation.to_mir(), None)
Expand Down
8 changes: 4 additions & 4 deletions nada_dsl/nada_types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,10 @@ def __init__(self, child: OperationType):

def to_mir(self):
"""Default implementation for the Conversion of a type into MIR representation."""
return self.__class__.class_to_type()
return self.__class__.class_to_mir()

@classmethod
def class_to_type(cls) -> str:
def class_to_mir(cls) -> str:
"""Converts a class into a MIR Nada type."""
name = cls.__name__
# Rename public variables so they are considered as the same as literals.
Expand All @@ -165,8 +165,8 @@ def __bool__(self):
raise NotImplementedError

@classmethod
def is_scalable(cls) -> bool:
"""Returns True if the type is a scalable."""
def is_scalar(cls) -> bool:
"""Returns True if the type is a scalar."""
return False

@classmethod
Expand Down
Loading

0 comments on commit b7959ee

Please sign in to comment.