Skip to content

Add functions - #128

Merged
dmsc merged 8 commits into
masterfrom
add-functions
Oct 20, 2026
Merged

dmsc merged 8 commits into
masterfrom
add-functions

Conversation

@dmsc

@dmsc dmsc commented Oct 20, 2026

Copy link
Copy Markdown
Owner

Add functions to FastBasic.

This PR adds a RETURN statement that can return values from a PROC, and a @ syntax that allows calling the functions from any expression.

Fixes #69 and #109.

dmsc added 8 commits October 19, 2026 22:55
PUSH_SADDR pushes the lvalue pointer to the 6502 stack.  POP_SADDR
restores it while preserving A and X, so a function result in AX
survives.  Y is clobbered but next_instruction reloads it from sptr.

Aided by Opencode / DeepSeek V4.1 Flash 1017
Add RETURN expr for procedures, returning an integer value in AX.  RETURN
inside an active FOR loop is supported by emitting one TOK_FOR_EXIT per
loop before TOK_RET, so the AX result survives.

The native parser uses a single bad RETURN error for invalid returns; the
cross compiler keeps explanatory outside-PROC messages.  Add tests for
returns inside nested FOR loops and repeated function calls.

Aided by Opencode / DeepSeek V4.1 Flash 1017
Add @name(args) to integer expressions.  Calls use PUSH_SADDR/POP_SADDR
around argument evaluation so array and memory destinations survive the
call; the cross compiler removes unnecessary pairs.

TOK_RET now preserves A because the function result is in AX.  The
native parser shares its variable stack for saved labels and inlines the
one-use FUNC_ARGS rule.  Function argument counts use the empty
FUNC_ARG_OK hook implemented by E_COUNT_PARAM in the cross compiler.

Add tests for calls, nested calls, recursion, parser stack use, and
SADDR preservation in byte and word array assignments.

Aided by Opencode / DeepSeek V4.1 Flash 1017
Add a cross-compiler peephole pass that tracks pending SADDR lvalue
addresses and removes PUSH_SADDR/POP_SADDR pairs around calls when no
address is live.  It handles POKE, DPOKE, NUM_POKE, BYTE_POKE,
FP_STORE, COPY_STR, INC and DEC as consumers, and drops nested saves
inside an already-saved region.

Keep the native parser unchanged and always emitting the wrappers.

Add tests for function calls in POKE/DPOKE address and value
expressions, including functions whose bodies clobber SADDR.

Aided by Opencode / DeepSeek V4.1 Flash 1017
Return success booleans from InputLine and InputFilename, and rename
AskSaveFileChanged to CheckUnsavedChanges with a positive "may continue"
result.  This removes the old KEY-variable convention from the callers
while keeping the IDE the same size.

Aided by Opencode / DeepSeek V4.1 Flash 1017
Use the existing conditional return-of-zero tokens when a conditional
branch contains only RETURN 0.  This removes the jump, constant load and
RET while preserving the preceding COMP_0, so full 16-bit non-zero
function results are handled correctly.

Add a test covering zero, odd, even and 16-bit non-zero values in both
branch directions; declare the test variable with DIM before use.

Aided by Opencode / DeepSeek V4.1 Flash 1017
Mark labels called with @name(...) as functions and flag any RETURN
inside the current PROC.  At the end of the cross-compiler parse, report
an error if a function was used in an expression but never contains a
RETURN.

This deliberately checks only for at least one RETURN, not full
control-flow coverage, matching the language's permissive behavior.

Add cross-only error coverage and a test that conditional-only returns
are accepted.  Native compiler behavior is unchanged.

Aided by Opencode / DeepSeek V4.1 Flash 1017
Make the RETURN expression optional.  A bare RETURN exits the current
PROC, unwinding active FOR state, without producing a value.  This is
useful for early exits from nested loops.

Only value-returning RETURN statements mark a function for the
cross-compiler diagnostic; a bare RETURN does not count.

FBI.XEX grows from 8299 to 8304 bytes.

Aided by Opencode / DeepSeek V4.1 Flash 1017
@dmsc
dmsc merged commit 28e4cba into master Oct 20, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Proposed enhancement FUNCTION or DEF FN

1 participant