Skip to content

Commit

Permalink
chore: rename NadaType to DslType, rename MetaType suffix to Type, re…
Browse files Browse the repository at this point in the history
…name BooleanType to AbstractBooleanType
  • Loading branch information
Jmgr committed Nov 26, 2024
1 parent 0822056 commit 3694709
Show file tree
Hide file tree
Showing 8 changed files with 329 additions and 254 deletions.
13 changes: 7 additions & 6 deletions nada_dsl/nada_types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,14 @@ def is_numeric(self) -> bool:
return self in (BaseType.INTEGER, BaseType.UNSIGNED_INTEGER)


# TODO: make this abstract?
@dataclass
class NadaType:
"""Nada type class.
class DslType:
"""DSL type class.
This is the parent class of all nada types.
This is the parent class of all DSL types.
In Nada, all the types wrap Operations. For instance, an addition between two integers
In the DSL, all the types wrap Operations. For instance, an addition between two integers
is represented like this SecretInteger(child=Addition(...)).
In MIR, the representation is based around operations. A MIR operation points to other
Expand All @@ -145,7 +146,7 @@ def __init__(self, child: OperationType):
"""
self.child = child
if self.child is not None:
self.child.store_in_ast(self.metatype().to_mir())
self.child.store_in_ast(self.type().to_mir())

def __bool__(self):
raise NotImplementedError
Expand All @@ -161,5 +162,5 @@ def is_literal(cls) -> bool:
return False

@abstractmethod
def metatype(self):
def type(self):
"""Returns a meta type for this NadaType."""
96 changes: 48 additions & 48 deletions nada_dsl/nada_types/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
ReduceASTOperation,
UnaryASTOperation,
)
from nada_dsl.nada_types import NadaType
from nada_dsl.nada_types import DslType

# Wildcard import due to non-zero types
from nada_dsl.nada_types.scalar_types import * # pylint: disable=W0614:wildcard-import
Expand Down Expand Up @@ -108,12 +108,12 @@ def store_in_ast(self, ty):
)


class TupleMetaType(MetaType):
class TupleType(DslType):
"""Marker type for Tuples."""

is_compound = True

def __init__(self, left_type: MetaType, right_type: MetaType):
def __init__(self, left_type: DslType, right_type: DslType):
self.left_type = left_type
self.right_type = right_type

Expand All @@ -131,7 +131,7 @@ def to_mir(self):


@dataclass
class Tuple(Generic[T, U], NadaType):
class Tuple(Generic[T, U], DslType):
"""The Tuple type"""

left_type: T
Expand All @@ -144,39 +144,39 @@ def __init__(self, child, left_type: T, right_type: U):
super().__init__(self.child)

@classmethod
def new(cls, left_value: NadaType, right_value: NadaType) -> "Tuple[T, U]":
def new(cls, left_value: DslType, right_value: DslType) -> "Tuple[T, U]":
"""Constructs a new Tuple."""
return Tuple(
left_type=left_value.metatype(),
right_type=right_value.metatype(),
left_type=left_value.type(),
right_type=right_value.type(),
child=TupleNew(
child=(left_value, right_value),
source_ref=SourceRef.back_frame(),
),
)

@classmethod
def generic_type(cls, left_type: U, right_type: T) -> TupleMetaType:
def generic_type(cls, left_type: U, right_type: T) -> TupleType:
"""Returns the generic type for this Tuple"""
return TupleMetaType(left_type=left_type, right_type=right_type)
return TupleType(left_type=left_type, right_type=right_type)

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


def _generate_accessor(ty: Any, accessor: Any) -> NadaType:
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)


class NTupleMetaType(MetaType):
class NTupleType(DslType):
"""Marker type for NTuples."""

is_compound = True

def __init__(self, types: List[MetaType]):
def __init__(self, types: List[DslType]):
self.types = types

def instantiate(self, child_or_value):
Expand All @@ -192,7 +192,7 @@ def to_mir(self):


@dataclass
class NTuple(NadaType):
class NTuple(DslType):
"""The NTuple type"""

types: List[Any]
Expand All @@ -205,7 +205,7 @@ def __init__(self, child, types: List[Any]):
@classmethod
def new(cls, values: List[Any]) -> "NTuple":
"""Constructs a new NTuple."""
types = [value.metatype() for value in values]
types = [value.type() for value in values]
return NTuple(
types=types,
child=NTupleNew(
Expand All @@ -214,7 +214,7 @@ def new(cls, values: List[Any]) -> "NTuple":
),
)

def __getitem__(self, index: int) -> NadaType:
def __getitem__(self, index: int) -> DslType:
if index >= len(self.types):
raise IndexError(f"Invalid index {index} for NTuple.")

Expand All @@ -226,9 +226,9 @@ def __getitem__(self, index: int) -> NadaType:

return _generate_accessor(self.types[index], accessor)

def metatype(self):
"""Metatype for NTuple"""
return NTupleMetaType(self.types)
def type(self):
"""Type for NTuple"""
return NTupleType(self.types)


@dataclass
Expand Down Expand Up @@ -261,12 +261,12 @@ def store_in_ast(self, ty: object):
)


class ObjectMetaType(MetaType):
class ObjectType(DslType):
"""Marker type for Objects."""

is_compound = True

def __init__(self, types: Dict[str, MetaType]):
def __init__(self, types: Dict[str, DslType]):
self.types = types

def to_mir(self):
Expand All @@ -280,7 +280,7 @@ def instantiate(self, child_or_value):


@dataclass
class Object(NadaType):
class Object(DslType):
"""The Object type"""

types: Dict[str, Any]
Expand All @@ -293,7 +293,7 @@ def __init__(self, child, types: Dict[str, Any]):
@classmethod
def new(cls, values: Dict[str, Any]) -> "Object":
"""Constructs a new Object."""
types = {key: value.metatype() for key, value in values.items()}
types = {key: value.type() for key, value in values.items()}
return Object(
types=types,
child=ObjectNew(
Expand All @@ -302,7 +302,7 @@ def new(cls, values: Dict[str, Any]) -> "Object":
),
)

def __getattr__(self, attr: str) -> NadaType:
def __getattr__(self, attr: str) -> DslType:
if attr not in self.types:
raise AttributeError(
f"'{self.__class__.__name__}' object has no attribute '{attr}'"
Expand All @@ -316,9 +316,9 @@ def __getattr__(self, attr: str) -> NadaType:

return _generate_accessor(self.types[attr], accessor)

def metatype(self):
"""Metatype for Object"""
return ObjectMetaType(types=self.types)
def type(self):
"""Type for Object"""
return ObjectType(types=self.types)


@dataclass
Expand Down Expand Up @@ -412,7 +412,7 @@ def store_in_ast(self, ty: NadaTypeRepr):
)


class ArrayMetaType(MetaType):
class ArrayType(DslType):
"""Marker type for arrays."""

is_compound = True
Expand All @@ -428,7 +428,7 @@ def to_mir(self):
# and apply the same logic when the function gets passed to .map() or .reduce()
# so we now the size of the array
if self.size is None:
raise NotImplementedError("ArrayMetaType.to_mir")
raise NotImplementedError("ArrayType.to_mir")
return {
"Array": {
"inner_type": self.contained_type.to_mir(),
Expand All @@ -441,7 +441,7 @@ def instantiate(self, child_or_value):


@dataclass
class Array(Generic[T], NadaType):
class Array(Generic[T], DslType):
"""Nada Array type.
This is the representation of arrays in Nada MIR.
Expand All @@ -461,14 +461,14 @@ class Array(Generic[T], NadaType):
size: int

