Repository navigation
Add functions - #128
Merged
Merged
Add functions#128
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.