Skip to content

Commit 689e7c6

Browse files
committed
kevm-pyk/utils: initial simple macro expander
1 parent c69dd40 commit 689e7c6

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

kevm-pyk/src/kevm_pyk/utils.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from typing import Collection, Iterable, List, Optional, Tuple
33

44
from pyk.cterm import CTerm
5-
from pyk.kast.inner import KApply, KInner, KVariable
5+
from pyk.kast.inner import KApply, KInner, KRewrite, KVariable
66
from pyk.kast.manip import (
77
abstract_term_safely,
88
bool_to_ml_pred,
@@ -25,6 +25,23 @@
2525

2626

2727
def KDefinition__expand_macros(defn: KDefinition, term: KInner) -> KInner: # noqa: N802
28+
def _expand_macros(_term: KInner) -> KInner:
29+
if type(_term) is KApply:
30+
prod = defn.production_for_klabel(_term.label)
31+
if 'macro' in prod.att or 'alias' in prod.att or 'macro-rec' in prod.att or 'alias-rec' in prod.att:
32+
for r in defn.macro_rules:
33+
assert type(r.body) is KRewrite
34+
_new_term = r.body.apply_top(_term)
35+
if _new_term != _term:
36+
_term = _new_term
37+
break
38+
return _term
39+
40+
old_term = None
41+
while term != old_term:
42+
old_term = term
43+
term = bottom_up(_expand_macros, term)
44+
2845
return term
2946

3047

0 commit comments

Comments
 (0)