Permalink
Browse files

support new syntax for case expressions

before guards look like as follows:
     (case some-var
       ({:my-pattern y}
         (erlang:is_atom y)
       {:woot}))

This has become

      (case some-var
       ({:my-pattern y}
         (when (erlang:is_atom y))
       {:woot}))
  • Loading branch information...
1 parent 85743e0 commit a154a399912157e70da26b3b4ef98be291513bd7 @ericbmerritt ericbmerritt committed Dec 28, 2011
Showing with 29 additions and 3 deletions.
  1. +26 −0 src/jxa_try.erl
  2. +3 −3 test/jxat_case.erl
View
@@ -81,6 +81,32 @@ comp_clause(Path0, Ctx0, [Type, Pattern, Body]) ->
cerl:ann_c_var([compiler_generated], '_')],
jxa_clause:mk_guards(Line, PatternGuards),
CerlBody)};
+comp_clause(Path0, Ctx0, [Type, Pattern, ['when', Guards], Body]) ->
+ {_, {Line, _}} = jxa_annot:get(jxa_path:path(Path0),
+ jxa_ctx:annots(Ctx0)),
+ Ctx1 = jxa_ctx:push_scope(Ctx0),
+ {{Ctx2, TypeGuards}, CerlType} =
+ jxa_clause:comp_pattern(jxa_path:add(Path0), {Ctx1, []}, Type),
+ {{Ctx3, PatternGuards}, CerlPattern} =
+ jxa_clause:comp_pattern(jxa_path:add(jxa_path:incr(Path0)),
+ {Ctx2, TypeGuards},
+ Pattern),
+ {Ctx4, CerlGuard} =
+ jxa_expression:comp(
+ jxa_path:add(jxa_path:incr(
+ jxa_path:add(jxa_path:incr(Path0)))),
+ Ctx3, Guards),
+
+ {Ctx5, CerlBody} =
+ jxa_expression:comp(jxa_path:add(jxa_path:incr(2, Path0)),
+ Ctx4, Body),
+ {jxa_ctx:pop_scope(Ctx5),
+ cerl:ann_c_clause([Line, compiler_generated],
+ [CerlType, CerlPattern,
+ cerl:ann_c_var([compiler_generated], '_')],
+ jxa_clause:mk_guards(Line, [CerlGuard, PatternGuards]),
+ CerlBody)};
+
comp_clause(Path0, Ctx0, _) ->
{_, Idx} = jxa_annot:get(jxa_path:path(Path0),
jxa_ctx:annots(Ctx0)),
View
@@ -12,16 +12,16 @@ given([a,module,that,has,a,'case',statement], _State, _) ->
({:foo :bar}
(phash2 :bar))
((= four (= three :z))
- (erlang/is_atom three)
+ (when (erlang/is_atom three))
(phash2 four))
(_z
:ok)))
(defn internal-test2 (arg1 arg2 arg3)
(case [arg1 arg2 arg3]
([one two three]
- (and (and (erlang/is_atom one)
+ (when (and (and (erlang/is_atom one)
(erlang/is_integer two))
- (erlang/is_list three))
+ (erlang/is_list three)))
(phash2 one))
([{a a 33} a 4]
(phash2 a))

0 comments on commit a154a39

Please sign in to comment.