Page 1 of 2 12>
Topic Options
#47150 - 2003-10-22 08:12 AM Using exit from a within gosub and do...until?
sneal Offline
Fresh Scripter

Registered: 2003-10-22
Posts: 6
Loc: Tokyo, Japan
Hi. I've been using KiXtart off and on for a few years for fairly simple scripting, but I finally ran into a problem I can't figure out, so it's time to ask the experts. When I use the exit command from a subroutine called from within a do...until loop, I get an error. Maybe I'm missing something really obvious. Please help!

I've created a test script which yields the same error as my real script.

code:
? "script begins"
do
? "enters do...until loop"
gosub "subscript"
? "returns to do...until loop"
until 1 = 1
? "script finishes"
exit 0

:subscript
? "in subscript"
if 1
? "exiting now..."
exit 1
endif
? "subscript continues"
return

I'm using 3.63, and I get the error,
Script error : expected expression !.
? "returns to do...until loop"


4.20 gives a different error:
ERROR : Error in expression.!
Script: T:\KiX\exittest.KIX
Line : 13


Replacing "exit" with "quit" works, but I'm calling this script from a main script, and need to continue with the main script after exiting.

Any ideas? Thanks in advance!

Top
#47151 - 2003-10-22 08:36 AM Re: Using exit from a within gosub and do...until?
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
hmm,

you might want to use a return instead of exit 1 in your subscript to make it work.

hth
_________________________



Top
#47152 - 2003-10-22 08:45 AM Re: Using exit from a within gosub and do...until?
Co Offline
MM club member
***

Registered: 2000-11-20
Posts: 1342
Loc: NL
What do you mean with if 1 [Confused]
I think this must be: if @error = 1

[ 22. October 2003, 08:53: Message edited by: Co ]
_________________________
Co


Top
#47153 - 2003-10-22 08:46 AM Re: Using exit from a within gosub and do...until?
sneal Offline
Fresh Scripter

Registered: 2003-10-22
Posts: 6
Loc: Tokyo, Japan
Jochen, thanks for the quick reply.

I want to abort the current script (and return to the main script). "Return" returns me to the gosub call location, and continues execution of the current script.

Top
#47154 - 2003-10-22 09:00 AM Re: Using exit from a within gosub and do...until?
sneal Offline
Fresh Scripter

Registered: 2003-10-22
Posts: 6
Loc: Tokyo, Japan
Co, the if 1 statement is always true. It's replicating the format of my original script, but you're right... it's not necessary for execution. Here's a more compact version which gives the same error:

code:
do
gosub "subscript"
until 1 = 1
exit 0
:subscript
exit 1
return


Top
#47155 - 2003-10-22 10:05 AM Re: Using exit from a within gosub and do...until?
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Sorry,

I just don't get that straight ... is there a higher level script form which this whole thing is called ?
Please explain [Confused]

[ 22. October 2003, 10:07: Message edited by: Jochen ]
_________________________



Top
#47156 - 2003-10-22 10:15 AM Re: Using exit from a within gosub and do...until?
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Well, that surprised me [Eek!]

It should work, and is valid use of it in your situation.

More weirdness with 4.20:


BREAK ON

while "true"
"Stage 1 *" ?
gosub "subscript"
"Stage 2 .." ?
loop
exit 0

:subscript
"Stage 3 :::" ?
exit 1
"Stage 4 !!!!" ?



This loops, printing "Stage 1" and "Stage 3".

It looks like the stack is not getting cleaned up properly after an exit. Anyone got the latest release candidate to hand that they can check with?

Top
#47157 - 2003-10-22 10:38 AM Re: Using exit from a within gosub and do...until?
sneal Offline
Fresh Scripter

Registered: 2003-10-22
Posts: 6
Loc: Tokyo, Japan
Jochen, you said,
quote:
... is there a higher level script form which this whole thing is called ?
Yes! Exactly. I'm calling this from a higher level script, and I need to return to that script, but abort the current script. So I need the EXIT command, not quit or return. I didn't include the higher level script, since it isn't necessary to demonstrate the problem (although it explains why I need EXIT).

Sorry... I tend to leave out important details! It also shows up in my scripts -- not commented very well! [Embarrassed]

Top
#47158 - 2003-10-23 12:52 AM Re: Using exit from a within gosub and do...until?
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
With 4.22rc1:

Using "Exit 1" in the subroutine prints "Stage 1" and "Stage 3".

Changing "Exit 1" to "Return" print "Stage 1", "Stage 3", and "Stage 2".

It seems that that could be a useful feature in some circumstances. The Exit in the subroutine cause the "Current loop to exit and start at the top of the loop where "Return" lets the loop continue from the Gosub.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#47159 - 2003-10-22 01:51 PM Re: Using exit from a within gosub and do...until?
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
A useful feature, yes. Also a bug though [Frown]

The "Exit" should exit the script cleanly in both the examples.

As the "feature" still occurs in the next version I'll post a link in Beta.

Top
#47160 - 2003-10-22 03:52 PM Re: Using exit from a within gosub and do...until?
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
hmmm, exit should exit the script cleanly ? dont forget about scope though ... hes calling exit from within a subroutine, and i think the rules state that exit exits the subroutine, not the script, just like doing an exit from a UDF. I think the real solutiion to this, and better programming practice, is that one should never want to really exit from within a UDF or sub-routine anyways ... should always exit from the mainline ...
Top
#47161 - 2003-10-22 04:01 PM Re: Using exit from a within gosub and do...until?
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Yeah, thinking of it now...

