Skip to content

Commit

Permalink
Implemented factory pattern for trace transformers
Browse files Browse the repository at this point in the history
  • Loading branch information
NikosDelijohn committed Oct 10, 2024
1 parent f5c1245 commit 4972ae4
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/testcrush/grammars/transformers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
# SPDX-License-Identifier: MIT

import lark
import pathlib

from typing import Literal, Any, Iterable
from typing import Literal, Any, Iterable, Union
from testcrush.zoix import Fault
from testcrush.utils import get_logger

Expand Down Expand Up @@ -312,3 +313,20 @@ def reg_and_mem(self, *reg_val_pairs: str) -> str:
"""
reg_and_mem = f"\"{', '.join([ str(pair).strip() for pair in reg_val_pairs])}\""
return reg_and_mem


class TraceTransformerFactory:
"""
Factory pattern for trace transformers and the corresponding grammars.
To be used as ``transformer, grammar = TraceTransformerFactory("ProcessorString")``
"""
_transformers = {
"CV32E40P": (TraceTransformerCV32E40P, pathlib.Path("trace_cv32e40p.lark").resolve())
}

def __call__(self, processor_type: str) -> Union[tuple[TraceTransformerCV32E40P, pathlib.Path]]:
transformer, grammar = self._transformers.get(processor_type, default=(None, None))
if not transformer:
raise KeyError(f"Transformer for {processor_type} not found")
return transformer(), grammar

0 comments on commit 4972ae4

Please sign in to comment.