Re: Open Source
- Posted by CChris <christian.cuvier at a?ric?lture.gouv.fr> Oct 22, 2007
- 670 views
Andy Drummond wrote: > > Jason Gade wrote: > > > > I also like the idea of "continue <label>" or "next <label>" where label > > is an optional identifier at the beginning of a loop. But I haven't given it > > too > > much thought. > > > > I would like to suggest three additions to the syntax of Euphoria which would > get around 99% of the desire for a goto. Nothing new but as suggested above: > > next var -- Go to the iteration point of the for loop for this variable > exit var -- Ditto but exit the loop > redo var -- Ditto but re-run the loop without iterating the variable > > For nested loops the interpreter would have to release variables which were > out of scope, but that is simple enough. If you have three nested loops > using I, J & K, say, and in the middle had "next I", then that would be coded > as exit K; exit J; next I; > > }}} <eucode> > for i=1 to 10 do > for j=1 to 10 do > for k=1 to 10 do > if some condition met then > next i > -- effectively: exit k > -- exit j > -- next i > end if > end for > end for > end for > </eucode> {{{ > > These three would make the nested loop control much easier to code and much > easier to read. And the desire for goto would almost vanish. Most people who > ask for goto statements want them for exiting nested loops, it seems to me. > > Any suggestions? Anyone able to add these to the interpreter? > > Andy Drummond Your approach only works for for-loops, not for while-loops. However, I can add the loop variable as an extra possible argument to exit/next/retry which work in my modified interpreter - see my reply to the first post from Don Cole-. Only the front end needs be modified: * next and retry translate to ELSE adequate_target * Patch[E|X]List() handles delayed block exits, otherwise do business as usual. Hiding the loop var is done at end of for loop, unchanged. The other constructs I parse are: * exit 1 (exit loop above), 2 (two loops above), ... -1 (all loops), -2 (all loops but the topmost),... exit 0 is the same as exit. * exit some_string, where the loop has been labelled with some_string. * The way I add labels is as follows: for i=1 to n [by 2] [label "my string"] do if <some_condition> label "my block" then I have no qualms changing "retry" to "redo". "continue" seems a little ambiguous to me, which is why I prefer "next" over it, but it's not much of an issue. I also have an "exif" statement which exits (nested) if blocks, same syntax enhancements. CChris