Skip to content

Commit 5b29b38

Browse files
authored
Implement a unified flow across all entry points for GST processing (#2820)
* define gst iterator * apply changes to exec_kast * update iterator to output kore * update expected output files * use _LOGGER instead of print * Fix syntax error in test-integration target * tweaks
1 parent 306ef67 commit 5b29b38

File tree

9 files changed

+110
-47
lines changed

9 files changed

+110
-47
lines changed

kevm-pyk/src/kevm_pyk/__main__.py

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from pyk.proof.tui import APRProofViewer
3333
from pyk.utils import FrozenDict, hash_str, single
3434

35+
from kevm_pyk.interpreter import iterate_gst
3536
from kevm_pyk.summarizer import batch_summarize, clear_proofs, summarize
3637

3738
from . import VERSION, config
@@ -41,7 +42,7 @@
4142
get_argument_type_setter,
4243
get_option_string_destination,
4344
)
44-
from .gst_to_kore import SORT_ETHEREUM_SIMULATION, filter_gst_keys, gst_to_kore, kore_pgm_to_kore
45+
from .gst_to_kore import SORT_ETHEREUM_SIMULATION, kore_pgm_to_kore
4546
from .kevm import KEVM, KEVMSemantics, kevm_node_printer
4647
from .kompile import KompileTarget, kevm_kompile
4748
from .utils import (
@@ -598,26 +599,38 @@ def exec_run(options: RunOptions) -> None:
598599

599600
try:
600601
json_read = json.loads(options.input_file.read_text())
601-
gst_data = filter_gst_keys(json_read)
602-
kore_pattern = gst_to_kore(gst_data, options.schedule, options.mode, options.chainid, options.usegas)
602+
if options.gst_name:
603+
json_read = {options.gst_name: json_read[options.gst_name]}
604+
kore_pattern_list = [
605+
(name, kore)
606+
for (name, kore) in iterate_gst(json_read, options.schedule, options.mode, options.chainid, options.usegas)
607+
]
603608
except json.JSONDecodeError:
604609
pgm_token = KToken(options.input_file.read_text(), KSort('EthereumSimulation'))
605610
kast_pgm = kevm.parse_token(pgm_token)
606611
kore_pgm = kevm.kast_to_kore(kast_pgm, sort=KSort('EthereumSimulation'))
607-
kore_pattern = kore_pgm_to_kore(
608-
kore_pgm, SORT_ETHEREUM_SIMULATION, options.schedule, options.mode, options.chainid, options.usegas
612+
kore_pattern_list = [
613+
(
614+
'',
615+
kore_pgm_to_kore(
616+
kore_pgm, SORT_ETHEREUM_SIMULATION, options.schedule, options.mode, options.chainid, options.usegas
617+
),
618+
),
619+
]
620+
621+
for name, kore_pattern in kore_pattern_list:
622+
if name:
623+
_LOGGER.info(f'Processing test - {name}')
624+
kevm.run(
625+
kore_pattern,
626+
depth=options.depth,
627+
term=True,
628+
expand_macros=options.expand_macros,
629+
output=options.output,
630+
check=True,
631+
debugger=options.debugger,
609632
)
610633

611-
kevm.run(
612-
kore_pattern,
613-
depth=options.depth,
614-
term=True,
615-
expand_macros=options.expand_macros,
616-
output=options.output,
617-
check=True,
618-
debugger=options.debugger,
619-
)
620-
621634

622635
def exec_kast(options: KastOptions) -> None:
623636
target = options.target or 'llvm'
@@ -628,17 +641,30 @@ def exec_kast(options: KastOptions) -> None:
628641

629642
try:
630643
json_read = json.loads(options.input_file.read_text())
631-
kore_pattern = gst_to_kore(json_read, options.schedule, options.mode, options.chainid, options.usegas)
644+
if options.gst_name:
645+
json_read = {options.gst_name: json_read[options.gst_name]}
646+
kore_pattern_list = [
647+
(name, kore)
648+
for (name, kore) in iterate_gst(json_read, options.schedule, options.mode, options.chainid, options.usegas)
649+
]
632650
except json.JSONDecodeError:
633651
pgm_token = KToken(options.input_file.read_text(), KSort('EthereumSimulation'))
634652
kast_pgm = kevm.parse_token(pgm_token)
635653
kore_pgm = kevm.kast_to_kore(kast_pgm)
636-
kore_pattern = kore_pgm_to_kore(
637-
kore_pgm, SORT_ETHEREUM_SIMULATION, options.schedule, options.mode, options.chainid, options.usegas
638-
)
639-
640-
output_text = kore_print(kore_pattern, definition_dir=kevm.definition_dir, output=options.output)
641-
print(output_text)
654+
kore_pattern_list = [
655+
(
656+
'',
657+
kore_pgm_to_kore(
658+
kore_pgm, SORT_ETHEREUM_SIMULATION, options.schedule, options.mode, options.chainid, options.usegas
659+
),
660+
),
661+
]
662+
663+
for name, kore_pattern in kore_pattern_list:
664+
if name:
665+
_LOGGER.info(f'Processing test - {name}')
666+
output_text = kore_print(kore_pattern, definition_dir=kevm.definition_dir, output=options.output)
667+
print(output_text)
642668

643669

644670
def exec_summarize(options: SummarizeOptions) -> None:

kevm-pyk/src/kevm_pyk/cli.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,13 @@ def _create_argument_parser() -> ArgumentParser:
259259
],
260260
)
261261
run_args.add_argument('input_file', type=file_path, help='Path to input file.')
262+
run_args.add_argument(
263+
'--test',
264+
type=str,
265+
dest='gst_name',
266+
default=None,
267+
help='Name of the test inside the gst file. By default all tests are processed.',
268+
)
262269
run_args.add_argument(
263270
'--output',
264271
type=KRunOutput,
@@ -298,6 +305,13 @@ def _create_argument_parser() -> ArgumentParser:
298305
],
299306
)
300307
kast_args.add_argument('input_file', type=file_path, help='Path to input file.')
308+
kast_args.add_argument(
309+
'--test',
310+
type=str,
311+
dest='gst_name',
312+
default=None,
313+
help='Name of the test inside the gst file. By default all tests are processed.',
314+
)
301315
kast_args.add_argument(
302316
'--output',
303317
type=PrintOutput,
@@ -755,14 +769,11 @@ class RunOptions(
755769
output: KRunOutput
756770
expand_macros: bool
757771
debugger: bool
772+
gst_name: str | None
758773

759774
@staticmethod
760775
def default() -> dict[str, Any]:
761-
return {
762-
'output': KRunOutput.PRETTY,
763-
'expand_macros': True,
764-
'debugger': False,
765-
}
776+
return {'output': KRunOutput.PRETTY, 'expand_macros': True, 'debugger': False, 'gst_name': None}
766777

767778
@staticmethod
768779
def from_option_string() -> dict[str, str]:
@@ -798,11 +809,13 @@ class KastOptions(
798809
):
799810
input_file: Path
800811
output: PrintOutput
812+
gst_name: str | None
801813

802814
@staticmethod
803815
def default() -> dict[str, Any]:
804816
return {
805817
'output': PrintOutput.KORE,
818+
'gst_name': None,
806819
}
807820

808821
@staticmethod

kevm-pyk/src/kevm_pyk/interpreter.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,23 @@
88
from .gst_to_kore import filter_gst_keys, gst_to_kore
99

1010
if TYPE_CHECKING:
11+
from collections.abc import Iterator
1112
from typing import Any
1213

13-
from pyk.kore.syntax import Pattern
14+
from pyk.kore.syntax import App, Pattern
1415

1516

16-
def interpret(gst_data: Any, schedule: str, mode: str, chainid: int, usegas: bool, *, check: bool = True) -> Pattern:
17+
def iterate_gst(
18+
gst_data: dict, schedule: str, mode: str, chainid: int, usegas: bool, skipped_keys: frozenset[str] = frozenset()
19+
) -> Iterator[tuple[str, App]]:
20+
"""Yield (test_name, kore_pattern) for each test in GST data after filtering discarded keys."""
21+
for test_name, test in gst_data.items():
22+
if test_name in skipped_keys:
23+
continue
24+
gst_filtered = {test_name: filter_gst_keys(test)}
25+
yield test_name, gst_to_kore(gst_filtered, schedule, mode, chainid, usegas)
26+
27+
28+
def interpret(init_kore: Any, *, check: bool = True) -> Pattern:
1729
"""Interpret the given GST data using the LLVM backend."""
18-
init_kore = gst_to_kore(filter_gst_keys(gst_data), schedule, mode, chainid, usegas)
1930
return llvm_interpret(kdist.get('evm-semantics.llvm'), init_kore, check=check)

kevm-pyk/src/tests/integration/test_run.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from pyk.kdist import kdist
88
from pyk.kore.tools import PrintOutput, kore_print
99

10-
from kevm_pyk.interpreter import interpret
10+
from kevm_pyk.interpreter import interpret, iterate_gst
1111

1212
from ..utils import REPO_ROOT
1313

@@ -30,9 +30,11 @@ def test_run(gst_file: Path, update_expected_output: bool) -> None:
3030
with gst_file.open() as f:
3131
gst_data = json.load(f)
3232

33+
actual = ''
3334
# When
34-
pattern = interpret(gst_data, 'PRAGUE', 'NORMAL', 1, True, check=False)
35-
actual = kore_print(pattern, definition_dir=kdist.get('evm-semantics.llvm'), output=PrintOutput.PRETTY)
35+
for _, init_kore in iterate_gst(gst_data, 'PRAGUE', 'NORMAL', 1, True):
36+
pattern = interpret(init_kore, check=False)
37+
actual += kore_print(pattern, definition_dir=kdist.get('evm-semantics.llvm'), output=PrintOutput.PRETTY)
3638

3739
# Then
3840

kevm-pyk/src/tests/unit/test_gst_to_kore.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import pytest
77
from pyk.kore.parser import KoreParser
88

9-
from kevm_pyk.gst_to_kore import gst_to_kore
9+
from kevm_pyk.interpreter import iterate_gst
1010

1111
from ..utils import REPO_ROOT
1212

@@ -35,17 +35,22 @@ def test_gst_to_kore(gst_path: str, expected_path: str, update_expected_output:
3535
# Given
3636
gst_file = REPO_ROOT / gst_path
3737
gst_data = json.loads(gst_file.read_text())
38-
3938
expected_file = REPO_ROOT / expected_path
40-
expected = KoreParser(expected_file.read_text()).pattern()
4139

4240
# When
43-
actual = gst_to_kore(gst_data, 'CANCUN', 'NORMAL', 1, True)
41+
actuals = [kore for _, kore in iterate_gst(gst_data, 'CANCUN', 'NORMAL', 1, True)]
4442

4543
# Then
4644
if update_expected_output:
4745
with expected_file.open('w') as f:
48-
actual.write(f)
46+
for kore in actuals:
47+
kore.write(f)
48+
f.write('\n')
4949
return
5050

51-
assert actual == expected
51+
expected_lines = expected_file.read_text().strip().split('\n')
52+
assert len(actuals) == len(expected_lines)
53+
54+
for actual, expected_line in zip(actuals, expected_lines, strict=True):
55+
expected = KoreParser(expected_line).pattern()
56+
assert actual == expected

kevm-pyk/src/tests/utils.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from pyk.kore.syntax import App
1313
from pyk.kore.tools import PrintOutput, kore_print
1414

15-
from kevm_pyk.interpreter import interpret
15+
from kevm_pyk.interpreter import interpret, iterate_gst
1616

1717
if TYPE_CHECKING:
1818
from collections.abc import Callable
@@ -88,11 +88,9 @@ def _test(
8888

8989
chain_id = compute_chain_id(gst_file_relative_path)
9090

91-
for test_name, test in gst_data.items():
91+
for test_name, init_kore in iterate_gst(gst_data, schedule, mode, chain_id, usegas, skipped_gst_tests):
9292
_LOGGER.info(f'Running test: {gst_file} - {test_name}')
93-
if test_name in skipped_gst_tests:
94-
continue
95-
res = interpret({test_name: test}, schedule, mode, chain_id, usegas, check=False)
93+
res = interpret(init_kore, check=False)
9694

9795
try:
9896
_assert_exit_code_zero(res)

tests/interactive/CallRecursiveContract_d0g0v0.json.parse-expected

Lines changed: 5 additions & 1 deletion
Large diffs are not rendered by default.

tests/interactive/log3.gst-to-kore.expected

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

tests/interactive/log3_MaxTopic_d0g0v0.json.parse-expected

Lines changed: 5 additions & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)