cli: parse nested expression contexts iteratively

This commit is contained in:
dev committed 2026-10-10 22:13:08 +03:00
1 parent 371329f3f0
commit bda43d2ecb
5 files changed
+1107 -221

No files matched your search

+91 -1
View File
@@ -1,5 +1,5 @@
#!/usr/bin/env python3
"""Operand/generic depth, exact lowering/CFG/fuel and rejection positions (Q-061)."""
"""Expression parsing/lowering depth, exact CFG/fuel and rejection positions (Q-061)."""
import json
from pathlib import Path
import subprocess
@@ -264,6 +264,96 @@ with tempfile.TemporaryDirectory(prefix='aji-expression-depth-') as directory:
for command in ('lower', 'run', 'report', 'explore'):
assert json.loads(cli(command, path, code=1)) == error
# Grouping is transparent in the AST; alternating binary/group frames
# also reach ordinary lowering, with independently counted AIR and fuel.
for depth in (0, 1, 16, 1024, 16384, 65536):
for kind in ('group', 'grouped-add'):
expression = ('(' * depth + '1i64' + ')' * depth if kind == 'group'
else '1i64 + (' * depth + '1i64' + ')' * depth)
instructions = 2 if kind == 'group' else 2 * depth + 2
value = 1 if kind == 'group' else depth + 1
path = root / f'{kind}-{depth}.aji'
path.write_text('module depth\nfn main() -> i64 effects [] { return ' + expression + '; }\n')
assert cli('check', path) == b''
lowered = cli('lower', path)
assert sum(b' = ' in line or line.strip().startswith(b'ret ') for line in lowered.splitlines()) == instructions
if depth <= 1024:
assert json.loads(cli('run', path, '--fuel', instructions)) == {'status': 'returned', 'value': value}
assert json.loads(cli('run', path, '--fuel', instructions - 1, code=5)) == {
'status': 'limit_exceeded', 'kind': 'fuel', 'limit': instructions - 1, 'steps': instructions - 1}
formatted = path.with_suffix('.formatted.aji')
output = cli('fmt', path)
formatted.write_bytes(output)
assert cli('fmt', formatted) == output
assert json.loads(cli('fmt', formatted, '--check')) == {
'file': str(formatted), 'status': 'formatted', 'changed': False}
assert cli('lower', formatted) == lowered
print(f'grouping depth: {kind} {depth}', flush=True)
forms = (
('(', ')'), ('f(', ', 2u8)'), ('value[', ']'), ('value[0u8..', ']'),
('[', ', 2u8]'), ('[', '; 1]'), ('Record { field: ', ', other: 2u8 }'),
('channel.make<mut<ref<u8>>>(', ')'), ('ns.E::<u8>::A(', ')'),
('not (', ')'), ('1u8 + (', ')'), ('(', ') as u8'), ('ref *(', ')'),
)
header = 'fn f() -> void effects [] { let x = '
for depth in (16, 1024, 16384, 32768):
prefix = ''.join(forms[i % len(forms)][0] for i in range(depth))
suffix = ''.join(forms[i % len(forms)][1] for i in reversed(range(depth)))
path = root / f'mixed-syntax-{depth}.aji'
path.write_text('module depth\n' + header + prefix + 'leaf' + suffix + '; }\n')
# Syntax/formatter coverage only: these synthetic expressions need not
# have compatible types. Other non-scalar lowering remains Q-061 work.
output = cli('fmt', path)
formatted = path.with_suffix('.formatted.aji')
formatted.write_bytes(output)
assert cli('fmt', formatted) == output
assert json.loads(cli('fmt', formatted, '--check')) == {
'file': str(formatted), 'status': 'formatted', 'changed': False}
path.write_text('module depth\n' + header + prefix + '!' + suffix + '; }\n')
error = json.loads(cli('check', path, code=1))
assert (error['code'], error['file'], error['line'], error['col']) == (
'E7001', str(path), 2, len(header) + len(prefix) + 1), error
assert error['repair_candidates'][0]['kind'] == 'boolean_negation', error
for command in ('lower', 'run', 'report', 'explore', 'fmt'):
assert json.loads(cli(command, path, code=1)) == error
print(f'mixed syntax depth: {depth}', flush=True)
for depth in (16, 1024, 16384, 65536):
path = root / f'nested-call-syntax-{depth}.aji'
expression = '1i64 + id(' * depth + '1i64' + ')' * depth
path.write_text('module depth\nfn id(x: i64) -> i64 effects [] { return x; }\n'
'fn main() -> i64 effects [] { return ' + expression + '; }\n')
output = cli('fmt', path)
formatted = path.with_suffix('.formatted.aji')
formatted.write_bytes(output)
assert cli('fmt', formatted) == output
assert json.loads(cli('fmt', formatted, '--check')) == {
'file': str(formatted), 'status': 'formatted', 'changed': False}
print(f'nested call syntax depth: {depth}', flush=True)
# Block/control callbacks remain native. Their existing shallow/deeper
# syntax must retain its capacity while expression dispatch is moved out
# of the native stack, including explicit-return callback re-entry.
for depth in (64, 256):
for kind in ('block', 'if', 'returned-block', 'returned-if'):
if kind == 'block':
expression = '{' * depth + '1i64' + '}' * depth
elif kind == 'if':
expression = 'if true {' * depth + '1i64' + '} else {1i64}' * depth
elif kind == 'returned-block':
expression = '{return ' * depth + '1i64' + ';}' * depth
else:
expression = 'if true {return ' * depth + '1i64' + ';} else {return 1i64;}' * depth
path = root / f'control-syntax-{kind}-{depth}.aji'
path.write_text('module depth\nfn main() -> i64 effects [] { return ' + expression + '; }\n')
output = cli('fmt', path)
formatted = path.with_suffix('.formatted.aji')
formatted.write_bytes(output)
assert cli('fmt', formatted) == output
assert json.loads(cli('fmt', formatted, '--check')) == {
'file': str(formatted), 'status': 'formatted', 'changed': False}
# Signed boundary literal remains one negative constant; the outer
# checked negation traps rather than changing literal acceptance.
minimum = root / 'minimum.aji'