skx / gobasic

A BASIC interpreter written in golang.
https://blog.steve.fi/tags/basic/
GNU General Public License v2.0
331 stars 27 forks source link

Error in DEF FN: Expected FN after DEF #94

Open udhos opened 5 years ago

udhos commented 5 years ago

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

skx commented 5 years ago

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

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 ...

udhos commented 5 years ago

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
skx commented 5 years ago

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"..