Quote:
Originally Posted by chaley
I am implementing the for/range loop.
Syntax:
Code:
for_expr ::= for_list | for_range
for_list ::= 'for' identifier 'in' list_expr
[ 'separator' separator_expr ] ':' expression_list 'rof'
for_range ::= 'for' variable 'range' [ start_expr 'to' ] stop_expr
[ 'limit' limit_expr ] ':' expression_list 'rof'
The loop is zero-based -- from start_expr up to but not including stop_expr. Example: "range 0 to 10" counts from zero to nine. This makes loops that reference lists and strings more natural, as in this example that uppercases every other letter in the title.
Code:
program:
res = '';
for i range 0 to strlen($title):
c = substr($title, i, i+1);
res = strcat(res, if mod(i, 2) == 0 then uppercase(c) else c fi)
rof
If you omit the start_expr then it is set to zero.
The limit specified the maximum number of loop iterations allowed. It is set to 1000 if omitted.
|
That looks good.
Except that I really don't like the handling of the range. Firstly, I will never remember the upper limit rules. I will automatically write "strlen($title) - 1" and wonder why it doesn't work properly. I would always expect that the range to handle both the start number and the last number.