-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
95665c8
commit 233b1e6
Showing
7 changed files
with
260 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING, Final | ||
|
||
import pytest | ||
from pyk.ktool.krun import llvm_interpret | ||
|
||
if TYPE_CHECKING: | ||
from pathlib import Path | ||
|
||
from pyk.kore.syntax import Pattern | ||
|
||
|
||
@pytest.fixture(scope='module') | ||
def definition_dir() -> Path: | ||
from pyk.kdist import kdist | ||
|
||
return kdist.get('imp-semantics.expr') | ||
|
||
|
||
TEST_DATA: Final = ( | ||
('false', False), | ||
('true', True), | ||
('0', 0), | ||
('-1', -1), | ||
('--1', 1), | ||
('1 + 2', 3), | ||
('3 - 2', 1), | ||
('2 * 3', 6), | ||
('6 / 2', 3), | ||
('1 + 2 - 3', 0), | ||
('2 * 3 / 4', 1), | ||
('1 + 2 * 3', 7), | ||
('(1 + 2) * 3', 9), | ||
('0 == 0', True), | ||
('0 == 1', False), | ||
('true == true', True), | ||
('false == false', True), | ||
('true == false', False), | ||
('false == true', False), | ||
('1 >= 1', True), | ||
('1 > 1', False), | ||
('1 <= 1', True), | ||
('1 < 1', False), | ||
('0 < 1 == 1 < 2', True), | ||
('1 == 1 == true', True), | ||
('!true', False), | ||
('!false', True), | ||
('!!true', True), | ||
('!(1 > 2)', True), | ||
('false && true', False), | ||
('true && true', True), | ||
('true && 1', 1), | ||
('true || false', True), | ||
('false || false', False), | ||
('false || 1', 1), | ||
('1 > 2 && 1 == 1', False), | ||
('1 > 2 || 1 == 1', True), | ||
('true || false == false', True), | ||
('(true || false) == false', False), | ||
) | ||
|
||
|
||
@pytest.mark.parametrize('text,expected', TEST_DATA, ids=[test_id for test_id, _ in TEST_DATA]) | ||
def test_expr( | ||
text: str, | ||
expected: int | bool, | ||
definition_dir: Path, | ||
) -> None: | ||
# When | ||
pgm = parse(definition_dir, text) | ||
pattern = config(pgm) | ||
result = llvm_interpret(definition_dir, pattern) | ||
actual = extract(definition_dir, result) | ||
|
||
# Then | ||
assert actual == expected | ||
|
||
|
||
def parse(definition_dir: Path, text: str) -> Pattern: | ||
from subprocess import CalledProcessError | ||
|
||
from pyk.kore.parser import KoreParser | ||
from pyk.utils import run_process_2 | ||
|
||
parser = definition_dir / 'parser_PGM' | ||
args = [str(parser), '/dev/stdin'] | ||
|
||
try: | ||
kore_text = run_process_2(args, input=text).stdout | ||
except CalledProcessError as err: | ||
raise ValueError(err.stderr) from err | ||
|
||
return KoreParser(kore_text).pattern() | ||
|
||
|
||
def config(pgm: Pattern) -> Pattern: | ||
from pyk.kore.prelude import SORT_K_ITEM, inj, top_cell_initializer | ||
from pyk.kore.syntax import SortApp | ||
|
||
return top_cell_initializer( | ||
{ | ||
'$PGM': inj(SortApp('SortExpr'), SORT_K_ITEM, pgm), | ||
} | ||
) | ||
|
||
|
||
def extract(definition_dir: Path, pattern: Pattern) -> int | bool: | ||
from pyk.kore.syntax import DV, App, String | ||
|
||
match pattern: | ||
case App( | ||
"Lbl'-LT-'generatedTop'-GT-'", | ||
args=( | ||
App( | ||
"Lbl'-LT-'k'-GT-'", | ||
args=( | ||
App( | ||
'kseq', | ||
args=( | ||
App('inj', args=(DV(value=String(res)),)), | ||
App('dotk'), | ||
), | ||
), | ||
), | ||
), | ||
*_, | ||
), | ||
): | ||
try: | ||
return int(res) | ||
except Exception: | ||
pass | ||
match res: | ||
case 'true': | ||
return True | ||
case 'false': | ||
return False | ||
|
||
pretty_pattern = pretty(definition_dir, pattern) | ||
raise ValueError(f'Cannot extract result from pattern:\n{pretty_pattern}') | ||
|
||
|
||
def pretty(definition_dir: Path, pattern: Pattern) -> str: | ||
from pyk.kore.tools import kore_print | ||
|
||
return kore_print(pattern, definition_dir=definition_dir) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from __future__ import annotations | ||
|
||
from itertools import count | ||
from typing import TYPE_CHECKING | ||
|
||
import pytest | ||
|
||
from kimp import KImp | ||
|
||
if TYPE_CHECKING: | ||
from typing import Final | ||
|
||
Env = dict[str, int] | ||
|
||
|
||
TEST_DATA: Final[tuple[tuple[Env, str, Env], ...]] = ( | ||
({}, '{}', {}), | ||
({'x': 0}, '{}', {'x': 0}), | ||
({}, 'x = 1;', {'x': 1}), | ||
({}, 'x = 1 + 1;', {'x': 2}), | ||
({'x': 0}, 'x = 1;', {'x': 1}), | ||
({'x': 0, 'y': 1}, 'x = y;', {'x': 1, 'y': 1}), | ||
({'x': 1}, 'x = false;', {'x': False}), | ||
({}, '{ x = 0; }', {'x': 0}), | ||
({}, 'x = 0; x = 1;', {'x': 1}), | ||
({}, 'x = 0; y = 1;', {'x': 0, 'y': 1}), | ||
({'x': 0, 'y': 1}, 'z = x; x = y; y = z;', {'x': 1, 'y': 0, 'z': 0}), | ||
({'b': True}, 'if (b) x = 1;', {'b': True, 'x': 1}), | ||
({'b': False}, 'if (b) x = 1;', {'b': False}), | ||
({'b': True}, 'if (b) x = 1; else x = 2;', {'b': True, 'x': 1}), | ||
({'b': False}, 'if (b) x = 1; else x = 2;', {'b': False, 'x': 2}), | ||
({'x': 2}, 'while (x > 0) x = x - 1;', {'x': 0}), | ||
) | ||
|
||
|
||
@pytest.mark.parametrize('env,pgm,expected', TEST_DATA, ids=count()) | ||
def test_kimp(env: Env, pgm: str, expected: Env) -> None: | ||
# Given | ||
kimp = KImp() | ||
|
||
# When | ||
pattern = kimp.pattern(pgm=pgm, env=env) | ||
result = kimp.run(pattern, depth=1000) | ||
actual = kimp.env(result) | ||
|
||
# Then | ||
assert actual == expected |
This file was deleted.
Oops, something went wrong.