diff --git a/package/version b/package/version index 514adcc3d..b120a5024 100644 --- a/package/version +++ b/package/version @@ -1 +1 @@ -0.1.394 +0.1.395 diff --git a/pyproject.toml b/pyproject.toml index ea345019c..d910cfe6b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "pyk" -version = "0.1.394" +version = "0.1.395" description = "" authors = [ "Runtime Verification, Inc. ", diff --git a/src/pyk/kast/pretty.py b/src/pyk/kast/pretty.py index 87ea13b95..5e1fecbfa 100644 --- a/src/pyk/kast/pretty.py +++ b/src/pyk/kast/pretty.py @@ -44,10 +44,25 @@ SymbolTable = dict[str, Callable[..., str]] -class PrettyPrinter: +class BasePrinter: + _lazy_symbol_table: Callable[[], SymbolTable] + + def __init__(self, lazy_symbol_table: Callable[[], SymbolTable]): + self._lazy_symbol_table = lazy_symbol_table + + @cached_property + def symbol_table(self) -> SymbolTable: + return self._lazy_symbol_table() + + def _applied_label_str(self, symbol: str) -> Callable[..., str]: + return lambda *args: symbol + ' ( ' + ' , '.join(args) + ' )' + + def get_unparser_for(self, label: str) -> Callable[..., str]: + return self._applied_label_str(label) if label not in self.symbol_table else self.symbol_table[label] + + +class PrettyPrinter(BasePrinter): definition: KDefinition - _extra_unparsing_modules: Iterable[KFlatModule] - _patch_symbol_table: Callable[[SymbolTable], None] | None _unalias: bool _sort_collections: bool @@ -59,23 +74,22 @@ def __init__( unalias: bool = True, sort_collections: bool = False, ): + def lazy_symbol_table() -> SymbolTable: + symb_table = build_symbol_table( + definition, + extra_modules=extra_unparsing_modules, + opinionated=True, + ) + if patch_symbol_table is not None: + patch_symbol_table(symb_table) + return symb_table + + super().__init__(lazy_symbol_table) + self.definition = definition - self._extra_unparsing_modules = extra_unparsing_modules - self._patch_symbol_table = patch_symbol_table self._unalias = unalias self._sort_collections = sort_collections - @cached_property - def symbol_table(self) -> SymbolTable: - symb_table = build_symbol_table( - self.definition, - extra_modules=self._extra_unparsing_modules, - opinionated=True, - ) - if self._patch_symbol_table is not None: - self._patch_symbol_table(symb_table) - return symb_table - def print(self, kast: KAst) -> str: """Print out KAST terms/outer syntax. - Input: KAST term. @@ -172,7 +186,7 @@ def _print_kapply(self, kapply: KApply) -> str: cell_contents = '\n'.join(unparsed_args).rstrip() cell_str = label + '\n' + indent(cell_contents) + '\n str: @@ -340,9 +354,6 @@ def join_sep(s: str) -> str: else: return self.print(kast) - def _applied_label_str(self, symbol: str) -> Callable[..., str]: - return lambda *args: symbol + ' ( ' + ' , '.join(args) + ' )' - def build_symbol_table( definition: KDefinition,