valoeghese / Jason-Basic

Programming language for niche discord bot in private server
Other
2 stars 2 forks source link

Better handling of Subroutines #9

Closed valoeghese closed 1 month ago

valoeghese commented 3 months ago

GOTO Label goes to a specified label. What if we want something like a function that can return?

We could use a stack, and something like CALL and RETURN commands, but I have two propositions for ways that are more aligned with the simplicity style of the language.

Jump to Variable

A simple way could be letting the user jump to a label stored in a variable. This is less readable but really flexible (and could lead to some funny looking programs which is a plus).

% Return to the label resolved in expression.
RETURN (expression)

Or

% Return to the label resolved in expression.
GOTO LABEL (expression)

END (Label)

This effectively implements CALL and RETURN aligns well with the way the rest of the language looks and feels. We have to keep a call stack of labels that have matching "END label" commands however. If this is added potentially GOTO between sections should be disallowed. (Labels inside subroutines cannot be jumped to outside the subroutine)

a = 5 
GOTO Process
END

# subroutine Process
Process:
PRINT a
END Process
valoeghese commented 1 month ago

I think GOTO LABEL should be implemented

valoeghese commented 1 month ago

Done. More than one token in GOTO is interpreted as a dynamic GOTO. It is recommended for clarity to put brackets around dynamic GOTO