diff --git a/nada_dsl/nada_types/__init__.py b/nada_dsl/nada_types/__init__.py index 182150d..cb90b8b 100644 --- a/nada_dsl/nada_types/__init__.py +++ b/nada_dsl/nada_types/__init__.py @@ -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()`. """ @@ -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() diff --git a/nada_dsl/nada_types/collections.py b/nada_dsl/nada_types/collections.py index aacd227..07d7227 100644 --- a/nada_dsl/nada_types/collections.py +++ b/nada_dsl/nada_types/collections.py @@ -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 {} @@ -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() ), @@ -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]): @@ -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(), } } @@ -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], } } @@ -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 @@ -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()}, } } @@ -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()}, } } @@ -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, } } @@ -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( @@ -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( diff --git a/nada_dsl/nada_types/function.py b/nada_dsl/nada_types/function.py index 87fea86..34cfd67 100644 --- a/nada_dsl/nada_types/function.py +++ b/nada_dsl/nada_types/function.py @@ -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.""" diff --git a/tests/compiler_frontend_test.py b/tests/compiler_frontend_test.py index e8e62f4..0e133e1 100644 --- a/tests/compiler_frontend_test.py +++ b/tests/compiler_frontend_test.py @@ -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() @@ -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] @@ -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( @@ -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():