cli: lower struct fields iteratively

This commit is contained in:
dev committed 2026-10-11 00:22:18 +03:00
1 parent 0791a50eb9
commit 6d2c4d40a8
5 files changed
+557 -53

No files matched your search

+78
View File
@@ -372,6 +372,84 @@ with tempfile.TemporaryDirectory(prefix='aji-expression-depth-') as directory:
assert cli(command, path, code=1) == expected
print(f'nested call invalid depth: {depth}', flush=True)
# Constructors and value-field reads share the scalar/call driver.
for depth in (0, 1, 16, 1024, 16384, 32768, 65536):
for kind in ('field', 'length-field', 'mixed-field', 'generic-field'):
if depth == 32768 and kind != 'mixed-field':
continue
field = 'len' if kind == 'length-field' else 'n'
generic = kind == 'generic-field'
name = 'Box::<i64>' if generic else 'Box'
prefix = name + ' { ' + field + ': '
if kind == 'mixed-field':
prefix += '- id('
suffix = (')' if kind == 'mixed-field' else '') + ' }.' + field
expression = prefix * depth + '1i64' + suffix * depth
declaration = ('struct Box<T> { n: T }\n' if generic else
'struct Box { ' + field + ': i64 }\n')
helper = 'fn id(x: i64) -> i64 effects [] { return x; }\n' if kind == 'mixed-field' else ''
path = root / f'struct-{kind}-{depth}.aji'
path.write_text('module depth\n' + declaration + helper +
'fn main() -> i64 effects [] { return ' + expression + '; }\n')
main_instructions = (5 if kind == 'mixed-field' else 2) * depth + 2
limited = main_instructions > 262144
expected_check = cli('check', path, code=1 if limited else 0)
if limited:
diagnostic = json.loads(expected_check)
assert (diagnostic['code'], diagnostic['limit'], diagnostic['max'], diagnostic['found']) == (
'E5101', 'instructions per function', 262144, main_instructions), diagnostic
for command in ('run', 'report', 'explore'):
assert cli(command, path, code=1) == expected_check
else:
assert expected_check == b''
lowered = cli('lower', path)
instructions = sum((line.lstrip().startswith(b'%') and b' = ' in line) or line.strip().startswith(b'ret ') for line in lowered.splitlines())
assert instructions == main_instructions + (1 if helper else 0), (kind, depth, instructions)
if depth <= 1024:
fuel = main_instructions + (depth if helper else 0)
value = -1 if kind == 'mixed-field' and depth % 2 else 1
assert json.loads(cli('run', path, '--fuel', fuel)) == {'status': 'returned', 'value': value}
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) == cli('run', path, '--fuel', fuel)
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}
formatted_check = cli('check', formatted, code=1 if limited else 0)
if limited:
diagnostic = json.loads(formatted_check)
assert (diagnostic['code'], diagnostic['limit'], diagnostic['max'], diagnostic['found']) == (
'E5101', 'instructions per function', 262144, main_instructions), diagnostic
else:
assert formatted_check == b''
assert cli('lower', formatted) == lowered
print(f'struct field depth: {kind} {depth}', flush=True)
for depth in (1024, 16384, 65536):
# Exact missing/duplicate/unknown-field priorities at the inner leaf.
for leaf, code, reason in (('Box {}.n', 'E7006', 'invalid_literal'),
('Box { n: 1i64, n: false }.n', 'E7006', 'invalid_literal'),
('Box { missing: unknown }.n', 'E7002', None)):
path = root / f'struct-invalid-{depth}-{code}-{len(leaf)}.aji'
header = 'fn main() -> i64 effects [] { return '
prefix = 'Box { n: ' * depth
path.write_text('module depth\nstruct Box { n: i64 }\n' + header + prefix + leaf + ' }.n' * depth + '; }\n')
expected = cli('check', path, code=1)
error = json.loads(expected)
offset = 0 if 'Box {}' in leaf else (len('Box { n: 1i64, ') if code == 'E7006' else len('Box { '))
assert (error['code'], error['line'], error['col'], error['file']) == (
code, 3, len(header) + len(prefix) + offset + 1, str(path)), error
if reason:
assert error['reason'] == reason, error
for command in ('lower', 'run', 'report', 'explore'):
assert cli(command, path, code=1) == expected
print(f'struct field invalid 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):