Re: Open Source
- Posted by Andy Drummond <andy at k?s?reltele.com> Oct 22, 2007
- 657 views
> > > > 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 I hadn't appreciated that you were producing a version of the interpreter, CChris, so if you do I should be very pleased to sample it in due time. I have had a number of loops - and yes, I almost always use for loops rather than while loops because the latter needs some setup before they commence - in which I need to exit the loop partway through. The if is easier but would gain from some kind of multiple level exit route - your exif would be useful. I have to say I am less impressed with the labelling system, though I can see the reason behind it; it just smacks of the old goto system which I am sure someone will want extended to become a full goto. Not a nice prospect. I understand the change is only a front-end change, but it is the front-end code which is hardest to sort out. Would Rob be interested in having this kind of code change being incrporated into the official Euphoria? If the phrase is meaningful now it is open-source and every Tom Dick & Harry can produce his own flavour! We've gone from (benign) dictatorship to design by committee! AndyD