Author Topic: o2 compile problem #3  (Read 595 times)

0 Members and 1 Guest are viewing this topic.

Offline Ed Davis

  • Newbie
  • *
  • Posts: 11
  • User-Rate: +0/-0
o2 compile problem #3
« on: July 19, 2022, 06:23:53 PM »
Same program as last time.
I backed out the last change so it will compile again.
I added a simple sub:
Code: [Select]
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:

Code: [Select]
ERROR:  undefined type for (
WORD:   !
LINE:   10
FILE:   "main source"

And this is line 10:

Code: [Select]
#lookahead

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

I think O2 doesn't like me :(

Offline Nicola_Piano

  • Full Member
  • ***
  • Posts: 111
  • User-Rate: +0/-0
Re: o2 compile problem #3
« Reply #1 on: August 03, 2022, 03:45:18 PM »
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 ...

 :)

Code: [Select]
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

Offline Nicola_Piano

  • Full Member
  • ***
  • Posts: 111
  • User-Rate: +0/-0
Re: o2 compile problem #3
« Reply #2 on: August 03, 2022, 04:20:27 PM »
Indeed, CALL (or GOSUB) works if it calls a procedure without parameters.
If you put parameters it goes into error... :-X


Code: [Select]
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

Offline Pierre Bellisle

  • JK-Mitglieder
  • Full Member
  • *
  • Posts: 149
  • User-Rate: +12/-7
Re: o2 compile problem #3
« Reply #3 on: August 04, 2022, 05:45:47 AM »
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".

Code: [Select]
//---------------------------
#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
//---------------------------

Code: [Select]
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
//---------------------------



Offline Nicola_Piano

  • Full Member
  • ***
  • Posts: 111
  • User-Rate: +0/-0
Re: o2 compile problem #3
« Reply #4 on: August 04, 2022, 11:24:31 AM »
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)

Code: [Select]
'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

Code: [Select]
'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

Offline Charles Pegge

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 879
  • User-Rate: +33/-1
    • Charles Pegge
Re: o2 compile problem #3
« Reply #5 on: August 04, 2022, 02:29:02 PM »
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

Offline Nicola_Piano

  • Full Member
  • ***
  • Posts: 111
  • User-Rate: +0/-0
Re: o2 compile problem #3
« Reply #6 on: August 04, 2022, 09:26:27 PM »
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
« Last Edit: August 04, 2022, 09:32:18 PM by Nicola_Piano »

Offline Zlatko Vid

  • Sr. Member
  • ****
  • Posts: 298
  • User-Rate: +2/-2
Re: o2 compile problem #3
« Reply #7 on: August 05, 2022, 08:46:53 AM »
well i don't use call and i don't use gosub
just this:

mySub()  or  myFunc()

and i jump to labels with goto

Offline Charles Pegge

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 879
  • User-Rate: +33/-1
    • Charles Pegge
Re: o2 compile problem #3
« Reply #8 on: August 05, 2022, 10:33:41 AM »
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.

Offline Nicola_Piano

  • Full Member
  • ***
  • Posts: 111
  • User-Rate: +0/-0
Re: o2 compile problem #3
« Reply #9 on: August 18, 2022, 03:25:06 PM »
Hi Charles,
might it be a good idea to put this comment in the Help?

Cheers