• Welcome to Jose's Read Only Forum 2023.
 

o2 compile problem #3

Started by Ed Davis, July 19, 2022, 06:23:53 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Ed Davis

Same program as last time.
I backed out the last change so it will compile again.
I added a simple sub:
sub help()
  print("bye, quit - exits the interpreter               " & cr)
  print("clear     - clears variables                    " & cr)
  print("help      - simple help screen                  " & cr)
  print("list      - lists current program               " & cr)
  print("load      - loads a program into the interpreter" & cr)
  print("new       - discards the current program        " & cr)
  print("run       - runs the current program            " & cr)
  print("save      - saves the current program           " & cr)
end sub


And now when I compile, it complains:

ERROR:  undefined type for (
WORD:   !
LINE:   10
FILE:   "main source"


And this is line 10:

#lookahead


Note that I tried it with and without the "& cr".  Help! :)

I think O2 doesn't like me :(

Nicola_Piano

Hi,
I'm sure this isn't the best solution, but I tried using CALL HELP and it called the procedure even without using #lookahead ...

:)


use console


call Help


sub help()
  print("bye, quit - exits the interpreter               " & cr)
  print("clear     - clears variables                    " & cr)
  print("help      - simple help screen                  " & cr)
  print("list      - lists current program               " & cr)
  print("load      - loads a program into the interpreter" & cr)
  print("new       - discards the current program        " & cr)
  print("run       - runs the current program            " & cr)
  print("save      - saves the current program           " & cr)
end sub

wait

Nicola_Piano

Indeed, CALL (or GOSUB) works if it calls a procedure without parameters.
If you put parameters it goes into error... :-X



use console

call Help

call ciao

char tx="ciao2"
gosub greet("ciao3")

wait


sub help()
  print("bye, quit - exits the interpreter               " & cr)
  print("clear     - clears variables                    " & cr)
  print("help      - simple help screen                  " & cr)
  print("list      - lists current program               " & cr)
  print("load      - loads a program into the interpreter" & cr)
  print("new       - discards the current program        " & cr)
  print("run       - runs the current program            " & cr)
  print("save      - saves the current program           " & cr)
end sub

sub ciao
printl "ciao1"
print cr
end sub

sub greet '(char tx)
printl "cheers"
printl tx
printl
end sub

Pierre Bellisle

Nicola,
In your first post, it work without #lookahead because "call Help" is before "sub help()". The compiler do not have to look ahead to know what "help()" is made of.

In your second post, I think you misunderstood how GOSUB work.
It might be good to look at the help for those... "gosub", "#lookahead".
For "call" I didn't see it in the help, seems that it is optional to use it.
"call Help" is the same as "Help".


//---------------------------
#lookahead // Here it is needed because greet(), ciao(), and help() are called before any declaration

use console

string tx = "Text 1"

call Help '"call" is optional

call ciao

gosub greetSub 'invoke local subroutines >>>inside<<< a procedure

call greet(tx)

greet("text 2")

wait

return 0 'End of program

greetSub:
print "in greetSub via GOSUB" & cr
ret 'Return to the line after the gosub greetSub was invoked
//---------------------------
sub help()
  print("in sub help" & cr)
end sub
//---------------------------
sub ciao()
print("in sub ciao" & cr)
end sub
//---------------------------
sub greet(string tx)
  print("in sub greet -[" & tx & "]" & cr)
end sub
//---------------------------



use console
//---------------------------
sub help()
  print("in sub help" & cr)
end sub
//---------------------------
sub ciao()
print("in sub ciao" & cr)
end sub
//---------------------------
sub greet(string tx)
  print("in sub greet -[" & tx & "]" & cr)
end sub
//---------------------------
'#lookahead // Here it is not needed because greet(), ciao(), and help() are preeceeding and known by the compiler

string tx = "Text 1"

call Help '"call" is optional

call ciao

gosub greetSub 'invoke local subroutines >>>inside<<< a procedure

call greet(tx)

greet("text 2")

wait

return 0 'End of program

greetSub:
print "in greetSub via GOSUB" & cr
ret 'Return to the line after the gosub greetSub
//---------------------------




Nicola_Piano

Hello,
it is as you say that both CALL and GOSUB are calls to be made in the same subroutine,
but the fact remains that in this way the calls to subs (without parameters) are made without errors.
Also by putting other various subs in the program.

(for help, see the link at the bottom of my post)


'this runs
use console
call Help
call ciao1

char tx="tx ciao2"
call greet("tx ciao3")

wait

'--------------
' end program
'--------------

sub a1
print "do noting"
end sub

sub a2
print "do noting"
end sub
'-----------------------------
sub help()
  print("bye, quit - exits the interpreter               " & cr)
end sub
'-----------------------------
sub ciao1
printl "sub ciao1"
print cr
end sub
'--------------------------
sub greet
printl "sub greet"
printl tx
printl
end sub


'It does not work this way.
'You need to enter the #LOOKAHEAD command

use console

Help()
ciao1()

char tx="tx ciao2"
greet("tx ciao3")

wait

'--------------
' end program
'--------------

sub a1
print "do noting"
end sub
'------------------
sub a2
print "do noting"
end sub
'------------------
sub help()
  print("bye, quit - exits the interpreter               " & cr)
end sub
'-------------------
sub ciao1
printl "sub ciao1"
print cr
end sub
'--------------------
sub greet(char tx)
printl "sub greet"
printl tx
printl
end sub


Charles Pegge

gosubs and simple calls do not depend on having a declaration. They are resolved entirely by the linker, whereas procedures rely on matching the prototype signature, which supports overloading.

when compiling in 64bit however, you must avoid using call instead of gosub. This is due to stack alignment which must be rounded up to the nearest 16 bytes. This alignment happens automatically with gosub

Nicola_Piano

#6
Thanks Charles,
you were very clear.

Could we assume that the gosub call even outside a sub however simply refers to a label?
Otherwise it would also be fine for a sub call with parameters ... or not?

However, what could have been causing the problem Ed reported?
Cheers

Zlatko Vid

well i don't use call and i don't use gosub
just this:

mySub()  or  myFunc()

and i jump to labels with goto

Charles Pegge

It is possible to call gosubs in the global space, but only using global variables inside the subroutine.

A subroutine starts with a simple label and ends with an asm ret instruction. They are very fast but don't provide any encapsulation.


Ed's problems were mainly indexbase 0, beuse he was using array elements 0 as the first element.

Nicola_Piano

Hi Charles,
might it be a good idea to put this comment in the Help?

Cheers