diff --git a/nada_dsl/ast_util.py b/nada_dsl/ast_util.py index 10290dd..806dc65 100644 --- a/nada_dsl/ast_util.py +++ b/nada_dsl/ast_util.py @@ -10,6 +10,7 @@ OPERATION_ID_COUNTER = 0 + def next_operation_id() -> int: """Returns the next value of the operation id counter.""" global OPERATION_ID_COUNTER @@ -55,6 +56,9 @@ def to_mir(self): # The key is the operation identifier, the value the operation AST_OPERATIONS: Dict[int, ASTOperation] = SortedDict() +# Map of literal hashes to index +LITERALS: Dict[str, int] = {} + @dataclass class BinaryASTOperation(ASTOperation): @@ -167,7 +171,7 @@ class LiteralASTOperation(ASTOperation): name: str value: object - literal_name: str + literal_index: str def __init__( self, @@ -184,16 +188,23 @@ def __init__( self.source_ref = source_ref # Generate a unique name depending on the value and type # to prevent duplicating literals in the bytecode. - self.literal_name = hashlib.md5( + literal_name = hashlib.md5( (str(self.value) + str(self.ty)).encode("UTF-8") ).hexdigest() + + # Replace the literal name by an index so it is shorter + if literal_name not in LITERALS: + LITERALS[literal_name] = len(LITERALS) + + self.literal_index = str(LITERALS[literal_name]) + super().__init__(id=self.id, source_ref=self.source_ref, ty=self.ty) def to_mir(self): return { "LiteralReference": { "id": self.id, - "refers_to": self.literal_name, + "refers_to": self.literal_index, "type": self.ty, "source_ref_index": self.source_ref.to_index(), } diff --git a/nada_dsl/compiler_frontend.py b/nada_dsl/compiler_frontend.py index d44b123..400beb3 100644 --- a/nada_dsl/compiler_frontend.py +++ b/nada_dsl/compiler_frontend.py @@ -305,7 +305,7 @@ def process_operation( processed_operation = ProcessOperationOutput(operation.to_mir(), None) elif isinstance(operation, LiteralASTOperation): - LITERALS[operation.literal_name] = (str(operation.value), operation.ty) + LITERALS[operation.literal_index] = (str(operation.value), operation.ty) processed_operation = ProcessOperationOutput(operation.to_mir(), None) elif isinstance( operation, (MapASTOperation, ReduceASTOperation, NadaFunctionCallASTOperation) diff --git a/nada_dsl/compiler_frontend_test.py b/nada_dsl/compiler_frontend_test.py index ff609c5..3f49d63 100644 --- a/nada_dsl/compiler_frontend_test.py +++ b/nada_dsl/compiler_frontend_test.py @@ -577,7 +577,6 @@ def test_binary_operator_integer_publicinteger(operator, name, ty): right_ast = AST_OPERATIONS[inner["right"]] assert isinstance(left_ast, LiteralASTOperation) assert left_ast.value == -3 - assert len(left_ast.literal_name) == 32 assert isinstance(right_ast, InputASTOperation) assert right_ast.name == "right" assert inner["type"] == to_type(ty)