dmsc / fastbasic

FastBasic - Fast BASIC interpreter for the Atari 8-bit computers
GNU General Public License v2.0
139 stars 20 forks source link

Proposed enhancement FUNCTION or DEF FN #69

Open marianodominguez opened 1 year ago

marianodominguez commented 1 year ago

This is a thing lacking in all Atari Basic implementations, ability to define functions. (same as PROC defines rubroutines)

https://zxbasic.readthedocs.io/en/docs/function/

I propose using the stack for being able to do recursive functions, if local variables are too complex, this implementation uses only parameter copy.

That allows for shorter code. eg

160 XX=X+1:EXEC HS:HS1=V:XX=X-1:EXEC HS:HS2=V:XX=X+3:EXEC HS:HS3=V:XX=X-3:EXEC HS:HS4=V 170 YT=(H-L)HS1+(R-H)HS2+(L-W)HS3+(W-R)HS4+W becomes 170 YT=(H-L)HS(X+1)+(R-H)HS(X-1)+(L-W)HS(X+3)+(W-R)HS(X-3)+W

dmsc commented 1 year ago

Note that your example is better written using a procedure with parameters:

  @HS X+1 : YT = V*(H-L) : @HS X-1 : YT = YT + V*(R-H) : @HS X+3 : YT = YT + V*(L-W) : @HS X-3 : YT = YT + V*(W-R)
  PROC HS COORD
    V = ........ ' your expression here
  ENDPROC  

The problem with functions is that, for the 6502 compiler to work, you can´t write to variables or call statements from a function - and also you could not nest USR() functions.

Having a limited DEF FN implementation could work - it has the benefit of having a standard naming convention for functions ( FN* ).