cli: parse control flow with shared continuations
This commit is contained in:
5 files changed
+1442
-345
No files matched your search
@@ -1370,4 +1370,79 @@ with tempfile.TemporaryDirectory(prefix='aji-place-base-depth-') as directory:
|
||||
print('place base contexts and rejection positions passed', flush=True)
|
||||
|
||||
|
||||
with tempfile.TemporaryDirectory(prefix='aji-control-parser-depth-') as directory:
|
||||
root = Path(directory)
|
||||
for kind in ('block', 'if', 'else-if'):
|
||||
depths = (1, 16, 256, 1024) if kind != 'else-if' else (1, 16, 256, 1024, 16384, 65536)
|
||||
for depth in depths:
|
||||
if kind == 'block':
|
||||
expression = '{ ' * depth + '0usize' + ' }' * depth
|
||||
elif kind == 'if':
|
||||
expression = 'if true { ' * depth + '0usize' + ' } else { 1usize }' * depth
|
||||
else:
|
||||
expression = 'if false { 1usize } else ' * depth + '{ 0usize }'
|
||||
path = root / f'control-parser-{kind}-{depth}.aji'
|
||||
path.write_text('module depth\nfn main() -> usize effects [] { return ' + expression + '; }\n')
|
||||
formatted = path.with_suffix('.formatted.aji')
|
||||
output = cli('fmt', path)
|
||||
formatted.write_bytes(output)
|
||||
assert cli('fmt', formatted) == output
|
||||
if depth <= 256:
|
||||
assert cli('check', path) == b''
|
||||
lowered = cli('lower', path)
|
||||
assert cli('lower', formatted) == lowered
|
||||
if depth <= 16:
|
||||
air = path.with_suffix('.air')
|
||||
air.write_bytes(lowered)
|
||||
options = ('--quantum', 1, '--schedule-seed', 7, '--fuel', 4096)
|
||||
result = cli('run', path, *options)
|
||||
assert json.loads(result) == {'status': 'returned', 'value': 0}, (kind, depth, result)
|
||||
assert cli('run', air, *options) == result
|
||||
print(f'control parser {kind} {depth} passed', flush=True)
|
||||
|
||||
# Syntax-only control contexts avoid conflating parser depth with the
|
||||
# still-open lowering/liveness audit or requiring semantic task fixtures.
|
||||
syntax_forms = (
|
||||
('let', '{ let x = ', '; x }'),
|
||||
('return', '{ return ', '; }'),
|
||||
('while', '{ while true { ', '; } }'),
|
||||
('for', '{ for i in 0usize..1usize { ', '; } }'),
|
||||
('loop', '{ loop { ', '; } }'),
|
||||
('scope', '{ scope s { ', '; } }'),
|
||||
('shield', '{ shield { ', '; } }'),
|
||||
('lock', '{ lock ref m as x { ', '; } cancelled {} }'),
|
||||
('match', 'match e { _ => ', ' }'),
|
||||
('select-send', '{ select { send(tx, ', ') { ok => {} } cancelled => {} } }'),
|
||||
('select-outcome', '{ select { recv(rx) { ok => ', ' } cancelled => {} } }'),
|
||||
('select-cancelled', '{ select { cancelled => ', ' } }'),
|
||||
)
|
||||
for label, before, after in syntax_forms:
|
||||
path = root / f'control-syntax-{label}.aji'
|
||||
path.write_text('module depth\nfn main() -> usize effects [] { return ' + before * 256 + '0usize' + after * 256 + '; }\n')
|
||||
output = cli('fmt', path)
|
||||
formatted = path.with_suffix('.formatted.aji')
|
||||
formatted.write_bytes(output)
|
||||
assert cli('fmt', formatted) == output
|
||||
|
||||
# Six sequential CLI processes at each depth. The 32,768 mixed check
|
||||
# measured 350,804 KiB max RSS on Linux/Debug; this is informational,
|
||||
# environment-dependent evidence, not a CI memory bound.
|
||||
for mixed in (False, True):
|
||||
for depth in (16, 1024, 32768):
|
||||
forms = syntax_forms if mixed else syntax_forms[:1]
|
||||
expression = ''.join(forms[i % len(forms)][1] for i in range(depth)) + '!0usize' + ''.join(forms[i % len(forms)][2] for i in reversed(range(depth)))
|
||||
path = root / f'control-error-{mixed}-{depth}.aji'
|
||||
source = '// UTF-8: λ\r\nmodule depth\r\nfn main() -> usize effects [] { return ' + expression + '; }\r\n'
|
||||
path.write_bytes(source.encode())
|
||||
rejected = cli('check', path, code=1)
|
||||
record = json.loads(rejected)
|
||||
pos = source.index('!0usize')
|
||||
assert (record['code'], record['line'], record['col']) == ('E7001', 3, pos - source.rfind('\n', 0, pos)), record
|
||||
assert record['expected'] == 'an expression' and record['found'] == '!', record
|
||||
assert record['repair_candidates'][0]['kind'] == 'boolean_negation', record
|
||||
for command in ('lower', 'run', 'report', 'explore', 'fmt'):
|
||||
assert cli(command, path, code=1) == rejected
|
||||
print('control parser syntax and first-error positions passed', flush=True)
|
||||
|
||||
|
||||
print(f'source expression depth: {checks} checks passed')
|
||||
Reference in new issue
Block a user