Skip to content
This repository has been archived by the owner on Nov 7, 2020. It is now read-only.

Commit

Permalink
handle nary application correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
LdBeth committed Sep 24, 2020
1 parent 1683356 commit da68900
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions support/shell/mptop.ml
Original file line number Diff line number Diff line change
Expand Up @@ -174,20 +174,30 @@ and mk_tuple_expr loc = function
| _ ->
not_supported loc "tuple expression"

(*
* Correctly handle N-ary function application
* Learned from CamlP5 source
*)
and unapplist e =
let rec unrec acc = function
(<:expr< $op$ $arg$ >>) -> unrec (arg::acc) op
| op -> (op, acc)
in unrec [] e

and mk_expr top_expr =
let loc = loc_of_expr top_expr in
let expr =
match top_expr with
(<:expr< $e1$ . $e2$ >> as top_expr) ->
mk_proj_expr loc top_expr
| (<:expr< $e1$ - $e2$ >>) ->
ApplyExpr ((loc, ApplyExpr ((loc, VarExpr "-"), mk_expr e1)), mk_expr e2)
| (<:expr< - $e$ >>) ->
(match mk_expr e with
_ ,IntExpr s -> IntExpr (- s)
| expr -> ApplyExpr ((loc, VarExpr "~-"), expr))
| (<:expr< $e1$ $e2$ >>) ->
ApplyExpr (mk_expr e1, mk_expr e2)
| (<:expr< $e1$ $e2$ >>) as app_expr ->
(match unapplist app_expr with
(<:expr< $lid:"-"$ >>) , [e] ->
(match mk_expr e with
_ ,IntExpr s -> IntExpr (-s)
| expr -> ApplyExpr ((loc, VarExpr "~-"), expr))
| op , args -> snd (List.fold_left (fun e el -> loc, ApplyExpr (e, el))
(mk_expr op) (List.map mk_expr args)))
| (<:expr< $e1$ .( $e2$ ) >>) ->
not_supported loc "array subscript"
| (<:expr< [| $list:el$ |] >>) ->
Expand Down

0 comments on commit da68900

Please sign in to comment.