skx / gobasic

A BASIC interpreter written in golang.

Home Page:https://blog.steve.fi/tags/basic/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error in DEF FN: Expected FN after DEF

udhos opened this issue · comments

commented

gobasic is reporting error for this program. How can I find the root cause?

lab@ubu1:~$ gobasic basgo/examples/trek/superstartrek.bas
Error constructing interpreter:
        Error in DEF FN: Expected FN after DEF
lab@ubu1:~$

Full source code: https://github.com/udhos/basgo/blob/master/examples/trek/superstartrek.bas

"Error in DEF FN: Expected FN after DEF" means:

  • The program started processing a "DEF..." statement.
  • But the next token was not "FN".

gobasic expects something like:

DEF FN blah(arg) = arg * arg

Instead it received something without FN:

DEF ....

If you look at the sample code you posted you'll see:

  470 DEF FND(D)=SQR((K(I,1)-S1)^2+(K(I,2)-S2)^2)
  475 DEF FNR(R)=INT(RND(R)*7.98+1.01)

DEF FNR.. != DEF FN ...

commented

Got it, thanks!

Do you think that gobasic could support both modes? FN A() and FNA() ? See example below.

Code:

$ more fn.bas
10 DEF FNA(X)=X*X
20 DEF FN B(X)=X*X*X
30 PRINT "fna(2)=";FNA(2)
40 PRINT "fn b(2)=";FN B(2)

Output:

fna(2)= 4
fn b(2)= 8

That would be a harder change than I'd like; since the parse would need to know that "FNA" was not the identifier "FNA" but instead two tokens "FN + A"..