Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: split NadaType and NadaValue / refactoring #60

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -452,8 +452,7 @@ timeout-methods=requests.api.delete,requests.api.get,requests.api.head,requests.

# List of note tags to take in consideration, separated by a comma.
notes=FIXME,
XXX,
TODO
XXX

# Regular expression of note tags to take in consideration.
notes-rgx=
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ test-dependencies:
pip install .'[test]'

test: test-dependencies
pytest
uv run pytest

# Build protocol buffers definitions.
build_proto:
Expand Down
26 changes: 24 additions & 2 deletions nada_dsl/ast_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import hashlib
from typing import Dict, List
from sortedcontainers import SortedDict
from nada_dsl.nada_types import NadaTypeRepr, Party
from nada_dsl.nada_types import DslTypeRepr, Party
from nada_dsl.source_ref import SourceRef

OPERATION_ID_COUNTER = 0
Expand Down Expand Up @@ -41,7 +41,7 @@ class ASTOperation(ABC):

id: int
source_ref: SourceRef
ty: NadaTypeRepr
ty: DslTypeRepr

def child_operations(self) -> List[int]:
"""Returns the list of identifiers of all the child operations of this operation."""
Expand Down 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
25 changes: 9 additions & 16 deletions nada_dsl/nada_types/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Nada type definitions."""

from abc import ABC, abstractmethod
from dataclasses import dataclass
from enum import Enum
from typing import Dict, TypeAlias, Union, Type
Expand Down Expand Up @@ -84,7 +85,7 @@ def __init__(self, name):
]

""
NadaTypeRepr: TypeAlias = str | Dict[str, Dict]
DslTypeRepr: TypeAlias = str | Dict[str, Dict]
"""Type alias for the NadaType representation.

This representation can be either a string ("SecretInteger")
Expand Down Expand Up @@ -112,8 +113,9 @@ def is_numeric(self) -> bool:
return self in (BaseType.INTEGER, BaseType.UNSIGNED_INTEGER)


# TODO: abstract?
@dataclass
class NadaType:
class DslType:
"""Nada type class.

This is the parent class of all nada types.
Expand Down Expand Up @@ -144,20 +146,7 @@ def __init__(self, child: OperationType):
"""
self.child = child
if self.child is not None:
self.child.store_in_ast(self.to_mir())

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

@classmethod
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.
if name.startswith("Public"):
name = name[len("Public") :].lstrip()
return name
self.child.store_in_ast(self.type().to_mir())

def __bool__(self):
raise NotImplementedError
Expand All @@ -171,3 +160,7 @@ def is_scalar(cls) -> bool:
def is_literal(cls) -> bool:
"""Returns True if the type is a literal."""
return False

@abstractmethod
def type(self):
"""Returns a meta type for this NadaType."""
Loading
Loading