diff options
author | Alain Frisch <alain@frisch.fr> | 2013-08-30 14:55:37 +0000 |
---|---|---|
committer | Alain Frisch <alain@frisch.fr> | 2013-08-30 14:55:37 +0000 |
commit | 462d83344f756e73ae1d6229e68cd40ece2b5163 (patch) | |
tree | a1f190153bb89e175d4e6f2df27b8fde1c09daeb | |
parent | bc05874f5d66777191d35960516387045a1d6877 (diff) |
Porting pa_matches.
git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@14050 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
-rw-r--r-- | experimental/frisch/Makefile | 7 | ||||
-rw-r--r-- | experimental/frisch/ppx_matches.ml | 27 | ||||
-rw-r--r-- | experimental/frisch/test_matches.ml | 3 |
3 files changed, 37 insertions, 0 deletions
diff --git a/experimental/frisch/Makefile b/experimental/frisch/Makefile index 113467bc4..ae17e0951 100644 --- a/experimental/frisch/Makefile +++ b/experimental/frisch/Makefile @@ -79,3 +79,10 @@ copy_typedef: nomli: $(OCAMLC) -linkall -o nomli.exe -w +A-4-9 $(COMMON) $(BYTECMP) ../../tools/untypeast.cmo ../../tools/tast_iter.cmo nomli.ml ./nomli.exe test_nomli.ml + +## A port of pa_matches + +.PHONY: matches +matches: + $(OCAMLC) -linkall -o ppx_matches.exe $(COMMON) ppx_matches.ml + $(OCAMLC) -c -dsource -ppx ./ppx_matches.exe test_matches.ml diff --git a/experimental/frisch/ppx_matches.ml b/experimental/frisch/ppx_matches.ml new file mode 100644 index 000000000..7c26e67b6 --- /dev/null +++ b/experimental/frisch/ppx_matches.ml @@ -0,0 +1,27 @@ +(* + Example : List.filter [%matches ? 'a' .. 'z' ] text + Output : List.filter (function 'a' .. 'z' -> true | _ -> false) text +*) + +open Asttypes +open Parsetree +open Ast_helper + +let mapper = + object(this) + inherit Ast_mapper.mapper as super + + method! expr e = + match e.pexp_desc with + | Pexp_extension({txt="matches";_}, PPat (p, guard)) -> + let p = this # pat p in + let guard = Ast_mapper.map_opt (this # expr) guard in + Exp.function_ ~loc:e.pexp_loc + [ + Exp.case p ?guard (Convenience.constr "true" []); + Exp.case (Pat.any ()) (Convenience.constr "false" []); + ] + | _ -> super#expr e + end + +let () = Ast_mapper.main mapper diff --git a/experimental/frisch/test_matches.ml b/experimental/frisch/test_matches.ml new file mode 100644 index 000000000..a46a38baf --- /dev/null +++ b/experimental/frisch/test_matches.ml @@ -0,0 +1,3 @@ +let l = List.filter [%matches ? 'a'..'z'] ['a';'A';'X';'x'] + +let f = [%matches ? Some i when i >= 0] |