let rec patt_to_exp (_loc: Ast.Loc.t) (a_pattern: Ast.patt): Ast.expr =
match a_pattern with
<:patt< $int:s$ >> -> <:expr< $int:s$ >> (* integer constant *)
| <:patt< $chr:c$ >> -> <:expr< $chr:c$ >> (* character constant *)
| <:patt< $str:s$ >> -> <:expr< $str:s$ >> (* string constant *)
| <:patt< $lid:b$ >> -> <:expr< $lid:b$ >> (* local variable *)
| <:patt< $uid:b$ >> -> <:expr< $uid:b$ >> (* variable of other module *)
| <:patt< $e1$ $e2$ >> -> (* function application *)
let p1 = patt_to_exp _loc e1
and p2 = patt_to_exp _loc e2 in
<:expr< $p1$ $p2$ >>
| <:patt< ($tup:p$) >> -> (* tuple *)
let e = patt_to_exp _loc p in
<:expr< ($tup:e$) >>
| <:patt< $p1$, $p2$ >> ->
let e1 = patt_to_exp _loc p1
and e2 = patt_to_exp _loc p2 in
<:expr< $e1$, $e2$ >>
| <:patt< { $r$ } >> ->
<:expr< { $rec_binding:patt_to_recbinding _loc r$ } >>
| <:patt< ($e$ : $t$) >> -> (* type restriction *)
let p = patt_to_exp _loc e in
<:expr< ($p$ : $t$) >>
| _ ->
Loc.raise _loc
(Stream.Error "patt_to_exp: this pattern is not yet supported")
(** patt_to_recbinding _loc a_pattern
Convert |
and patt_to_recbinding (_loc: Ast.Loc.t) (a_pattern: Ast.patt): Ast.rec_binding =
match a_pattern with
<:patt< >> -> <:rec_binding< >>
| <:patt< $i$ = $p$ >> ->
let p = patt_to_exp _loc p in
<:rec_binding< $i$ = $p$ >>
| <:patt< $p1$ ; $p2$ >> ->
let b1 = patt_to_recbinding _loc p1
and b2 = patt_to_recbinding _loc p2 in
<:rec_binding< $b1$; $b2$ >>
| <:patt< $anti:_$ >> ->
Loc.raise _loc
(Stream.Error "patt_to_recbinding: antiquotation are not yet supported")
| _ ->
Loc.raise _loc
(Stream.Error "patt_to_recbinding: never reached")