Permalink
Browse files

added the for expression, another way of writting a list comprehensio…

…n but supports multiple statements in the body
  • Loading branch information...
1 parent 4e62580 commit e17483b31dae4476b916d58b565223a2f6e62a71 @marianoguerra committed May 27, 2010
Showing with 37 additions and 1 deletion.
  1. +26 −0 examples/for.ifn
  2. +11 −1 src/fn_parser.yrl
View
@@ -0,0 +1,26 @@
+
+@public
+run = fn ()
+ R0 = for X in lists.seq(1, 10)
+ A = X + 1
+ A
+
+ R1 = for X in lists.seq(1, 10) if X % 2 == 0
+ A = X + 1
+ A
+
+ R2 = for X in lists.seq(1, 5)
+ for Y in lists.seq(6, 10)
+ (X, Y)
+
+ R2A = [(X, Y) for X in lists.seq(1, 5) for Y in lists.seq(6, 10)]
+
+ R3 = for (X, Y) in lists.zip(lists.seq(1, 3), lists.seq(4, 6))
+ (Y, X)
+
+ io.format("R0: ~p~n", [R0])
+ io.format("R1: ~p~n", [R1])
+ io.format("R2: ~p~n", [R2])
+ io.format("flatten R2: ~p~n", [lists.flatten(R2)])
+ io.format("R2A: ~p~n", [R2A])
+ io.format("R3: ~p~n", [R3])
View
@@ -20,7 +20,8 @@ Nonterminals
rec rec_def rec_set rec_new attr_sets attr_set
binary binary_items binary_item bin_type_def bin_type
prefix_op attribute
- obj_def obj_attrs obj_attrs_tail.
+ obj_def obj_attrs obj_attrs_tail
+ for_expr.
Terminals
fn match open close open_block close_block
@@ -142,6 +143,7 @@ unary_expr -> prefix_op literal : {op, line('$2'), op(unwrap('$1'))
unary_expr -> block_expr : '$1'.
block_expr -> if_expr : '$1'.
+block_expr -> for_expr : '$1'.
block_expr -> when_expr : '$1'.
block_expr -> arrow_expr : '$1'.
block_expr -> case_expr : '$1'.
@@ -174,6 +176,14 @@ if_expr -> if bool_expr fn_block else fn_block:
[{clause, line('$3'), [{atom, line('$3'), true}], [], '$3'},
{clause, line('$3'), [{atom, line('$3'), false}], [], '$5'}]}.
+% for expression
+
+for_expr -> for match_expr in bool_expr fn_block :
+ {lc, line('$1'), {block, line('$1'), '$5'}, [{generate, line('$3'), '$2', '$4'}]}.
+
+for_expr -> for match_expr in bool_expr if bool_expr fn_block :
+ {lc, line('$1'), {block, line('$1'), '$7'}, [{generate, line('$3'), '$2', '$4'}, '$6']}.
+
% case expression
case_expr -> switch bool_expr case_body : {'case', line('$1'), '$2', '$3'}.

0 comments on commit e17483b

Please sign in to comment.