Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support try expression #254

Merged
merged 3 commits into from
Jul 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 24 additions & 10 deletions lib/from_erlang.ml
Original file line number Diff line number Diff line change
Expand Up @@ -312,13 +312,7 @@ and expr_of_erlang_expr' = function
raise Known_error.(FialyzerError (NotImplemented {issue_links=["https://github.com/dwango/fialyzer/issues/222"];
message="support block expr"}))
| ExprCase {line; expr; clauses} ->
let cs = clauses |> List.map ~f:(function
| F.ClsCase {line; pattern; guard_sequence; body; _} ->
if Option.is_some guard_sequence then Log.debug [%here] "line:%d %s" line "Guard (when clauses) are not supported";
((pattern_of_erlang_pattern pattern, Constant (line, Atom "true")), expr_of_erlang_expr' body)
| F.ClsCatch _ | F.ClsFun _ | F.ClsIf _ ->
failwith "cannot reach here"
) in
let cs = case_clauses_of_clauses clauses in
Case (line, expr_of_erlang_expr' expr, cs)
| ExprCatch {line; expr} ->
let e = expr_of_erlang_expr' expr in
Expand Down Expand Up @@ -380,9 +374,20 @@ and expr_of_erlang_expr' = function
raise Known_error.(FialyzerError (NotImplemented {issue_links=["https://github.com/dwango/fialyzer/issues/227"];
message="support record expr"}))
| ExprTuple {line; elements} -> Tuple (line, List.map ~f:expr_of_erlang_expr' elements)
| ExprTry _ ->
raise Known_error.(FialyzerError (NotImplemented {issue_links=["https://github.com/dwango/fialyzer/issues/223"];
message="support try expr"}))
| ExprTry {line; exprs; case_clauses; catch_clauses; after} ->
(* TODO: ignore catch clauses, see the issue https://github.com/dwango/fialyzer/issues/252 *)
let e =
if List.is_empty case_clauses then
expr_of_erlang_exprs exprs
else
let cs = case_clauses_of_clauses case_clauses in
Case (line, expr_of_erlang_exprs exprs, cs)
in
if List.is_empty after then
e
else
let e_after = expr_of_erlang_exprs after in
Let (line, "_", e, e_after)
| ExprVar {line; id} -> Ref (line, Var id)
| ExprLit {lit} -> expr_of_literal lit
| ExprCons {line; head; tail; _} -> ListCons (line, expr_of_erlang_expr' head, expr_of_erlang_expr' tail)
Expand Down Expand Up @@ -461,6 +466,15 @@ and function_of_clauses clauses =
let arity = List.nth_exn arities 0 in
let fresh_variables = (make_fresh_variables arity) in
{args=fresh_variables; body=make_case cs fresh_variables}
and case_clauses_of_clauses clauses =
let f = function
| F.ClsCase {line; pattern; guard_sequence; body; _} ->
if Option.is_some guard_sequence then Log.debug [%here] "line:%d %s" line "Guard (when clauses) are not supported";
((pattern_of_erlang_pattern pattern, Constant (line, Atom "true")), expr_of_erlang_expr' body)
| F.ClsCatch _ | F.ClsFun _ | F.ClsIf _ ->
failwith "cannot reach here"
in
List.map ~f clauses

let expr_of_erlang_expr e = e |> extract_toplevel |> expr_of_erlang_expr'

Expand Down
34 changes: 30 additions & 4 deletions test/blackbox-test/test-cases/try_catch_expr.erl
Original file line number Diff line number Diff line change
@@ -1,8 +1,34 @@
-module(try_catch_expr).

-export([main/0]).
-export([catch_expr/0, expr_try1/0, expr_try2/0, expr_try3/0]).

-spec main() -> any().
main() ->
-spec catch_expr() -> any().
catch_expr() ->
catch 1 + 2.



-spec expr_try1() -> number().
expr_try1() ->
try
1 + 2
catch
Err:Stack -> {hoge, Err, Stack}
end.

-spec expr_try2() -> number().
expr_try2() ->
try 1 + 2 of
X -> 2 * X
catch
Err:Stack -> {hoge, Err, Stack}
end.

-spec expr_try3() -> ok.
expr_try3() ->
try 1 + 2 of
X -> 2 * X
catch
Err:Stack -> {hoge, Err, Stack}
after
ok
end.