Source

orakuda / pa / pa_hash.ml

(* 
   ocamlc -annot -I +camlp4 dynlink.cma camlp4lib.cma -pp camlp4of.opt pa_hash.ml  
   ocaml dynlink.cma camlp4o.cma pa_hash.cmo
*)
module Id = struct
  let name = "pa_hash"
  let version = "1.0"
end

open Camlp4

module Make (Syntax : Sig.Camlp4Syntax) = struct
  (* open Sig *)
  include Syntax

  (* We need Orakuda.Std.Hashtbl to avoid name collisions when Hashtbl module is redefined *)
  let bigarray_hashtbl_set _loc var newval =
    match var with
    | <:expr< Bigarray.Array1.get $arr$ $c1$ >> ->
        Some <:expr< Bigarray.Array1.set $arr$ $c1$ $newval$ >>
    | <:expr< Bigarray.Array2.get $arr$ $c1$ $c2$ >> ->
        Some <:expr< Bigarray.Array2.set $arr$ $c1$ $c2$ $newval$ >>
    | <:expr< Bigarray.Array3.get $arr$ $c1$ $c2$ $c3$ >> ->
        Some <:expr< Bigarray.Array3.set $arr$ $c1$ $c2$ $c3$ $newval$ >>
    | <:expr< Bigarray.Genarray.get $arr$ [| $coords$ |] >> ->
        Some <:expr< Bigarray.Genarray.set $arr$ [| $coords$ |] $newval$ >>
    | <:expr< Orakuda.Std.Hashtbl.find $e1$ $e2$ >> ->
        Some <:expr< Orakuda.Std.Hashtbl.replace $e1$ $e2$ $newval$ >>
    | <:expr< Orakuda.Std.Hashtbl.find_all $e1$ $e2$ >> ->
        Some <:expr< Orakuda.Std.Hashtbl.add $e1$ $e2$ $newval$ >>
    | _ -> None
  ;;

  DELETE_RULE Gram expr: SELF; "<-"; expr LEVEL "top" END;

  EXTEND Gram  
    GLOBAL: expr
  ;

  expr: LEVEL "simple"
    [ 
      [ e1 = SELF; "$"; "{"; e2 = SELF; "}" 
	-> <:expr<Orakuda.Std.Hashtbl.find $e1$ $e2$>> 
      | e1 = SELF; "$+"; "{"; e2 = SELF; "}" 
	-> <:expr<Orakuda.Std.Hashtbl.find_all $e1$ $e2$>> 
      | e1 = SELF; "$?"; "{"; e2 = SELF; "}"
	-> <:expr<Orakuda.Std.Hashtbl.mem $e1$ $e2$>> 
      ]
    ];
  expr: LEVEL ":="
    [
      [ e1 = SELF; "<-"; e2 = expr (* LEVEL "top" *) ->
        begin match bigarray_hashtbl_set _loc e1 e2 with
        | Some e -> e
        | None -> <:expr< $e1$ <- $e2$ >> 
	end
      ]
    ];
  END  
end

let module M = Register.OCamlSyntaxExtension(Id)(Make) in ()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.