Skip to content

Commit

Permalink
support new syntax for case expressions
Browse files Browse the repository at this point in the history
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
ericbmerritt committed Dec 28, 2011
1 parent 85743e0 commit a154a39
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
26 changes: 26 additions & 0 deletions src/jxa_try.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down
6 changes: 3 additions & 3 deletions test/jxat_case.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit a154a39

Please sign in to comment.