We all use 'EXIT x' to exit functions while setting error codes at the same time that can then be evaluated via @ERROR/@SERROR.

Ergo, not a bug, just incorrect information in the Kixtart Manual.
_________________________
There are two types of vessels, submarines and targets.

Top
#47162 - 2003-10-22 04:15 PM Re: Using exit from a within gosub and do...until?
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
I still think there is something broke here, because the bahavior of the exit in the sub-routine changes depending on how its called, if one calls the sub while nested in an IF statement, the EXIT exit's the script ...

code:
if "true"
gosub "myfunc"
endif

But if one calls the sub while nested in a WHILE loop, the sub returns instead of exit's ...

code:
while "true"
gosub "myfunc"
loop

hmmm

Top
#47163 - 2003-10-22 04:20 PM Re: Using exit from a within gosub and do...until?
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
quote:
dont forget about scope though...hes calling exit from within a subroutine, and i think the rules state that exit exits the subroutine, not the script
No, I hadn't forgotten about scope. The scope of the subroutine is the scope of the file and/or UDF that it is contained in - it very definately is not the loop.

A subroutine is just script in the same scope which happens to have a label. That's why you can drop into it accidentally if you forget to exit the script.

Personally I agree that converting the subroutine to a UDF and checking @ERROR on return is a much better way of handling the exit, however exiting the file in a subroutine is a valid process.

The fact that it doesn't work is a bug.

Consider this:
code:
; Sample 1
GoSub Sub

; Sample 2
For $i = 1 To 10 "In Loop" ? Exit 0 Next

; Sample 3
For $i = 1 To 10 "In Loop" ? Gosub Sub Next

Exit 0
:Sub
"Subroutine Called" ?
Exit 0

Why do tests 1 and 2 work as expected (script exits) but test 3 continues to execute the loop?

Top
#47164 - 2003-10-22 04:32 PM Re: Using exit from a within gosub and do...until?
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Well, by that logic then, DIMing a $var inside a sub would DIM it globally ?
Top
#47165 - 2003-10-22 04:42 PM Re: Using exit from a within gosub and do...until?
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
The subroutine is definitely one level down within a script as is code inside an IF-ENDIF construct. Now, the manual states
quote:
Exits the current KiXtart script, or, if used at the topmost level, exits KiXtart
which might actually mean that "KiXtart script" might be a down-level code section instead of a script being a complete text file.
_________________________
There are two types of vessels, submarines and targets.

Top
#47166 - 2003-10-22 04:54 PM Re: Using exit from a within gosub and do...until?
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
I think Jens is right ... and I think something is broke, or at least has been changed ... but its more than just an EXIT issue, its a larger scoping issue and maybe some out-dated documentation here ... the behavior of the sub (specifically the EXIT in subs) does change depending on how its called (maybe better said: changes depending on the scope of where its called from).

We can run and disect scripts until we're blue in face, but only Ruud can answer to his true intentions ... whether subs are more like UDF's now, whether subs were supposed to have global scope or not (like Richard says, treated like just some more mainline script) ...

[ 22. October 2003, 17:01: Message edited by: Shawn ]

Top
#47167 - 2003-10-22 05:02 PM Re: Using exit from a within gosub and do...until?
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Interesting. And indeed the manual states (p22) that variables dimmed in a subroutine have a local scope. That comes as a bit of a surprise.

quote:
The subroutine is definitely one level down within a script as is code inside an IF-ENDIF construct
That would mean that you couldn't use "Exit" from within an If/EndIf, as the exit would only exit the conditional structure.

Regardless, it is broken. It should either Exit the script as advertised, or return to the next line after the GoSub. Currently it does neither.

[ 22. October 2003, 17:03: Message edited by: Richard H. ]

Top
#47168 - 2003-10-23 12:02 AM Re: Using exit from a within gosub and do...until?
kholm Offline
Korg Regular
*****

Registered: 2000-06-19
Posts: 714
Loc: Randers, Denmark
It seems to me that, this is quite as expected.

I have never thougt of using the Exit command from a sub !!

It seems, that the looping structures in KiX are also "self contained", ie. keeping their own scope of @Error.
So you have to check the @Error macro within the loop, to check the result of Exit in sub's.

But Exit x in subroutines, causes the loop to start from beginning of the loop, skipping the remainder of the loop.

The following script will break correctly, and return to the calling script:

code:
For $i = 1 to 10
If @Error ; Sub returns an error - return to calling script
Return
EndIf
"Before call to sub" ?
GoSub sub
"After call to sub" ?
Next

Return

:sub
"Sub Called" ?
If $i = 3
Exit 1
EndIf
Return

The above is the working solution for Sub's, but i would prefer to use Function's instead Sub's if i want to inspect
the result of the call.

-Erik

Top
#47169 - 2003-10-23 03:37 AM Re: Using exit from a within gosub and do...until?
sneal Offline
Fresh Scripter

Registered: 2003-10-22
Posts: 6
Loc: Tokyo, Japan
Richard said,
quote:
As the "feature" still occurs in the next version I'll post a link in Beta.
Thanks! [Big Grin] Glad to know someone else thinks this is strange.

As I'm still using 3.63, UDFs are not an option. I've been putting off upgrading so I don't have to deal with corporate procedures, but it looks like I might have to bite the bullet on this one.

Cheers,
Sam

Top
Page 1 of 2 12>


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 1046 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.139 seconds in which 0.056 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org