From ec87737db57942464f025a621cbefb81612398b7 Mon Sep 17 00:00:00 2001 From: sneeker Date: Wed, 9 May 2018 10:21:00 +0000 Subject: [PATCH] Implement continuation expansion with the OCaml AST mapper --- continuum_ppx.ml | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 continuum_ppx.ml diff --git a/continuum_ppx.ml b/continuum_ppx.ml new file mode 100644 index 0000000..e6eb663 --- /dev/null +++ b/continuum_ppx.ml @@ -0,0 +1,21 @@ +open Asttypes +open Parsetree + +let expression mapper node = + let node = Ast_mapper.default_mapper.expr mapper node in + match node.pexp_desc with + | Pexp_extension ({ txt = ("continue" | "cps"); loc }, payload) -> + begin match payload with + | PStr [{ pstr_desc = Pstr_eval + ({ pexp_desc = Pexp_let (Nonrecursive, [binding], body); _ }, _); _ }] -> + let callback = Ast_helper.Exp.fun_ ~loc:binding.pvb_loc + ~attrs:binding.pvb_attributes Nolabel None binding.pvb_pat body in + Ast_helper.Exp.apply ~loc:node.pexp_loc ~attrs:node.pexp_attributes + binding.pvb_expr [Nolabel, callback] + | _ -> Location.raise_errorf ~loc + "continuum_ppx: expected let%%continue pattern = expression in body (one nonrecursive binding)" + end + | _ -> node + +let () = Ast_mapper.register "continuum_ppx" + (fun _ -> { Ast_mapper.default_mapper with expr = expression })