lua-users home
lua-l archive

Re: [patch] continue statement

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


A little late, but ...
The semantics of continue is available in Lua. No patch required.
while something do
 repeat
 ...
 if expr then break end
 ...
 until true
end
Here 'break' is read as 'goto end of block' ('continue', if you
prefer :-)), and 'repeat ... until true' is read as 'block ... end'.
Some other control structures produced by 'misusing' repeat:
perl's while { ... next ... } continue { ... }
while something do
 repeat
 ...
 if expr then do break end end
 ...
 until true
 ... -- the continue block
 -- is always executed just before the
 -- conditional is to be evaluated again.
end
perl's redo and more:
while something do
 ... -- only runs if not redo
 local redo = false
 repeat
 if redo then
 ... -- runs only if redo
 redo = false
 end
 repeat
 ...
 -- next line is pronounced 'redo if expr;' :-)
 if expr then redo = true break end
 ...
 until true
 ... -- runs whether or not redo
 until not redo
 ... -- continue block doesn't run if redo
end
Whew! That's enough of that.
The 'goto end of block' can be used to reduce 'if nests' and 'else
thickets', the problem the patch was to solve. For example,
 if a then
 ...
 else
 ...
 if b then
 ...
 else
 ... -- code 1 if not a and not b independent of c or d
 if c then
 ...
 else
 ...
 if d then
 ...
 end
 end
 ... -- code 2 if not a and not b perhaps dependent on c or d
 end -- some elses in here would make this code even harder to grasp
 end
becomes
 repeat
 if a then
 ...
 else break end
 ...
 if b then
 ...
 else break
 end -- the traditional placement, easier to read perhaps.
 ... -- code 1 if not a and not b
 if c then
 ...
 else -- code 2 may prevent use of break here. perhaps it could
 -- be here and else could be replaced by 'else break end'
 ...
 if d then
 ...
 end
 end
 ... -- code 2 here if code under 'if c' or 'if d' does
 -- influence
 until true
where 'repeat ... until true' is read as 'block ... end fnord:'
and 'else break' is read as 'goto fnord'. :-)
--
jrs

AltStyle によって変換されたページ (->オリジナル) /