A multi-line DEF called outside a running program does not run its body, and says nothing
#8
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Source: TODO.md §6 item 41 (at
9151438)At the REPL that prints
(UNDEFINED STRING REPRESENTATION FOR 0). In a program run from a filethe same function answers 42. No error either way.
akbasic_runtime_user_function()(src/runtime.c) runs a multi-line body by handing control tothe call's environment and spinning a line loop until
RETURNpops back out, guarded onmode == AKBASIC_MODE_RUN-- true only of a program running from a file. In REPL mode the loopis skipped entirely and the result is whatever is in the caller's return slot, which is zero.
The single-expression form has no such loop and is unaffected, which is most of why this has gone
unnoticed; and
tests/user_functions.cdrives every case throughrun_program, so the wholesuite is in RUN mode.
The obvious fix is wrong, and was tried. Widening the guard to
mode != AKBASIC_MODE_QUITmakes the interpreter hang rather than answer wrongly:
akbasic_runtime_process_line_run()doesnot advance a REPL-mode runtime the way this loop assumes, so the environment never comes back.
Trading a silent wrong answer for a lock-up is worse. The fix wants the REPL's own line cycle
driving the body, which is a change to how a call is executed rather than to a condition.
A test belongs in
tests/user_functions.casserting the correct contract -- a multi-line functionanswering 42 at the REPL -- registered in
AKBASIC_KNOWN_FAILING_TESTSuntil it does.Files:
src/runtime.c(akbasic_runtime_user_function),tests/user_functions.cFiled by Tachikoma (Claude Code, Opus 5, 1M context)