def __init__(self, child, size: int, contained_type: T = None):
self.contained_type = contained_type or child.metatype()
self.contained_type = contained_type or child.type()

self.size = size
self.child = (
child if contained_type is not None else getattr(child, "child", None)
)
if self.child is not None:
self.child.store_in_ast(self.metatype().to_mir())
self.child.store_in_ast(self.type().to_mir())

def __iter__(self):
raise NotAllowedException(
Expand All @@ -495,9 +495,9 @@ def map(self: "Array[T]", function) -> "Array":
def reduce(self: "Array[T]", function, initial: R) -> R:
"""The Reduce operation for arrays."""
self.check_not_constant(self.contained_type)
self.check_not_constant(initial.metatype())
self.check_not_constant(initial.type())
function = create_nada_fn(
function, args_ty=[initial.metatype(), self.contained_type]
function, args_ty=[initial.type(), self.contained_type]
)
return function.return_type.instantiate(
Reduce(
Expand All @@ -514,7 +514,7 @@ def zip(self: "Array[T]", other: "Array[U]") -> "Array[Tuple[T, U]]":
raise IncompatibleTypesError("Cannot zip arrays of different size")
return Array(
size=self.size,
contained_type=TupleMetaType(
contained_type=TupleType(
left_type=self.contained_type,
right_type=other.contained_type,
),
Expand Down Expand Up @@ -551,17 +551,17 @@ def new(cls, *args) -> "Array[T]":
raise TypeError("All arguments must be of the same type")

return Array(
contained_type=first_arg.metatype(),
contained_type=first_arg.type(),
size=len(args),
child=ArrayNew(
child=args,
source_ref=SourceRef.back_frame(),
),
)

def metatype(self):
"""Metatype for Array"""
return ArrayMetaType(self.contained_type, self.size)
def type(self):
"""Type for Array"""
return ArrayType(self.contained_type, self.size)


@dataclass
Expand Down Expand Up @@ -597,10 +597,10 @@ class NTupleNew:
Represents the creation of a new Tuple.
"""

child: List[NadaType]
child: List[DslType]
source_ref: SourceRef

def __init__(self, child: List[NadaType], source_ref: SourceRef):
def __init__(self, child: List[DslType], source_ref: SourceRef):
self.id = next_operation_id()
self.child = child
self.source_ref = source_ref
Expand All @@ -623,10 +623,10 @@ class ObjectNew:
Represents the creation of a new Object.
"""

child: Dict[str, NadaType]
child: Dict[str, DslType]
source_ref: SourceRef

def __init__(self, child: Dict[str, NadaType], source_ref: SourceRef):
def __init__(self, child: Dict[str, DslType], source_ref: SourceRef):
self.id = next_operation_id()
self.child = child
self.source_ref = source_ref
Expand All @@ -644,10 +644,10 @@ def store_in_ast(self, ty: object):

def unzip(array: Array[Tuple[T, R]]) -> Tuple[Array[T], Array[R]]:
"""The Unzip operation for Arrays."""
right_type = ArrayMetaType(
right_type = ArrayType(
contained_type=array.contained_type.right_type, size=array.size
)
left_type = ArrayMetaType(
left_type = ArrayType(
contained_type=array.contained_type.left_type, size=array.size
)

Expand All @@ -670,7 +670,7 @@ def __init__(self, child: List[T], source_ref: SourceRef):
self.child = child
self.source_ref = source_ref

def store_in_ast(self, ty: NadaType):
def store_in_ast(self, ty: DslType):
"""Store this ArrayNew object in the AST."""
AST_OPERATIONS[self.id] = NewASTOperation(
id=self.id,
Expand Down
10 changes: 5 additions & 5 deletions nada_dsl/nada_types/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
next_operation_id,
)
from nada_dsl.nada_types.generics import T, R
from nada_dsl.nada_types import NadaType
from nada_dsl.nada_types import DslType


class NadaFunctionArg(Generic[T]):
Expand Down Expand Up @@ -65,7 +65,7 @@ def __init__(
function: Callable[[T], R],
return_type: R,
source_ref: SourceRef,
child: NadaType,
child: DslType,
):
self.child = child
self.id = function_id
Expand Down Expand Up @@ -98,15 +98,15 @@ class NadaFunctionCall(Generic[R]):
"""Represents a call to a Nada Function."""

fn: NadaFunction
args: List[NadaType]
args: List[DslType]
source_ref: SourceRef

def __init__(self, nada_function, args, source_ref):
self.id = next_operation_id()
self.args = args
self.fn = nada_function
self.source_ref = source_ref
self.store_in_ast(nada_function.return_type.metatype().to_mir())
self.store_in_ast(nada_function.return_type.type().to_mir())

def store_in_ast(self, ty):
"""Store this function call in the AST."""
Expand Down Expand Up @@ -147,7 +147,7 @@ def create_nada_fn(fn, args_ty) -> NadaFunction[T, R]:

child = fn(*nada_args_type_wrapped)

return_type = child.metatype()
return_type = child.type()
return NadaFunction(
function_id,
function=fn,
Expand Down
Loading

0 comments on commit 3694709

Please sign in to comment.