cli: lower boolean builtin operands iteratively
This commit is contained in:
1 parent
627ad8f857
commit
5cc69393e4
5 files changed
+323
-27
No files matched your search
@@ -636,16 +636,103 @@ with tempfile.TemporaryDirectory(prefix='aji-expression-depth-') as directory:
|
||||
assert cli('lower', formatted) == lowered
|
||||
print(f'empty variant depth: {depth}', flush=True)
|
||||
|
||||
# Builtin callbacks are still native; preserve the prior diagnostic at
|
||||
# bounded nesting while ordinary calls gain explicit continuations.
|
||||
for depth in (16, 64, 256):
|
||||
path = root / f'nested-assert-{depth}.aji'
|
||||
path.write_text('module depth\nfn main() -> i64 effects [] { ' +
|
||||
'assert(' * depth + 'true' + ')' * depth + '; return 1i64; }\n')
|
||||
expected = cli('check', path, code=1)
|
||||
assert json.loads(expected)['code'] == 'E7005'
|
||||
for command in ('lower', 'run', 'report', 'explore'):
|
||||
assert cli(command, path, code=1) == expected
|
||||
# Boolean builtin operands share the expression driver. Void-valued
|
||||
# nested assertions keep their exact inner diagnostic at arbitrary depth.
|
||||
for depth in (2, 16, 64, 256, 1024, 16384, 65536):
|
||||
for kind in ('assert', 'assume', 'alternating'):
|
||||
names = [kind if kind != 'alternating' else ('assert' if i % 2 == 0 else 'assume')
|
||||
for i in range(depth)]
|
||||
prefix = 'module depth\nfn main() -> i64 effects [unsafe] { '
|
||||
expression = ''.join(name + '(' for name in names) + 'true' + ')' * depth
|
||||
path = root / f'nested-{kind}-{depth}.aji'
|
||||
path.write_text(prefix + expression + '; return 1i64; }\n')
|
||||
expected = cli('check', path, code=1)
|
||||
error = json.loads(expected)
|
||||
inner_col = len(prefix.split('\n')[-1]) + sum(len(name) + 1 for name in names[:-1]) + 1
|
||||
assert (error['code'], error['expected'], error['found'], error['file'], error['line'], error['col']) == (
|
||||
'E7005', 'bool', 'void', str(path), 2, inner_col), error
|
||||
for command in ('lower', 'run', 'report', 'explore'):
|
||||
assert cli(command, path, code=1) == expected
|
||||
formatted = path.with_suffix('.formatted.aji')
|
||||
output = cli('fmt', path)
|
||||
formatted.write_bytes(output)
|
||||
assert cli('fmt', formatted) == output
|
||||
assert json.loads(cli('check', formatted, code=1))['code'] == 'E7005'
|
||||
print(f'boolean builtin rejection depth: {depth}', flush=True)
|
||||
|
||||
for name in ('assert', 'assume'):
|
||||
for depth in (0, 1, 16, 256, 1024, 16384, 65536):
|
||||
expression = 'bool_id(' * depth + 'true' + ')' * depth
|
||||
path = root / f'boolean-builtin-{name}-{depth}.aji'
|
||||
path.write_text('module depth\nfn bool_id(v: bool) -> bool effects [] { return v; }\n'
|
||||
f'fn main() -> void effects [{"unsafe" if name == "assume" else ""}] {{ '
|
||||
f'{name}({expression}); }}\n')
|
||||
assert cli('check', path) == b''
|
||||
lowered = cli('lower', path)
|
||||
instructions = sum(b' = ' in line or line.strip().startswith((b'ret', b'assert ', b'assume '))
|
||||
for line in lowered.splitlines())
|
||||
assert instructions == depth + 4, (name, depth, instructions)
|
||||
if depth <= 1024:
|
||||
fuel = 2 * depth + 3
|
||||
result = cli('run', path, '--fuel', fuel)
|
||||
assert json.loads(result) == {'status': 'returned', 'value': None}, result
|
||||
assert json.loads(cli('run', path, '--fuel', fuel - 1, code=5)) == {
|
||||
'status': 'limit_exceeded', 'kind': 'fuel', 'limit': fuel - 1, 'steps': fuel - 1}
|
||||
air = path.with_suffix('.air')
|
||||
air.write_bytes(lowered)
|
||||
assert cli('run', air, '--fuel', fuel) == result
|
||||
formatted = path.with_suffix('.formatted.aji')
|
||||
output = cli('fmt', path)
|
||||
formatted.write_bytes(output)
|
||||
assert cli('fmt', formatted) == output
|
||||
assert cli('check', formatted) == b''
|
||||
assert cli('lower', formatted) == lowered
|
||||
print(f'boolean builtin accepted depth: {name}', flush=True)
|
||||
|
||||
# Final supplemental probes: stable feature/operation spelling, traps,
|
||||
# hosted run records and inner arity/type priority through mixed calls.
|
||||
for name in ('assert', 'assume'):
|
||||
for depth in (0, 16, 1024):
|
||||
expression = 'bool_id(' * depth + 'false' + ')' * depth
|
||||
prefix = 'module depth\nfn bool_id(v: bool) -> bool effects [] { return v; }\n'
|
||||
main = f'fn main() -> void effects [{"unsafe" if name == "assume" else ""}] {{ '
|
||||
path = root / f'boolean-builtin-trap-{name}-{depth}.aji'
|
||||
path.write_text(prefix + main + f'{name}({expression}); }}\n')
|
||||
lowered = cli('lower', path)
|
||||
header = next(line for line in lowered.splitlines() if line.startswith(b'features ['))
|
||||
assert (b'unsafe' in header) == (name == 'assume'), header
|
||||
operations = [line.strip() for line in lowered.splitlines() if line.strip().startswith((b'assert ', b'assume '))]
|
||||
assert len(operations) == 1 and operations[0].startswith(name.encode() + b' %'), operations
|
||||
fuel = 2 * depth + 2
|
||||
trapped = json.loads(cli('run', path, '--fuel', fuel, code=4))
|
||||
assert (trapped['status'], trapped['code'], trapped['kind'], trapped['inst']) == (
|
||||
'trapped', 'T0009' if name == 'assume' else 'T0002',
|
||||
'trap.unsafe_precondition' if name == 'assume' else 'trap.assert', depth + 1), trapped
|
||||
assert trapped.pop('source_location') == {'file': str(path), 'line': 3, 'col': len(main) + 1}
|
||||
air = path.with_suffix('.air')
|
||||
air.write_bytes(lowered)
|
||||
assert json.loads(cli('run', air, '--fuel', fuel, code=4)) == trapped
|
||||
record = path.with_suffix('.json')
|
||||
assert cli('run', path, '--fuel', fuel, '--profile', 'hosted', '--result-file', record, code=4) == b''
|
||||
hosted = json.loads(record.read_text())
|
||||
assert hosted == trapped, hosted
|
||||
|
||||
for leaf, found in (('assert()', None), ('assume(unknown, false)', None), ('assert(1i64)', 'i64')):
|
||||
for depth in (16, 1024, 65536):
|
||||
prefix = 'module depth\nfn bool_id(v: bool) -> bool effects [] { return v; }\nfn main() -> void effects [] { '
|
||||
expression = 'assert(bool_id(' * depth + leaf + '))' * depth
|
||||
path = root / f'boolean-builtin-leaf-{leaf.split("(")[0]}-{found}-{depth}.aji'
|
||||
path.write_text(prefix + expression + '; }\n')
|
||||
expected = cli('check', path, code=1)
|
||||
error = json.loads(expected)
|
||||
col = len(prefix.split('\n')[-1]) + len('assert(bool_id(') * depth + 1
|
||||
if found is not None:
|
||||
col += len('assert(')
|
||||
assert (error['expected'], error['found']) == ('bool', found), error
|
||||
assert (error['code'], error['line'], error['col'], error['file']) == ('E7005', 3, col, str(path)), error
|
||||
for command in ('lower', 'run', 'report', 'explore'):
|
||||
assert cli(command, path, code=1) == expected
|
||||
print(f'boolean builtin leaf priority: {leaf}', flush=True)
|
||||
|
||||
# Block/control callbacks remain native. Their existing shallow/deeper
|
||||
# syntax must retain its capacity while expression dispatch is moved out
|
||||
|
||||
Reference in new issue
Block a user