cli: lower ordinary call arguments iteratively
This commit is contained in:
1 parent
bda43d2ecb
commit
0791a50eb9
5 files changed
+648
-126
No files matched your search
@@ -319,18 +319,69 @@ with tempfile.TemporaryDirectory(prefix='aji-expression-depth-') as directory:
|
||||
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)
|
||||
# Ordinary calls suspend in the same driver as unary/binary operands.
|
||||
# Count calls and callee rets independently of the emitted AIR.
|
||||
for depth in (0, 1, 16, 1024, 16384, 65536):
|
||||
for kind in ('call', 'add-call', 'neg-call', 'generic-call'):
|
||||
prefix = {'call': 'id(', 'add-call': '1i64 + id(',
|
||||
'neg-call': '- id(', 'generic-call': 'id::<i64>('}[kind]
|
||||
expression = prefix * depth + '1i64' + ')' * depth
|
||||
generic = kind == 'generic-call'
|
||||
helper = ('fn id<T>(x: T) -> T effects [] { return move x; }\n' if generic
|
||||
else 'fn id(x: i64) -> i64 effects [] { return x; }\n')
|
||||
path = root / f'nested-{kind}-{depth}.aji'
|
||||
path.write_text('module depth\n' + helper +
|
||||
'fn main() -> i64 effects [] { return ' + expression + '; }\n')
|
||||
assert cli('check', path) == b''
|
||||
lowered = cli('lower', path)
|
||||
# Generic id is instantiated only if called; ordinary id is always emitted.
|
||||
helper_instructions = 0 if generic and depth == 0 else 1
|
||||
main_instructions = (3 if kind in ('add-call', 'neg-call') else 1) * depth + 2
|
||||
instructions = sum(b' = ' in line or line.strip().startswith(b'ret ') for line in lowered.splitlines())
|
||||
assert instructions == main_instructions + helper_instructions, (kind, depth, instructions)
|
||||
if depth <= 1024:
|
||||
fuel = main_instructions + depth
|
||||
value = depth + 1 if kind == 'add-call' else (-1 if kind == 'neg-call' 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)
|
||||
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}
|
||||
assert cli('check', formatted) == b''
|
||||
assert cli('lower', formatted) == lowered
|
||||
print(f'nested call depth: {kind} {depth}', flush=True)
|
||||
|
||||
for depth in (1024, 16384, 65536):
|
||||
path = root / f'nested-call-invalid-{depth}.aji'
|
||||
header = 'fn main() -> i64 effects [] { return '
|
||||
prefix = '1i64 + id(' * depth
|
||||
path.write_text('module depth\nfn id(x: i64) -> i64 effects [] { return x; }\n' +
|
||||
header + prefix + 'missing()' + ')' * depth + '; }\n')
|
||||
expected = cli('check', path, code=1)
|
||||
error = json.loads(expected)
|
||||
assert (error['code'], error['line'], error['col'], error['file'], error['name']) == (
|
||||
'E7002', 3, len(header) + len(prefix) + 1, str(path), 'missing'), error
|
||||
for command in ('lower', 'run', 'report', 'explore'):
|
||||
assert cli(command, path, code=1) == expected
|
||||
print(f'nested call 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):
|
||||
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
|
||||
|
||||
# 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