1. camlspotter
  2. opycaml

Commits

camlspotter  committed dc9fd39

pa_opycaml

  • Participants
  • Parent commits baddd7e
  • Branches default

Comments (0)

Files changed (5)

File OMakefile

View file
 OCamlProgram(test, test)
 OCamlProgram(test_bug1, test_bug1)
 
-.SUBDIRS: tests
+.SUBDIRS: tests pa
 
-

File js_of_ocaml/README.txt

View file
 This is a test of using js_of_ocaml syntax sugar in OPyCaml. Seems nice!!!
+
+Additional requirements: js_of_ocaml

File pa/OMakefile

View file
+.PHONY: all install clean
+
+USE_OCAMLFIND = true
+
+OCAMLPACKS[] =
+	camlp4
+
+OCAMLINCLUDES +=
+
+NATIVE_ENABLED = $(OCAMLOPT_EXISTS)
+BYTE_ENABLED = true
+
+OCAMLCFLAGS   += -annot -w Ae
+
+OCAMLPACKAGEFLAGS= 
+
+FILES[] =
+	pa_opycaml
+
+LIB = pa_opycaml
+
+.DEFAULT: $(OCamlLibrary $(LIB), $(FILES))
+
+clean:
+	rm $(filter-proper-targets $(ls R, .))
+
+OCAMLDEPFLAGS=-pp camlp4of
+OCAMLPPFLAGS=-pp camlp4of
+
+# top_test: pa_bind_inline.cma
+# 	ocaml -I +camlp4 dynlink.cma camlp4o.cma pa_bind_inline.cma 
+# 
+# .PHONY: test
+# 
+# test: pa_bind_inline.cma
+# 	camlp4o -printer OCaml -I . pa_bind_inline.cma test_pa_bind_inline.ml  
+
+# It is local use
+
+# install: pa_bind_inline.cma pa_bind_inline.cmxa
+# 	ocamlfind remove bind_inline
+# 	ocamlfind install bind_inline META pa*.cmo pa*.cmx pa*.cmi pa*.cma pa*.cmxa

File pa/pa_opycaml.ml

View file
+module Id = struct
+  let name = "pa_opycaml"
+  let version = "1.0"
+end
+
+open Camlp4
+open PreCast
+
+(* See tutorial at 
+   http://ambassadortothecomputers.blogspot.com/2010/03/reading-camlp4-part-5-filters.html 
+   http://brion.inria.fr/gallium/index.php/Camlp4MapGenerator
+*)
+
+module Make (AstFilters : Sig.AstFilters) = struct
+  open AstFilters
+
+  let _ = 
+    let simplify = object
+      inherit Ast.map as super
+      method! str_item sitem =
+        let sitem = super#str_item sitem in
+        match sitem with
+        | Ast.StMod (_loc, modname, module_expr) ->
+            let rec iter_sitems f = function
+              | Ast.StSem (_, sitem1, sitem2) -> iter_sitems f sitem1; iter_sitems f sitem2
+              | i -> f i
+            in 
+            begin match module_expr with
+            | Ast.MeStr (x, sitem) ->
+                let meths = ref [] in
+                iter_sitems (function
+                  | Ast.StExt (_, name, <:ctyp< [ > $cls$ ] t -> $_ret$>>, _slist) -> 
+(*
+                  | <:str_item< external $name$ : [ > $cls$ ] t -> $_$ = $_$>> ->     DOES NOT WORK...
+*)
+                      begin match cls with
+                      | Ast.TyId (_, Ast.IdLid (_, tname)) ->
+                          if tname = "_" ^ modname then meths := name :: !meths
+                      | _ -> ()
+                      end
+                  | _ -> ()
+                ) sitem;
+                let create_meth name = 
+                  let lid = Ast.ExId (_loc, Ast.IdLid (_loc, name)) in
+                  <:class_str_item< method $name$ = $lid$ t >> 
+                in
+                let meths = 
+                  List.fold_left (fun st name -> Ast.CrSem(_loc, create_meth name, st)) (Ast.CrNil _loc) !meths
+                in
+                let sitem' = Ast.StSem (_loc, sitem, 
+                                        <:str_item< class o t = object
+                                            $meths$
+                                        end>>)
+                in
+                Ast.StMod (_loc, modname, Ast.MeStr (x, sitem'))
+            | _ -> sitem
+            end
+        | sitem -> sitem
+    end
+    in AstFilters.register_str_item_filter simplify#str_item
+
+end
+
+let module M = Register.AstFilter(Id)(Make) in ()

File pa/test.txt

View file
+camlp4of ./pa_opycaml.cmo ../autoapi.ml