X16Community / x16-rom

Other
43 stars 27 forks source link

BASLOAD breaks BASIC DEF statement #346

Closed jestin closed 2 months ago

jestin commented 2 months ago

The BASIC DEF statement should allow a user to define functions that can be called later. It requires the symbol used for the function to begin with FN, such as DEF FND(N)=INT(RND(1)*N)+1.

image

When BASLOAD loads this program, it replaces the FN* name with a generated symbol, so that BASIC can no longer interpret it as a function:

image

This breaks the DEF statement such that it cannot be used with BASLOAD. If BASLOAD leaves any FN* symbols alone when translating, I believe it would fix this bug.

jestin commented 2 months ago

I found out that the parser works if you separate the FN from the function name. So while DEF FND(N)=INT(RND(1)*N)+1 does not work, DEF FN D(N)=INT(RND(1)*N)+1 does, provided you also call the function with the space: PRINT FN D(6).

I don't know if this workaround is enough to consider closing this bug.

stefan-b-jakobsson commented 2 months ago

DEF and FN are two BASIC commands. Requiring them to be separated from other code isn't really a workaround, even though the built-in interpreter accepts that you skip all spaces.

BASLOAD requires identifiers to be separated by a char that can't be part of an identifier. This is to make parsing source code with long symbol names user-friendly.

Not requiring spaces and long symbol names would be sort of possible, but a symbol could then not contain a BASIC command. I'm pretty sure users would be caught by this over and over, and that it would be a bad idea.

Some symbol name examples:

mooinglemur commented 2 months ago

I think this issue probably stems from a misunderstanding of what is happening here. DEF and FN are two separate keywords in CBM basic. The FN is not part of the identifier but a keyword all on its own. The syntax is DEF FN <identifier>(<param>).... CBM basic does not require spaces in between keywords and variable names, nor even two consecutive keywords, but BASLOAD does, for reasons including the potential unintended consequences Stefan alluded to, of supporting long identifiers and if it were to support keywords not surrounded by spaces.

tl;dr, not a bug, IMO. The "workaround" is indeed the intended syntax.

jestin commented 2 months ago

That sounds correct. I learned this convention from the old manuals, which specify that FN is part of the function name, and not a keyword onto itself (meaning it can't be separated with a space):

image

Clearly, that's not really the case. I've learned these manuals often oversimplify some of their explanations, and I think this might be the case here. Probably best to close this bug, and update BASIC docs to make it clear that FN isn't part of the symbol.