From ef260637684b4fd1dde75ab981e93fa0dae6a2f2 Mon Sep 17 00:00:00 2001 From: Jan Tusil Date: Wed, 2 Aug 2023 15:15:38 +0200 Subject: [PATCH 1/3] BasePrinter --- src/pyk/kast/pretty.py | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/src/pyk/kast/pretty.py b/src/pyk/kast/pretty.py index 87ea13b95..83c4d49aa 100644 --- a/src/pyk/kast/pretty.py +++ b/src/pyk/kast/pretty.py @@ -44,10 +44,19 @@ 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() + + +class PrettyPrinter(BasePrinter): definition: KDefinition - _extra_unparsing_modules: Iterable[KFlatModule] - _patch_symbol_table: Callable[[SymbolTable], None] | None _unalias: bool _sort_collections: bool @@ -59,23 +68,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. From 4b423eb03ef670d8241ca628634b31274a492555 Mon Sep 17 00:00:00 2001 From: Jan Tusil Date: Wed, 2 Aug 2023 15:29:35 +0200 Subject: [PATCH 2/3] move som stuff to base class --- src/pyk/kast/pretty.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/pyk/kast/pretty.py b/src/pyk/kast/pretty.py index 83c4d49aa..5e1fecbfa 100644 --- a/src/pyk/kast/pretty.py +++ b/src/pyk/kast/pretty.py @@ -54,6 +54,12 @@ def __init__(self, lazy_symbol_table: Callable[[], SymbolTable]): 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 @@ -180,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: @@ -348,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, From 719cfa68c91a3937116c5b417918aa87c866c9bc Mon Sep 17 00:00:00 2001 From: devops Date: Wed, 2 Aug 2023 13:32:49 +0000 Subject: [PATCH 3/3] Set Version: 0.1.395 --- package/version | 2 +- pyproject.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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. ",