Skip to content

Commit

Permalink
pyk getters for KOuter classes, updates to unparsing (#2693)
Browse files Browse the repository at this point in the history
* pyk/kast: add syntax_productions property, and lift both productions/syntax_products to KDefinition

* pyk/kast: add constructors/functions properties to KFlatModule and KDefinition

* pyk/kprint: only consider syntax productions for unparsing

* ktool/kprint: add parens for productions with NonTerminal on edges

* pyk/kast: calculate constructors as inverse of functions

* Revert "ktool/kprint: add parens for productions with NonTerminal on edges"

This reverts commit 4d8dbbe.

* pyk/kast: correct check for non-free-constructors

* pyk/kast: correct and clarify issue with functions property of KFlatModule

* Update pyk/src/pyk/kast.py

Co-authored-by: Tamás Tóth <[email protected]>

* pyk/kast: move _is_non_free_constructor into functions property of KFlatModule

Co-authored-by: Tamás Tóth <[email protected]>
Co-authored-by: rv-jenkins <[email protected]>
  • Loading branch information
3 people authored Jun 30, 2022
1 parent 51e719d commit f5b61d6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
36 changes: 36 additions & 0 deletions pyk/src/pyk/kast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1223,6 +1223,26 @@ def __iter__(self) -> Iterator[KSentence]:
def productions(self) -> List[KProduction]:
return [sentence for sentence in self.sentences if type(sentence) is KProduction]

@property
def syntax_productions(self) -> List[KProduction]:
return [prod for prod in self.productions if prod.klabel]

@property
def functions(self) -> List[KProduction]:

def _is_non_free_constructor(label: str) -> bool:
is_cell_map_constructor = label.endswith('CellMapItem') or label.endswith('CellMap_')
is_builtin_data_constructor = label in {'_Set_', '_List_', '_Map_', 'SetItem', 'ListItem', '_|->_'}
return is_cell_map_constructor or is_builtin_data_constructor

_functions = [prod for prod in self.syntax_productions if 'function' in prod.att.atts or 'functional' in prod.att.atts]
_functions = [f for f in _functions if not (f.klabel and _is_non_free_constructor(f.klabel.name))]
return _functions

@property
def constructors(self) -> List[KProduction]:
return [prod for prod in self.syntax_productions if prod not in self.functions]

@property
def rules(self) -> List[KRule]:
return [sentence for sentence in self.sentences if type(sentence) is KRule]
Expand Down Expand Up @@ -1354,6 +1374,22 @@ def let(
def let_att(self, att: KAtt) -> 'KDefinition':
return self.let(att=att)

@property
def productions(self) -> List[KProduction]:
return [prod for module in self.modules for prod in module.productions]

@property
def syntax_productions(self) -> List[KProduction]:
return [prod for module in self.modules for prod in module.syntax_productions]

@property
def functions(self) -> List[KProduction]:
return [prod for module in self.modules for prod in module.functions]

@property
def constructors(self) -> List[KProduction]:
return [prod for module in self.modules for prod in module.constructors]


# TODO make method of KInner
def bottom_up(f: Callable[[KInner], KInner], kinner: KInner) -> KInner:
Expand Down
16 changes: 5 additions & 11 deletions pyk/src/pyk/ktool/kprint.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import sys
from pathlib import Path
from typing import Optional

from ..kast import (
TRUE,
Expand Down Expand Up @@ -83,17 +82,12 @@ def build_symbol_table(definition, opinionated=False):

symbol_table = {}
for module in definition.modules:
for prod in module.productions:
label: Optional[str] = None

if prod.klabel:
label = prod.klabel.name
elif 'symbol' in prod.att and 'klabel' in prod.att:
for prod in module.syntax_productions:
label = prod.klabel.name
if 'symbol' in prod.att and 'klabel' in prod.att:
label = prod.att['klabel']

if label:
unparser = unparser_for_production(prod)
symbol_table[label] = unparser
unparser = unparser_for_production(prod)
symbol_table[label] = unparser

if opinionated:
symbol_table['#And'] = lambda c1, c2: c1 + '\n#And ' + c2
Expand Down

0 comments on commit f5b61d6

Please sign in to comment.