diff --git a/_tags b/_tags index 2e45667..8a23c20 100644 --- a/_tags +++ b/_tags @@ -1,4 +1,4 @@ - or or or :include + or or or or
:include <**/*.ml>: debug, dtypes : camlp4of, use_camlp4 <**/*.{byte,native}>: use_unix, use_str, debug diff --git a/myocamlbuild.ml b/myocamlbuild.ml index 4806e8b..cc4da2a 100644 --- a/myocamlbuild.ml +++ b/myocamlbuild.ml @@ -1,12 +1,7 @@ open Ocamlbuild_plugin open Ocamlbuild_plugin.Options -let sub_dirs = ["global"; "parsing"; "sigali"; "dataflow"; "sequential"; - "analysis"; "translation"; "main"; "simulation"] - let df = function - | Before_options -> - include_dirs := sub_dirs @ !include_dirs | After_rules -> (* Tell ocamlbuild about the camlp4 library. *) ocaml_lib ~extern:true ~dir:"+camlp4" "camlp4"; diff --git a/preproc.ml b/preproc.ml new file mode 100644 index 0000000..6529af7 --- /dev/null +++ b/preproc.ml @@ -0,0 +1,56 @@ +(** {1 Micro pre-processor for Heptagon} + + This module uses camlp4 to replace some fixed strings by string literals at + compile-time. At the moment, we only replace DATE by the current date and + STDLIB by "../../lib". Each pseudo-variable can be overriden by the + environment variable of the same name. *) + +open Camlp4.PreCast +open Unix + +(** {2 Compile-time strings} *) + +(** [date] is a string denoting the current date. *) +let date = + let days = [| "sunday"; "monday"; "tuesday"; "wednesday"; "thursday"; "friday"; + "saturday" |] + and months = [| "january"; "february"; "march"; "april"; "may"; "june"; + "july"; "august"; "september"; "october"; "november"; + "december" |] in + + let tm = Unix.localtime (Unix.gettimeofday ()) in + + let (day, month) = + let prefix s = String.sub s 0 3 in + (prefix days.(tm.tm_wday), prefix months.(tm.tm_mon)) in + + Printf.sprintf "%s. %s. %d %d:%d:%d CET %d" + day month tm.tm_mday tm.tm_hour tm.tm_min tm.tm_sec (1900 + tm.tm_year) + + +(** [stdlib] is the location of the standard Heptagon library. *) +let stdlib = + let wd = Unix.getcwd () in + Filename.concat (Filename.dirname (Filename.dirname wd)) "lib" + +(** Association list defining bindings between constant and our "compile-time + constants". *) +let env = [("DATE", date); ("STDLIB", stdlib)] + +(** {2 Camlp4 hook} *) + +(** Our home-grown super-duper syntax filter. Looks for string constants present + in [subst] and replaces them according to the couple found in the + environment defined above. *) +let filter = + object + inherit Ast.map as super + method expr e = match e with + | <:expr< $str:s$ >> when List.mem_assoc s env -> + let repl = try Sys.getenv s with Not_found -> List.assoc s env in + <:expr@here< $str:repl$ >> + | x -> x + end;; + +(** Tell Camlp4 about it. *) +AstFilters.register_str_item_filter filter#str_item