Re: New keyword added: continue
- Posted by Jeremy Cowgar <jeremy at cowgar.c?m> May 12, 2008
- 773 views
Kat wrote: > > Why? > Here's how you use one keyword instead of six: > > }}} <eucode> > > --repeat until > :repeat > x -= 1 > if x > 12 then goto repeat end if > > > -- case > atom fred, a, d > fred = 45 > a = 1 > d = 44 > > goto x > puts(1,x&" is not covered") goto caseend > :1 puts(1,"it's 1!") goto caseend > :34 puts(1,"wow, 34!") > :"Sam" puts(1,"you got Sam!" goto caseend > :a+d puts(1,"Yep, fred is 45!") goto caseend > :caseend > > -- continue, retry, restart, and next > :restart > for loop = 1 to 12 do > :retry > -- code > if test = failed then goto retry end if > if thistest = failed then goto restart end if > if bored = true then goto next end if > if done then goto demoend end if > -- code > :next > end for > :demoend > > </eucode> {{{ The above code is not entirely correct or as simple as it seems. Let's start with the case statement. I think a case statement would a much better choice. Why? What if you want two case statements, either nested or in the same function in different sections that answer both to the integer 1 ?
goto x :1 puts "One!\n" :2 puts "Two!\n" :caseend goto y :1 puts "One!\n" -- invalid already used :2 puts "Two!\n" -- invalid already used :caseend -- invalid already used
The for loop with next, retry, exit, restart, etc... First to program might be a bit of a chore because on the keywords "end for" the stack is cleaned up, temp variables cleared, etc... A direct goto out of the for loop w/o hitting an end for would leave the internals dirty. It should be able to detect that it went out of the loop and then clean up, but it's not as easy as pushing adding a label and going to a label in the backend as you would think. #2 with the for loop, it suffers the same fate as the case statement. Sure, on the first for loop you can use :retry, :end, :next, :restart, but then on subsequent for loops in the same context (in top level or inside a function), you would have to get creative in your naming... :end2, :next2, :restart2, etc... Then have 1 for loop that is nested in another, and then later in the function you add a new for loop. Then, as time goes on, you add a for loop at the beginning of the function. Now, your first for loop is named :end3, :restart3, and your second is just :end, :restart, etc... and an inner is :restart3, etc... It begins to get pretty confusing. So, while I personally think a goto statement is a good thing, I do not think it is a replacement for exit, continue and a case statement. -- Jeremy Cowgar http://jeremy.cowgar.com