#47150 - 2003-10-22 08:12 AM
Using exit from a within gosub and do...until?
|
sneal
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
|
|
|
|
#47153 - 2003-10-22 08:46 AM
Re: Using exit from a within gosub and do...until?
|
sneal
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
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
|
|
|
|
#47156 - 2003-10-22 10:15 AM
Re: Using exit from a within gosub and do...until?
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Well, that surprised me
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
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!
|
|
Top
|
|
|
|
#47158 - 2003-10-23 12:52 AM
Re: Using exit from a within gosub and do...until?
|
Howard Bullock
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.
|
|
Top
|
|
|
|
#47159 - 2003-10-22 01:51 PM
Re: Using exit from a within gosub and do...until?
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
A useful feature, yes. Also a bug though
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
|
|
|
|
#47162 - 2003-10-22 04:15 PM
Re: Using exit from a within gosub and do...until?
|
Shawn
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
   
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
|
|
|
|
#47167 - 2003-10-22 05:02 PM
Re: Using exit from a within gosub and do...until?
|
Richard H.
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
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
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! 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
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 1046 anonymous users online.
|
|
|