Skip to content

Commit

Permalink
chore: rename to_type to to_mir
Browse files Browse the repository at this point in the history
  • Loading branch information
Jmgr committed Nov 14, 2024
1 parent 0d75641 commit b525148
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 28 deletions.
6 changes: 3 additions & 3 deletions nada_dsl/nada_types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class NadaType:
Whenever the Python interpreter constructs an instance of NadaType, it will also store
in memory the corresponding parent operation. In order to do so, the ASTOperation will
need the type in MIR format. Which is why all instances of `NadaType` provide an implementation
of `to_type()`.
of `to_mir()`.
"""

Expand All @@ -146,9 +146,9 @@ def __init__(self, parent: OperationType):
"""
self.parent = parent
if self.parent is not None:
self.parent.store_in_ast(self.to_type())
self.parent.store_in_ast(self.to_mir())

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

Expand Down
38 changes: 19 additions & 19 deletions nada_dsl/nada_types/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class Collection(NadaType):
right_type: AllTypesType
contained_types: AllTypesType

def to_type(self):
def to_mir(self):
"""Convert operation wrapper to a dictionary representing its type."""
if isinstance(self, (Array, ArrayType)):
size = {"size": self.size} if self.size else {}
Expand All @@ -65,12 +65,12 @@ def to_type(self):
return {
"Tuple": {
"left_type": (
self.left_type.to_type()
self.left_type.to_mir()
if isinstance(self.left_type, (NadaType, ArrayType, TupleType))
else self.left_type.class_to_type()
),
"right_type": (
self.right_type.to_type()
self.right_type.to_mir()
if isinstance(self.right_type, (NadaType, ArrayType, TupleType))
else self.right_type.class_to_type()
),
Expand All @@ -86,7 +86,7 @@ def retrieve_inner_type(self):
return "T"
if inspect.isclass(self.contained_types):
return self.contained_types.class_to_type()
return self.contained_types.to_type()
return self.contained_types.to_mir()


class Map(Generic[T, R]):
Expand Down Expand Up @@ -159,12 +159,12 @@ class TupleType:
left_type: NadaType
right_type: NadaType

def to_type(self):
def to_mir(self):
"""Convert a tuple object into a Nada type."""
return {
"Tuple": {
"left_type": self.left_type.to_type(),
"right_type": self.right_type.to_type(),
"left_type": self.left_type.to_mir(),
"right_type": self.right_type.to_mir(),
}
}

Expand Down Expand Up @@ -205,11 +205,11 @@ class NTupleType:

types: List[NadaType]

def to_type(self):
def to_mir(self):
"""Convert a n tuple object into a Nada type."""
return {
"NTuple": {
"types": [ty.to_type() for ty in self.types],
"types": [ty.to_mir() for ty in self.types],
}
}

Expand Down Expand Up @@ -240,9 +240,9 @@ def generic_type(cls, types: List[NadaType]) -> NTupleType:
"""Returns the generic type for this NTuple"""
return NTupleType(types=types)

def to_type(self):
def to_mir(self):
"""Convert operation wrapper to a dictionary representing its type."""
return {"NTuple": {"types": [ty.to_type() for ty in self.types]}}
return {"NTuple": {"types": [ty.to_mir() for ty in self.types]}}


@dataclass
Expand All @@ -251,11 +251,11 @@ class ObjectType:

types: Dict[str, NadaType]

def to_type(self):
def to_mir(self):
"""Convert an object into a Nada type."""
return {
"Object": {
"types": {name: ty.to_type() for name, ty in self.types.items()},
"types": {name: ty.to_mir() for name, ty in self.types.items()},
}
}

Expand Down Expand Up @@ -286,11 +286,11 @@ def generic_type(cls, types: Dict[str, NadaType]) -> ObjectType:
"""Returns the generic type for this Object"""
return ObjectType(types=types)

def to_type(self):
def to_mir(self):
"""Convert operation wrapper to a dictionary representing its type."""
return {
"Object": {
"types": {name: ty.to_type() for name, ty in self.types.items()},
"types": {name: ty.to_mir() for name, ty in self.types.items()},
}
}

Expand Down Expand Up @@ -370,11 +370,11 @@ class ArrayType:
contained_types: AllTypesType
size: int

def to_type(self):
def to_mir(self):
"""Convert this generic type into a MIR Nada type."""
return {
"Array": {
"contained_types": self.contained_types.to_type(),
"contained_types": self.contained_types.to_mir(),
"size": self.size,
}
}
Expand Down Expand Up @@ -410,7 +410,7 @@ def __init__(self, parent, size: int, contained_types: T = None):
parent if contained_types is not None else getattr(parent, "parent", None)
)
if self.parent is not None:
self.parent.store_in_ast(self.to_type())
self.parent.store_in_ast(self.to_mir())

def __iter__(self):
raise NotAllowedException(
Expand Down Expand Up @@ -540,7 +540,7 @@ def __init__(self, parent, size, contained_types=None):
)
self.size = size
self.parent = parent if contained_types else getattr(parent, "parent", None)
self.parent.store_in_ast(self.to_type())
self.parent.store_in_ast(self.to_mir())

def __iter__(self):
raise NotAllowedException(
Expand Down
2 changes: 1 addition & 1 deletion nada_dsl/nada_types/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def __init__(self, function_id: int, name: str, arg_type: T, source_ref: SourceR
self.name = name
self.type = arg_type
self.source_ref = source_ref
self.store_in_ast(arg_type.to_type())
self.store_in_ast(arg_type.to_mir())

def store_in_ast(self, ty):
"""Store object in AST."""
Expand Down
2 changes: 1 addition & 1 deletion nada_dsl/nada_types/scalar_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"""The Nada Scalar type definitions."""

from dataclasses import dataclass
from typing import Any, Union, TypeVar
from typing import Union, TypeVar
from typing_extensions import Self
from nada_dsl.operations import *
from nada_dsl.program_io import Literal
Expand Down
8 changes: 4 additions & 4 deletions tests/compiler_frontend_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def create_output(root: AllTypes, name: str, party: str) -> Output:
return Output(root, name, Party(party))


def to_type(name: str):
def to_mir(name: str):
# Rename public variables so they are considered as the same as literals.
if name.startswith("Public"):
name = name[len("Public") :].lstrip()
Expand Down Expand Up @@ -127,7 +127,7 @@ def test_duplicated_inputs_checks():
def test_array_type_conversion(input_type, type_name, size):
inner_input = create_input(SecretInteger, "name", "party", **{})
collection = create_collection(input_type, inner_input, size, **{})
converted_input = collection.to_type()
converted_input = collection.to_mir()
assert list(converted_input.keys()) == [type_name]


Expand Down Expand Up @@ -595,7 +595,7 @@ def test_binary_operator_integer_integer(binary_operator, name, ty):
op = process_operation(ast_operation, {}).mir
assert list(op.keys()) == [name]
parent = op[name]
assert parent["type"] == to_type(ty)
assert parent["type"] == to_mir(ty)


@pytest.mark.parametrize(
Expand Down Expand Up @@ -629,7 +629,7 @@ def test_binary_operator_integer_publicinteger(operator, name, ty):
assert left_ast.value == -3
assert isinstance(right_ast, InputASTOperation)
assert right_ast.name == "right"
assert parent["type"] == to_type(ty)
assert parent["type"] == to_mir(ty)


def test_logical_operations():
Expand Down

0 comments on commit b525148

Please sign in to comment.