diff --git a/Makefile b/Makefile index d1794e0..40b0e5e 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,7 @@ DISTRIB=_build/aurochs-$(VERSION) all: targets targets: - @./build.sh aurochs_lib.cma aurochs_lib.cmxa aurochs_tool.native aurochs/test_aurochs.native cnog/check + @./build.sh aurochs_lib.cma aurochs_lib.cmxa aurochs_tool.native aurochs/test_aurochs.native c/test/test_nog clean: rm -rf _build/ diff --git a/_tags b/_tags index d5a1f4b..a038d62 100644 --- a/_tags +++ b/_tags @@ -1,5 +1,5 @@ #<*/*.c>: cstuff -<*/*.c>: cstuff, optimize +<**/*.c>: cstuff, optimize : use_str, use_unix <{aurochs/aurochs_tool,aurochs/test_aurochs,tests/test_{arith,inline}}.{byte,native}>: use_libaurochs <{util,peg,pack,syntax,nog,backends,front,aurochs}/*.cmx>: for-pack(Aurochs_pack) diff --git a/aurochs/aurochs_native.c b/aurochs/aurochs_native.c index fc5e741..6d9b8e4 100644 --- a/aurochs/aurochs_native.c +++ b/aurochs/aurochs_native.c @@ -11,16 +11,14 @@ #include #include -#include -#include -#include - #if 0 #define DEBUGF(x, y...) printf("DEBUG " __FILE__ ":" x "\n", ##y); #else #define DEBUGF(x,...) #endif +#include + typedef value tree; typedef value attribute; typedef struct { @@ -31,9 +29,7 @@ typedef construction_t *construction; #define BUILDER_TYPES_DEFINED 1 -#include -#include -#include +#include typedef struct { nog_program_t *p_nog; @@ -111,7 +107,7 @@ value caml_aurochs_program_of_binary(value binaryv) if(pack_init_from_string(&pk, binary, length)) { s = staloc_create(&alloc_stdlib); if(s) { - pg = cnog_unpack_program(&s->s_alloc, &pk); + pg = nog_unpack_program(&s->s_alloc, &pk); if(pg) { programv = program_alloc(pg, s); CAMLreturn(programv); @@ -414,7 +410,7 @@ value caml_aurochs_parse(value programv, value uv, value errorv) caml_failwith("Can't allocate context"); } - if(cnog_execute(cx, pg, &treev)) { + if(nog_execute(cx, pg, &treev)) { peg_delete_context(cx); staloc_dispose(s); CAMLreturn(some(treev)); @@ -422,7 +418,7 @@ value caml_aurochs_parse(value programv, value uv, value errorv) /* We've got a parse error, compute its position. */ int pos; - pos = cnog_error_position(cx, pg); + pos = nog_error_position(cx, pg); Store_field(errorv, 0, Val_int(pos)); peg_delete_context(cx); staloc_dispose(s); diff --git a/aurochs/aurochs_tool.ml b/aurochs/aurochs_tool.ml index b0342a5..bc56f00 100644 --- a/aurochs/aurochs_tool.ml +++ b/aurochs/aurochs_tool.ml @@ -37,7 +37,8 @@ module Spec = | "c" -> targets += `c | _ -> raise (Bad "Invalid target") end), - " target language"; + " Type of parser to generate. (nog: Aurochs parsing automaton as a binary file; c_table_nog: nog as a C table; + ml: nog as ML string; c: direct parsing using C code"; "-load-nog", String(aor load_nog), @@ -209,7 +210,14 @@ let _ = Arg.parse Spec.specs (fun x -> did_something := true; Process.process (Some x)) - (sf "Usage: %s [options] " progname); + (sf + "Usage:\n\ + \ 1. %s -parse file grammar.peg\n\ + \ 2. %s -target nog grammar.peg\n\ + \ 3. %s -load-nog grammar.nog -parse file\n\ + \n\ + Options:\n" + progname progname progname); (* Consistency checks *) if (!Opt.debug || !Opt.trace) && !Opt.interpreter != `mlnog then diff --git a/aurochs/bootstrap.ml b/aurochs/bootstrap.ml index 36765b4..370076f 100644 --- a/aurochs/bootstrap.ml +++ b/aurochs/bootstrap.ml @@ -1,11 +1,10 @@ (* Bootstrap *) -open Pffpsf;; -open Peg;; +open Pffpsf +open Peg -(*** load_grammar *) let load_grammar fn = - let u = Driver.read_file fn in + let u = Util.read_file fn in let m = String.length u in let peg = Grammar_original.peg in (*with_file_output "internal.peg" (fun oc -> Pretty.print_grammar oc peg);*) @@ -20,8 +19,6 @@ let load_grammar fn = raise (Error(sf "Error in grammar file %S at character %d" fn (i + 1))) with | Peg.Fail -> raise (Error(sf "Cannot parse grammar file %S" fn)) -;; -(* ***) let _ = let fn = "syntax/grammar.peg" in @@ -29,4 +26,3 @@ let _ = let peg = Canonify.canonify_grammar ~start:!Opt.start peg in let nog = Noggie.generate "grammar" ~root:"Root" ~start:"start" peg in Camelus.generate_implementation ~pack:false "syntax/grammar" "start" peg nog -;; diff --git a/aurochs/driver.ml b/aurochs/driver.ml index c900261..298fad2 100644 --- a/aurochs/driver.ml +++ b/aurochs/driver.ml @@ -1,42 +1,4 @@ (* Driver *) -open Peg;; +open Peg -open Pffpsf;; - -(*let process (start : peg) u = - try - let (q, i, t) = Peg.descent (fun x -> x) start u in - fp stdout "%b %d\n%a\n%!" q i (Peg.print_tree ~depth:0) t - with - | Peg.Fail -> - fp stdout "Does not parse.\n%!" -;; - -let line_driver start = - try - while true do - (*fp stdout "Query? %!";*) - let l = input_line stdin in - fp stdout "%s\n" l; - process start l - done - with - | End_of_file -> - () - (*fp stdout "\nBye.\n%!"*) -;;*) - -let read_file fn = - let ic = open_in fn in - let m = in_channel_length ic in - let u = String.create m in - really_input ic u 0 m; - close_in ic; - u -;; - -(*let file_driver start fn = - let u = read_file fn in - process start u -;;*) diff --git a/aurochs/process.ml b/aurochs/process.ml index 28c2667..4e32404 100644 --- a/aurochs/process.ml +++ b/aurochs/process.ml @@ -14,7 +14,7 @@ type 'a name = | Unresolved of string let bootstrap fn = - let u = Driver.read_file fn in + let u = Util.read_file fn in let m = String.length u in let peg = Grammar_original.peg in with_file_output "internal.peg" (fun oc -> Pretty.print_grammar oc peg); @@ -41,7 +41,6 @@ let bootstrap fn = module SM = Map.Make(String) -(*** build_color_table *) let build_color_table ?(min_color=1) ?(max_color=7) t = let color = ref min_color in let builds_table = ref SM.empty in @@ -58,8 +57,6 @@ let build_color_table ?(min_color=1) ?(max_color=7) t = Peg.iter_over_poly_tree_attributes (fun a -> add_color attributes_table a) t; (!builds_table, !attributes_table) -(* ***) -(*** colorize *) let colorize ?(default_acolor=Ansi.black) ?(default_bcolor=Ansi.white) (builds_table, attributes_table) u pt = let m = String.length u in let ac = Array.make (m + 1) default_acolor in @@ -88,8 +85,6 @@ let colorize ?(default_acolor=Ansi.black) ?(default_bcolor=Ansi.white) (builds_t loop_build pt; (ac, bc) -(* ***) -(*** dump_colorized *) let dump_colorized oc (ac, bc) u = let m = String.length u in let last_fg_color = ref (-1) in @@ -122,9 +117,7 @@ let dump_colorized oc (ac, bc) u = done; fp oc "%s%!" Ansi.none -(* ***) -(*** parse_file_with_nog *) let parse_file_with_nog pg fn = let with_dump_oc = match !Opt.dump_colorized with @@ -208,10 +201,8 @@ let parse_file_with_nog pg fn = | End_of_file -> () end else - treat (Driver.read_file fn) + treat (Util.read_file fn) -(* ***) -(*** parse_file_with_prog *) let parse_file_with_prog prog fn = let with_dump_oc = match !Opt.dump_colorized with @@ -267,10 +258,8 @@ let parse_file_with_prog prog fn = | End_of_file -> () end else - treat (Driver.read_file fn) + treat (Util.read_file fn) -(* ***) -(*** parse_file *) let parse_file peg fn = (*let peg' = List.map @@ -331,10 +320,8 @@ let parse_file peg fn = end end else - treat (Driver.read_file fn) + treat (Util.read_file fn) -(* ***) -(*** process *) let process fno = let (v1,v2,v3) = Version.version in banner "Aurochs %d.%d.%d" v1 v2 v3; @@ -346,9 +333,14 @@ let process fno = | Some fn -> let u = Aurochs.read_file fn in info `Normal "Grammar loaded from file %s" fn; - let peg = Convert_grammar.convert_grammar (Grammar.parse u) in - info `Minor "Grammar converted"; - peg + try + let peg = Convert_grammar.convert_grammar (Grammar.parse u) in + info `Minor "Grammar converted"; + peg + with + | Nog.Parse_error i -> + info `Important "PARSE ERROR IN GRAMMAR FILE %s AT CHARACTER %d" fn i; + exit 1 end in @@ -488,4 +480,3 @@ let process fno = ) targets -(* ***) diff --git a/aurochs/version.ml b/aurochs/version.ml index 47c2ee3..b4c98bc 100644 --- a/aurochs/version.ml +++ b/aurochs/version.ml @@ -1 +1 @@ -let version = (1,0,93) +let version = (1,0,94) diff --git a/backends/camelus.ml b/backends/camelus.ml index 86d6ed0..f6a99a7 100644 --- a/backends/camelus.ml +++ b/backends/camelus.ml @@ -10,24 +10,10 @@ module B = Boolean module SM = Map.Make(String) -(*** gensym *) -let gensym = - let t = Hashtbl.create 1009 in - fun prefix -> - let n = - try - Hashtbl.find t prefix - with - | Not_found -> 0 - in - Hashtbl.replace t prefix (n + 1); - sf "_%s_%d" prefix n - -(* ***) let print_node oc n = fp oc "%s%s" !Opt.node_prefix n let print_attr oc n = fp oc "%s%s" !Opt.attribute_prefix n -(*** generate_type_defs *) +(* generate_type_defs *) let generate_type_defs oc pg peg = let attributes, attribute_numbers = Nog.number_attributes pg peg and nodes, node_numbers = Nog.number_nodes pg peg @@ -40,16 +26,16 @@ let generate_type_defs oc pg peg = sum_type print_node "node_name" node_numbers; fp oc "\n"; sum_type print_attr "attribute_name" attribute_numbers -(* ***) -(*** generate_interface *) + +(* generate_interface *) let generate_interface ?(pack=true) fn pg peg = Util.with_file_output (fn^".mli") (fun oci -> fp oci "(* %s *)\n" (String.capitalize fn); fp oci "\n"; if pack then fp oci "\nopen Aurochs_pack;\n"; generate_type_defs oci pg peg) -(* ***) -(*** generate_classic *) + +(* generate_classic *) let generate_classic fn start peg (pg : (string, string) program) = let m = List.length peg in let num_alternatives = ref 0 in @@ -75,14 +61,6 @@ let generate_classic fn start peg (pg : (string, string) program) = let oc = open_out (fn^".ml") in let oci = open_out (fn^".mli") in - (*** pri *) - (*let pri oc d = - for i = 1 to d do - fp oc " " - done - in*) - (* ***) - fp oc "(* %s *)\n" (String.capitalize fn); fp oc "\n"; fp oci "(* %s *)\n" (String.capitalize fn); @@ -95,10 +73,11 @@ let generate_classic fn start peg (pg : (string, string) program) = fp oc "\n"; fp oc "exception Parse_error of int * string\n"; fp oc "\n"; + (* Collect builds *) let root_node = !Opt.root_node in - (*** build_names *) + (* build_names *) let build_names prefix iterator = let map = ref SM.empty in let number = ref 0 in @@ -132,7 +111,7 @@ let generate_classic fn start peg (pg : (string, string) program) = fp oc "\n" in - (* ***) + let print_node oc n = fp oc "%s%s" !Opt.node_prefix n in let print_attr oc n = fp oc "%s%s" !Opt.attribute_prefix n in fp oc "type node_name = \n"; @@ -155,144 +134,6 @@ let generate_classic fn start peg (pg : (string, string) program) = fp oci "val program : (node_name, attribute_name) Nog.program\n"; fp oc "let print_tree oc t = Peg.print_poly_tree ~print_node:print_node_name ~print_attribute:print_attribute_name () oc t\n"; - (* - (*** bexpr *) - fp oc "let build c u =\n"; - - let rec bexpr ?(indent=0) ?choice_number = function - | BOF -> fp oc "%a(* BOF *)\n" pri indent - | EOF -> fp oc "%a(* EOF *)\n" pri indent - | Epsilon -> fp oc "%a(* Epsilon *)\n" pri indent - | Tokenize x -> - fp oc "%achildren := (Token(String.sub u _i (r - _i))) :: !_children;" pri indent; - | Ascribe(n, x) -> - fp oc "%alet saved_i = _i in\n" pri indent; - bexpr ~indent x; - fp oc "%a_attributes := (%s%s, String.sub u saved_i (_i - saved_i)) :: !_attributes;\n" pri indent !Opt.attribute_prefix n - | N n -> fp oc "%alet _i = _build_%s _node _i in\n" pri indent n - | S xl -> - fp oc "%a(* Begin S *)\n" pri indent; - List.iter - begin fun x -> - bexpr ~indent:(indent + 1) x; - end - xl; - fp oc "%a(* End S *)\n" pri indent; - | Or xl -> - let choice_number' = - match choice_number with - | None -> invalid_arg "Unremoved inner disjunction in bexpr" - | Some n -> n - in - fp oc "%a(* Begin Or *)\n" pri indent; - fp oc "%aignore begin match c.choices.(%d).(_i) with\n" pri indent choice_number'; - fp oc "%a| None -> invalid_arg \"Bad choice table\"\n" pri indent; - fp oc "%a| Some x ->\n" pri indent; - let indent = indent + 1 in - fp oc "%amatch x with\n" pri indent; - let position = ref 0 in - List.iter - begin fun x -> - fp oc "%a| %d -> begin\n" pri indent !position; - bexpr ~indent:(indent + 1) x; - fp oc "%a_i\n" pri (indent + 1); - fp oc "%aend\n" pri indent; - incr position - end - xl; - fp oc "%a| _ -> invalid_arg \"Bad memo table\"\n" pri indent; - fp oc "%aend; (* End Or *)\n" pri (indent - 1) - | Build(n, xl) -> - fp oc "%a(* Build %s *)\n" pri indent n; - let old_node = gensym "old_node" in - fp oc "%alet %s = _node in\n" pri indent old_node; - fp oc "%alet (_attributes, _children) as _node = (ref [], ref []) in\n" pri indent; - bexpr ~indent (S xl); - fp oc "%alet new_node = Node(%s%s, List.rev !_attributes, List.rev !_children) in\n" pri indent !Opt.node_prefix n; - fp oc "%alet (_attributes, _children) as _node = %s in\n" pri indent old_node; - fp oc "%a_children := new_node :: !_children;\n" pri indent; - fp oc "%a(* End build %s *)\n" pri indent n - | And x -> fp oc "%a(* And *)\n" pri indent - | Not x -> fp oc "%a(* Not *)\n" pri indent - | Opt _|Star _|Plus _ -> invalid_arg "Not supported" - | A v -> fp oc "%alet _i = _i + %d in\n" pri indent (String.length v) - | C _ -> fp oc "%alet _i = _i + 1 in\n" pri indent - (*| _ -> fp oc "_i = c->c_result;\n"*) - (* ***) - in - (*** Generate builder function bodies *) - let first = ref true in - List.iter - begin fun (name, expr) -> - let (number, choice_number) = Hashtbl.find productions name in - if !first then - begin - first := false; - fp oc "let rec " - end - else - fp oc "and "; - fp oc "_build_%s ((_attributes, _children) as _node) _i =\n" name; - fp oc " match c.memo.(%d).(_i) with\n" number; - fp oc " | Jump r -> begin\n"; - bexpr ~indent:3 ?choice_number expr; - fp oc " r\n"; - fp oc " end\n"; - fp oc " | _ -> raise (Parse_error(_i, %S))\n" name - end - peg; - (* ***) - fp oc " in\n"; - fp oc " let ((_attributes, _children) as _node) = (ref [], ref []) in\n"; - fp oc " ignore (_build_%s _node 0);\n" start; - fp oc " (Node(%s%s, List.rev !_attributes, List.rev !_children))\n" !Opt.node_prefix root_node; - fp oc "\n"; - fp oc "\n"; - *) - - - (* - let pgm = Marshal.to_string pg [] in - let m = String.length pgm in - fp oc "let program : program =\n"; - fp oc " let u = String.create %d in\n" m; - fp oc " let d = \n"; - let rec loop i j = - if i + j = m then - begin - fp oc "\"\n"; - fp oc " in\n" - end - else - if j = 40 then - begin - fp oc "\\\n"; - loop (i + j) 0 - end - else - begin - if j = 0 then - if i = 0 then - fp oc " \"" - else - fp oc " " - else - (); - let c = Char.code pgm.[i + j] in - let ch = c lsr 4 - and cl = c land 15 - in - fp oc "%c%c" (Char.chr (97 + ch)) (Char.chr (97 + cl)); - loop i (j + 1) - end - in - loop 0 0; - fp oc " for i = 0 to %d do\n" (m - 1); - fp oc " u.[i] <- Char.chr ((((Char.code d.[2 * i]) - 97) lsl 4) + (((Char.code d.[2 * i + 1] - 97))))\n"; - fp oc " done;\n"; - fp oc " Marshal.from_string u 0\n"; - fp oc "\n";*) - fp oc "let program = {\n"; fp oc " pg_start = %S;\n" pg.pg_start; fp oc " pg_start_pc = %d;\n" pg.pg_start_pc; @@ -322,8 +163,8 @@ let generate_classic fn start peg (pg : (string, string) program) = fp oci "val parse_positioned : string -> positioned_tree\n"; fp oci "val parse : string -> tree\n" -(* ***) -(*** generate_implementation *) + +(* generate_implementation *) let generate_implementation ?(pack=true) fn start peg (pg : (string, string) program) = Util.with_file_output (fn^".ml") (fun oc -> Util.with_file_output (fn^".mli") (fun oci -> @@ -390,4 +231,4 @@ let generate_implementation ?(pack=true) fn start peg (pg : (string, string) pro fp oci "val print_tree : out_channel -> tree -> unit\n"; ) ) -(* ***) + diff --git a/backends/ritchie.ml b/backends/ritchie.ml index b5d7153..8b543af 100644 --- a/backends/ritchie.ml +++ b/backends/ritchie.ml @@ -20,9 +20,8 @@ let r_eof = 0 and r_unknown = 1 and r_fail = 2 and r_busy = 3 -;; -(*** print_c_char *) +(* print_c_char *) let print_c_char oc c = let k = Char.code c in fp oc "/* %d */ " k; @@ -36,9 +35,8 @@ let print_c_char oc c = fp oc "%C" c else fp oc "'\\%03o'" k -;; -(* ***) -(*** generate *) + +(* generate *) let generate fn ?(start="start") peg = let m = List.length peg in let num_alternatives = ref 0 in @@ -63,7 +61,7 @@ let generate fn ?(start="start") peg = end; let oc = open_out (fn^".c") in let och = open_out (fn^".h") in - (*** bexpr *) + (* bexpr *) let rec bexpr ?choice_number = function | BOF -> fp oc "/* BOF */\n" | EOF -> fp oc "/* EOF */\n" @@ -138,9 +136,9 @@ let generate fn ?(start="start") peg = | Ax(v, _) | A v -> fp oc "i += %d;\n" (String.length v) | C _ -> fp oc "i ++;\n" (*| _ -> fp oc "i = c->c_result;\n"*) - (* ***) + in - (*** gexpr *) + (* gexpr *) let rec gexpr ?choice_number = function | EOF -> fp oc "if(i < 0) i = R_FAIL;\n" | BOF -> fp oc "if(i != - cx->cx_input_length) i = R_FAIL;\n" @@ -233,8 +231,8 @@ let generate fn ?(start="start") peg = else i = saved_i; }\n" | Opt _ | Star _ | Plus _ -> invalid_arg "Uncanonical grammar (saw Opt, Star or Plus)" - (* ***) - (*** gatom *) + + (* gatom *) and gatom = function | One c -> fp oc "(i < 0 && u[i] == %a)" print_c_char c | Range(c1,c2) -> fp oc "(i < 0 && %a <= u[i] && u[i] <= %a)" print_c_char c1 print_c_char c2 @@ -249,8 +247,8 @@ let generate fn ?(start="start") peg = fp oc "(i < 0 && u[i] == %a)" print_c_char c end cl - (* ***) - (*** gbool *) + + (* gbool *) and gbool = function | B.True -> fp oc "1" | B.False -> fp oc "0" @@ -286,12 +284,12 @@ let generate fn ?(start="start") peg = gbool b; fp oc ")" in - (* ***) + fp oc "#include \n"; fp oc "#include \"peg_prelude.h\"\n"; let is_static name = name <> start in fp oc "#include \"%s.h\"\n" fn; - (*** Function prototypes *) + (* Function prototypes *) List.iter begin fun (name, expr) -> let st = is_static name in @@ -301,12 +299,12 @@ let generate fn ?(start="start") peg = if not st then fp och "int %sbuild_%s(context *, node *, int);\n" !Opt.function_prefix name; end peg; - (* ***) - (*** create_context *) + + (* create_context *) fp och "#define NUM_PRODUCTIONS %d\n" !num_productions; fp och "#define NUM_ALTERNATIVES %d\n" !num_alternatives; - (* ***) - (*** Generate parser function bodies *) + + (* Generate parser function bodies *) info `Time "Generating parser functions"; if not !Opt.build_only then List.iter @@ -336,8 +334,8 @@ let generate fn ?(start="start") peg = fp oc "}\n" end peg; - (* ***) - (*** Generate builder function bodies *) + + (* Generate builder function bodies *) List.iter begin fun (name, expr) -> let (number, choice_number) = Hashtbl.find productions name in @@ -350,8 +348,7 @@ let generate fn ?(start="start") peg = fp oc "}\n"; end peg; - (* ***) + fp oc "\n"; fp och "\n"; -;; -(* ***) + diff --git a/backends/stringifier.ml b/backends/stringifier.ml index 8e1108f..0563b6f 100644 --- a/backends/stringifier.ml +++ b/backends/stringifier.ml @@ -1,12 +1,11 @@ (* Stringifier *) -open Pffpsf;; +open Pffpsf let print_indent oc n = for i = 0 to n - 1 do output_char oc ' ' done -;; let print_ocaml_string ?(indent=4) ?(cols=32) () oc u = let m = String.length u in @@ -28,4 +27,3 @@ let print_ocaml_string ?(indent=4) ?(cols=32) () oc u = end in loop 0 -;; diff --git a/cutil/alloc.c b/c/alloc.c similarity index 100% rename from cutil/alloc.c rename to c/alloc.c diff --git a/cnog/cnog.c b/c/nog.c similarity index 79% rename from cnog/cnog.c rename to c/nog.c index 2d67743..143d6fc 100644 --- a/cnog/cnog.c +++ b/c/nog.c @@ -1,16 +1,16 @@ -/* cnog.c +/* nog.c * */ -#include -#include -#include +#include +#include +#include #include #include #include #include -#define CNOG_DEBUG 0 +#define NOG_DEBUG 0 static INLINE memo_block_t *memo_alloc_block(alloc_t *alloc, int cells_per_block) { @@ -123,7 +123,7 @@ static INLINE void set_result(peg_context_t *cx, int position, int production, i set_memo(&cx->cx_table_staloc->s_alloc, cx->cx_results, position, production, result, RESULT_CELLS_PER_BLOCK); } -int cnog_error_position(peg_context_t *cx, nog_program_t *pg) +int nog_error_position(peg_context_t *cx, nog_program_t *pg) { int i, j, k; int m; @@ -156,10 +156,10 @@ typedef struct { letter_t *head, *bof, *eof; /* Pointers to current position, beginning and end. */ peg_builder_t *bd; info bi; -} cnog_closure_t; +} nog_closure_t; /* Initialize to defined values */ -static void init(cnog_closure_t *c, peg_context_t *cx, nog_program_t *pg, tree *result) { +static void init(nog_closure_t *c, peg_context_t *cx, nog_program_t *pg, tree *result) { c->cx = cx; c->pg = pg; c->result = result; @@ -175,12 +175,12 @@ static void init(cnog_closure_t *c, peg_context_t *cx, nog_program_t *pg, tree * } /* Boolean stack manipulation */ -static INLINE void boolean_push(cnog_closure_t *c, bool x) { +static INLINE void boolean_push(nog_closure_t *c, bool x) { c->boolean <<= 1; c->boolean |= x ? 1 : 0; } -static INLINE bool boolean_pop(cnog_closure_t *c) { +static INLINE bool boolean_pop(nog_closure_t *c) { bool result; result = c->boolean & 1; @@ -189,17 +189,17 @@ static INLINE bool boolean_pop(cnog_closure_t *c) { } /* Regular stack manipulation */ -static INLINE void stack_push(cnog_closure_t *c, symbol_t x) { +static INLINE void stack_push(nog_closure_t *c, symbol_t x) { pushdown_push(c->cx->cx_stack, x); } -static INLINE symbol_t stack_pop(cnog_closure_t *c) { +static INLINE symbol_t stack_pop(nog_closure_t *c) { symbol_t s; (void) pushdown_pop(c->cx->cx_stack, &s); return s; } -static INLINE symbol_t stack_top(cnog_closure_t *c) { +static INLINE symbol_t stack_top(nog_closure_t *c) { symbol_t s; (void) pushdown_top(c->cx->cx_stack, &s); return s; @@ -211,7 +211,7 @@ static INLINE symbol_t stack_top(cnog_closure_t *c) { #define jump_to(pc) do { ip_next = c->pg->np_program + pc; } while(0) #define jump() do { jump_to(arg0()); } while(0) -static nog_instruction_t *run(cnog_closure_t *c, construction current, nog_instruction_t *ip_next, tree *result_tree) { +static nog_instruction_t *run(nog_closure_t *c, construction current, nog_instruction_t *ip_next, tree *result_tree) { nog_instruction_t *ip; /*printf("run pc=%ld i=%ld c->sp=%ld c->fail=%d c->memo=%d\n", ip_next - pg->np_program, c->head - c->bof, c->sp - c->cx->cx_stack, c->fail, c->memo);*/ @@ -223,7 +223,7 @@ static nog_instruction_t *run(cnog_closure_t *c, construction current, nog_instr assert(c->pg->np_program <= ip && ip < c->pg->np_program + c->pg->np_count); assert(c->bof <= c->head && c->head <= c->eof); /*printf("pc=%ld i=%ld c->sp=%ld c->fail=%d c->memo=%d\n", ip - c->pg->np_program, c->head - c->bof, c->sp - c->cx->cx_stack, c->fail, c->memo);*/ - DEBUGIF(CNOG_DEBUG,"%ld %ld %d\n", ip - c->pg->np_program, c->head - c->bof, c->fail); + DEBUGIF(NOG_DEBUG,"%ld %ld %d\n", ip - c->pg->np_program, c->head - c->bof, c->fail); ip_next = ip + 1; @@ -500,11 +500,11 @@ static nog_instruction_t *run(cnog_closure_t *c, construction current, nog_instr #undef jump_to #undef jump -#define CNOG_VERSION 0x00010001 +#define NOG_VERSION 0x00010001 -bool cnog_execute(peg_context_t *cx, nog_program_t *pg, tree *result) +bool nog_execute(peg_context_t *cx, nog_program_t *pg, tree *result) { - cnog_closure_t c; + nog_closure_t c; init(&c, cx, pg, result); if(run(&c, 0, pg->np_program + pg->np_start_pc, 0)) { @@ -526,7 +526,7 @@ bool cnog_execute(peg_context_t *cx, nog_program_t *pg, tree *result) return false; } -static void cnog_add_to_checksum(void *info, u8 *data, size_t size) +static void nog_add_to_checksum(void *info, u8 *data, size_t size) { u64 sum; @@ -538,55 +538,55 @@ static void cnog_add_to_checksum(void *info, u8 *data, size_t size) *((u64 *) info) = sum; } -nog_program_t *cnog_unpack_program(alloc_t *alloc, packer_t *pk) { +nog_program_t *nog_unpack_program(alloc_t *alloc, packer_t *pk) { nog_program_t *pg, *result; u64 signature, version; size_t size; unsigned int i, j; u64 checksum, checksum2; - DEBUGIF(CNOG_DEBUG,"Unpacking\n"); + DEBUGIF(NOG_DEBUG,"Unpacking\n"); result = 0; checksum = 0; - pack_set_observer(pk, &checksum, cnog_add_to_checksum); + pack_set_observer(pk, &checksum, nog_add_to_checksum); pg = alloc_malloc(alloc, sizeof(nog_program_t)); /* Welcome to C allocation hell! */ if(!pg) goto finish; - DEBUGIF(CNOG_DEBUG,"Allocated program\n"); + DEBUGIF(NOG_DEBUG,"Allocated program\n"); if(!pack_read_uint64(pk, &signature)) goto finish; - DEBUGIF(CNOG_DEBUG,"Read signature %lx\n", signature); + DEBUGIF(NOG_DEBUG,"Read signature %lx\n", signature); if(signature != NOG_SIGNATURE) goto finish; - DEBUGIF(CNOG_DEBUG,"Signature OK\n"); + DEBUGIF(NOG_DEBUG,"Signature OK\n"); if(!pack_read_uint64(pk, &version)) goto finish; - if(version <= CNOG_VERSION) { - DEBUGIF(CNOG_DEBUG,"Version too recent\n"); + if(version <= NOG_VERSION) { + DEBUGIF(NOG_DEBUG,"Version too recent\n"); goto finish; } - DEBUGIF(CNOG_DEBUG,"Version OK\n"); + DEBUGIF(NOG_DEBUG,"Version OK\n"); if(!pack_read_uint(pk, &pg->np_start_pc)) goto finish; - DEBUGIF(CNOG_DEBUG,"Start pc is %d\n", pg->np_start_pc); + DEBUGIF(NOG_DEBUG,"Start pc is %d\n", pg->np_start_pc); if(!pack_read_uint(pk, &pg->np_build_pc)) goto finish; - DEBUGIF(CNOG_DEBUG,"Build pc is %d\n", pg->np_build_pc); + DEBUGIF(NOG_DEBUG,"Build pc is %d\n", pg->np_build_pc); if(!pack_read_uint(pk, &pg->np_root_constructor)) goto finish; - DEBUGIF(CNOG_DEBUG,"Root constructor is %d\n", pg->np_root_constructor); + DEBUGIF(NOG_DEBUG,"Root constructor is %d\n", pg->np_root_constructor); if(!pack_read_uint(pk, &pg->np_num_productions)) goto finish; - DEBUGIF(CNOG_DEBUG,"Num_productions is %d\n", pg->np_num_productions); + DEBUGIF(NOG_DEBUG,"Num_productions is %d\n", pg->np_num_productions); if(!pack_read_uint(pk, &pg->np_num_choices)) goto finish; - DEBUGIF(CNOG_DEBUG,"Num_choices is %d\n", pg->np_num_choices); + DEBUGIF(NOG_DEBUG,"Num_choices is %d\n", pg->np_num_choices); if(!pack_read_uint(pk, &pg->np_num_constructors)) goto finish; - DEBUGIF(CNOG_DEBUG,"Num_constructors is %d\n", pg->np_num_constructors); + DEBUGIF(NOG_DEBUG,"Num_constructors is %d\n", pg->np_num_constructors); pg->np_constructors = alloc_malloc(alloc, sizeof(nog_string_t) * pg->np_num_constructors); if(!pg->np_constructors) goto finish; @@ -594,24 +594,24 @@ nog_program_t *cnog_unpack_program(alloc_t *alloc, packer_t *pk) { for(i = 0; i < pg->np_num_constructors; i ++) { if(!pack_read_string(pk, &pg->np_constructors[i].ns_chars, &size, alloc)) goto finish; pg->np_constructors[i].ns_length = size; - DEBUGIF(CNOG_DEBUG," Constructor #%d: %s\n", i, pg->np_constructors[i].ns_chars); + DEBUGIF(NOG_DEBUG," Constructor #%d: %s\n", i, pg->np_constructors[i].ns_chars); } if(!pack_read_uint(pk, &pg->np_num_attributes)) goto finish; - DEBUGIF(CNOG_DEBUG,"Num_attributes is %d\n", pg->np_num_attributes); + DEBUGIF(NOG_DEBUG,"Num_attributes is %d\n", pg->np_num_attributes); pg->np_attributes = alloc_malloc(alloc, sizeof(nog_string_t) * pg->np_num_attributes); if(!pg->np_attributes) goto finish; for(i = 0; i < pg->np_num_attributes; i ++) { if(!pack_read_string(pk, &pg->np_attributes[i].ns_chars, &size, alloc)) goto finish; pg->np_attributes[i].ns_length = size; - DEBUGIF(CNOG_DEBUG," Attribute #%d: %s\n", i, pg->np_attributes[i].ns_chars); + DEBUGIF(NOG_DEBUG," Attribute #%d: %s\n", i, pg->np_attributes[i].ns_chars); } if(!pack_read_uint(pk, &pg->np_num_tables)) goto finish; - DEBUGIF(CNOG_DEBUG,"Num_tables is %d\n", pg->np_num_tables); + DEBUGIF(NOG_DEBUG,"Num_tables is %d\n", pg->np_num_tables); pg->np_tables = alloc_malloc(alloc, sizeof(nog_table_t) * pg->np_num_tables); if(!pg->np_tables) goto finish; @@ -624,14 +624,14 @@ nog_program_t *cnog_unpack_program(alloc_t *alloc, packer_t *pk) { } if(!pack_read_uint(pk, &pg->np_count)) goto finish; - DEBUGIF(CNOG_DEBUG,"Program size is %d\n", pg->np_count); + DEBUGIF(NOG_DEBUG,"Program size is %d\n", pg->np_count); pg->np_program = alloc_malloc(alloc, sizeof(nog_instruction_t) * pg->np_count); if(!pg->np_program) goto finish; for(i = 0; i < pg->np_count; i ++) { - if(!cnog_unpack_instruction(alloc, pk, pg->np_program + i)) { - DEBUGIF(CNOG_DEBUG, "Unpack error at instruction %d\n", i); + if(!nog_unpack_instruction(alloc, pk, pg->np_program + i)) { + DEBUGIF(NOG_DEBUG, "Unpack error at instruction %d\n", i); goto finish; } } @@ -640,10 +640,10 @@ nog_program_t *cnog_unpack_program(alloc_t *alloc, packer_t *pk) { if(!pack_read_uint64(pk, &checksum2)) goto finish; if(checksum != checksum2) { - DEBUGIF(CNOG_DEBUG, "Bad checksum, residual 0x%lx recorded 0x%lx\n", checksum, checksum2); + DEBUGIF(NOG_DEBUG, "Bad checksum, residual 0x%lx recorded 0x%lx\n", checksum, checksum2); goto finish; } else { - DEBUGIF(CNOG_DEBUG, "Checksum OK 0x%lx\n", checksum2); + DEBUGIF(NOG_DEBUG, "Checksum OK 0x%lx\n", checksum2); } result = pg; @@ -651,71 +651,17 @@ nog_program_t *cnog_unpack_program(alloc_t *alloc, packer_t *pk) { return result; } -void cnog_free_program(alloc_t *alloc, nog_program_t *pg) +void nog_free_program(alloc_t *alloc, nog_program_t *pg) { unsigned int i; if(pg) { if(pg->np_program) { for(i = 0; i < pg->np_count; i ++) { - cnog_free_instruction(alloc, pg->np_program + i); + nog_free_instruction(alloc, pg->np_program + i); } alloc_free(alloc, pg->np_program); } alloc_free(alloc, pg); } } - -peg_context_t *peg_create_context(alloc_t *alloc, nog_program_t *pg, peg_builder_t *pb, info bi, letter_t *input, int input_length) -{ - int i; - peg_context_t *cx; - int num_alternatives; - int num_productions; - alloc_t *salloc; - - cx = alloc_malloc(alloc, sizeof(peg_context_t)); - - cx->cx_alloc = alloc; - cx->cx_table_staloc = staloc_create(alloc); - salloc = &cx->cx_table_staloc->s_alloc; - - cx->cx_input = input; - cx->cx_input_length = input_length; - - num_alternatives = pg->np_num_choices; - num_productions = pg->np_num_productions; - - cx->cx_num_alternatives = num_alternatives; - cx->cx_num_productions = num_productions; - - cx->cx_choices = alloc_malloc(salloc, sizeof(memo_block_t *) * (input_length + 1)); - - for(i = 0; i <= input_length; i ++) { - cx->cx_choices[i] = 0; - } - - cx->cx_results = alloc_malloc(alloc, sizeof(memo_block_t *) * (input_length + 1)); - for(i = 0; i <= input_length; i ++) { - cx->cx_results[i] = 0; - } - - cx->cx_builder = pb; - cx->cx_builder_info = bi; - - cx->cx_stack = pushdown_create(alloc); - - return cx; -} - -void peg_delete_context(peg_context_t *cx) -{ - if(cx) { -#if CNOG_SHOW_STATS - statistics(cx); -#endif - staloc_dispose(cx->cx_table_staloc); - pushdown_dispose(cx->cx_stack); - alloc_free(cx->cx_alloc, (cx)); - } -} diff --git a/cpack/pack.c b/c/pack.c similarity index 100% rename from cpack/pack.c rename to c/pack.c diff --git a/cnog/parse_tree.c b/c/parse_tree.c similarity index 100% rename from cnog/parse_tree.c rename to c/parse_tree.c diff --git a/cutil/pushdown.c b/c/pushdown.c similarity index 100% rename from cutil/pushdown.c rename to c/pushdown.c diff --git a/cutil/staloc.c b/c/staloc.c similarity index 100% rename from cutil/staloc.c rename to c/staloc.c diff --git a/cnog/check.c b/c/test/test_nog.c similarity index 95% rename from cnog/check.c rename to c/test/test_nog.c index 4f0f2a1..7884da2 100644 --- a/cnog/check.c +++ b/c/test/test_nog.c @@ -8,11 +8,7 @@ #include #include -#include -#include -#include -#include -#include +#include static unsigned char *load_file(char *name, size_t *size)/*{{{*/ { @@ -81,7 +77,7 @@ int main(int argc, char **argv) if(pack_init_from_string(&pk, peg_data, peg_data_size)) { printf("peg_data[0] = %d\n", peg_data[0]); - pg = cnog_unpack_program(&st->s_alloc, &pk); + pg = nog_unpack_program(&st->s_alloc, &pk); printf("Unpacked to %p\n", pg); if(pg) { peg_context_t *cx; @@ -110,12 +106,12 @@ int main(int argc, char **argv) if(cx) { tree tr; - if(cnog_execute(cx, pg, &tr)) { + if(nog_execute(cx, pg, &tr)) { printf("Parsed as %p.\n", tr); ptree_dump_tree(cx->cx_builder_info, stdout, buf, tr, 0); } else { printf("Doesn't parse.\n"); - error_pos = cnog_error_position(cx, pg); + error_pos = nog_error_position(cx, pg); printf("Error at %d\n", error_pos); } @@ -155,7 +151,7 @@ int main(int argc, char **argv) } #endif - /* cnog_free_program(&st->s_alloc, pg); */ + /* nog_free_program(&st->s_alloc, pg); */ staloc_dispose(st); } } diff --git a/cpack/test_pack.c b/c/test/test_pack.c similarity index 100% rename from cpack/test_pack.c rename to c/test/test_pack.c diff --git a/cpack/Makefile b/cpack/Makefile deleted file mode 100644 index d648929..0000000 --- a/cpack/Makefile +++ /dev/null @@ -1,21 +0,0 @@ -# Makefile - -CC=gcc -CFLAGS=-Wall -fPIC -g -I . -I .. -#CFLAGS=-Wall -fPIC -g -I . -I .. -O3 -HEADERS=pack.h - -.PHONY: all clean - -all: libpack.so - -clean: - rm -f test_pack *.o *.so - -test_pack: - $(CC) $(CFLAGS) -I. -L. -lpack test_pack.c -o test_pack - -libpack.so: pack.o - $(CC) -shared $(CFLAGS) $^ -o $@ - -%.o: $(HEADERS) diff --git a/genmachine.ml b/genmachine.ml index 588b3ee..ff2207f 100644 --- a/genmachine.ml +++ b/genmachine.ml @@ -68,15 +68,15 @@ let gen_c_unpacker ops cfn hfn = fp och " *\n"; fp och " */\n"; fp och "\n"; - fp och "#ifndef CNOG_UNPACK_H\n"; - fp och "#define CNOG_UNPACK_H\n"; + fp och "#ifndef NOG_UNPACK_H\n"; + fp och "#define NOG_UNPACK_H\n"; fp och "\n"; fp och "#include \n"; fp och "#include \n"; - fp och "#include \n"; + fp och "#include \n"; fp och "\n"; - fp och "bool cnog_unpack_instruction(alloc_t *alloc, packer_t *pk, nog_instruction_t *ins);\n"; - fp och "void cnog_free_instruction(alloc_t *alloc, nog_instruction_t *ins);\n"; + fp och "bool nog_unpack_instruction(alloc_t *alloc, packer_t *pk, nog_instruction_t *ins);\n"; + fp och "void nog_free_instruction(alloc_t *alloc, nog_instruction_t *ins);\n"; fp och "\n"; fp och "#endif\n"; (* ***) @@ -88,11 +88,11 @@ let gen_c_unpacker ops cfn hfn = fp occ " */\n"; fp occ "\n"; fp occ "#include \n"; - fp occ "#include \n"; + fp occ "#include \n"; fp occ "\n"; (*** Free *) - fp occ "void cnog_free_instruction(alloc_t *alloc, nog_instruction_t *ins)\n"; + fp occ "void nog_free_instruction(alloc_t *alloc, nog_instruction_t *ins)\n"; fp occ "{\n"; fp occ " switch(ins->ni_opcode) {\n"; @@ -129,7 +129,7 @@ let gen_c_unpacker ops cfn hfn = fp occ "\n"; (* ***) (*** Unpack *) - fp occ "bool cnog_unpack_instruction(alloc_t *alloc, packer_t *pk, nog_instruction_t *ins)\n"; + fp occ "bool nog_unpack_instruction(alloc_t *alloc, packer_t *pk, nog_instruction_t *ins)\n"; fp occ "{\n"; fp occ " int opcode;\n"; fp occ " u64 arg;\n"; @@ -260,8 +260,8 @@ let gen_ocaml_packer ops fn = ;; let _ = - List.iter Unix_util.mkdirhier ["nog"; "backends"; "cnog"]; + List.iter Unix_util.mkdirhier ["nog"; "backends"; "c"]; let ops = load_opcodes "nog/machine.ml" in gen_ocaml_packer ops "backends/nog_packer.ml"; - gen_c_unpacker ops "cnog/cnog_unpack.c" "include/cnog_unpack.h" + gen_c_unpacker ops "c/nog_unpack.c" "include/nog_unpack.h" ;; diff --git a/include/aurochs.h b/include/aurochs.h new file mode 100644 index 0000000..809a821 --- /dev/null +++ b/include/aurochs.h @@ -0,0 +1,13 @@ +/* aurochs.h + * + */ + +#ifndef AUROCHS_H +#define AUROCHS_H + +#include +#include +#include +#include + +#endif diff --git a/include/cnog.h b/include/nog.h similarity index 91% rename from include/cnog.h rename to include/nog.h index 49a3e3d..324c935 100644 --- a/include/cnog.h +++ b/include/nog.h @@ -1,11 +1,10 @@ -/* cnog.h +/* nog.h * */ -#ifndef CNOG_H -#define CNOG_H +#ifndef NOG_H +#define NOG_H -#include #include #include @@ -103,10 +102,10 @@ typedef struct { nog_instruction_t *np_program; } nog_program_t; -EXPORT bool cnog_execute(peg_context_t *cx, nog_program_t *pg, tree *result); -EXPORT int cnog_error_position(peg_context_t *cx, nog_program_t *pg); -EXPORT nog_program_t *cnog_unpack_program(alloc_t *alloc, packer_t *pk); -EXPORT void cnog_free_program(alloc_t *alloc, nog_program_t *pg); +EXPORT bool nog_execute(peg_context_t *cx, nog_program_t *pg, tree *result); +EXPORT int nog_error_position(peg_context_t *cx, nog_program_t *pg); +EXPORT nog_program_t *nog_unpack_program(alloc_t *alloc, packer_t *pk); +EXPORT void nog_free_program(alloc_t *alloc, nog_program_t *pg); #define NOG_SIGNATURE 0xABBE55E5 diff --git a/include/pack.h b/include/pack.h index 9b3ecaf..8f6020a 100644 --- a/include/pack.h +++ b/include/pack.h @@ -8,7 +8,6 @@ #ifndef PACK_H #define PACK_H -#include #include typedef size_t (*pack_resplenisher_t)(void *, u8 *, size_t); diff --git a/include/peg.h b/include/peg.h index f6bbfa3..3bad726 100644 --- a/include/peg.h +++ b/include/peg.h @@ -1,14 +1,12 @@ /* peg.h * - * Copyright (C)2007 Exalead + * Copyright (C)2007-2009 Exalead */ #ifndef PEG_H #define PEG_H #include -#include -#include #include #include diff --git a/include/peg_lib.h b/include/peg_lib.h deleted file mode 100644 index f057edc..0000000 --- a/include/peg_lib.h +++ /dev/null @@ -1,17 +0,0 @@ -/* peg_lib.h - * - */ - -#ifndef PEG_LIB_H -#define PEG_LIB_H - -#include -#include -#include - -EXPORT peg_context_t *peg_create_context(alloc_t *alloc, nog_program_t *pg, peg_builder_t *pb, info bi, letter_t *input, int input_length); -EXPORT void peg_delete_context(peg_context_t *cx); -EXPORT void peg_dump_context(FILE *f, peg_context_t *cx); -EXPORT int peg_error_position(peg_context_t *cx); - -#endif diff --git a/libaurochs.clib b/libaurochs.clib index 45e3236..549229a 100644 --- a/libaurochs.clib +++ b/libaurochs.clib @@ -1,8 +1,9 @@ -cnog/cnog.o -cpack/pack.o -cnog/parse_tree.o -cnog/cnog_unpack.o -cutil/alloc.o -cutil/staloc.o -cutil/pushdown.o +c/nog.o +c/pack.o +c/parse_tree.o +c/nog_unpack.o +c/alloc.o +c/staloc.o +c/peg.o +c/pushdown.o aurochs/aurochs_native.o diff --git a/myocamlbuild.ml b/myocamlbuild.ml index 2b54e53..42ab662 100644 --- a/myocamlbuild.ml +++ b/myocamlbuild.ml @@ -1,12 +1,12 @@ (* Ocamlbuild plugin *) -open Ocamlbuild_pack;; -open Ocamlbuild_plugin;; -open Command;; -open Ocaml_specific;; -open Outcome;; +open Ocamlbuild_pack +open Ocamlbuild_plugin +open Command +open Ocaml_specific +open Outcome -let ( & ) f x = f x;; +let ( & ) f x = f x let ocaml_local_dir = lazy begin @@ -19,7 +19,6 @@ let ocaml_local_dir = Log.dprintf 5 "Getting Ocaml directory from command %s" cmd; input_line ic) end -;; let cflags = lazy begin @@ -30,9 +29,9 @@ let cflags = with | Not_found -> S[] end -;; -let local dir = Filename.concat (Lazy.force ocaml_local_dir) dir;; + +let local dir = Filename.concat (Lazy.force ocaml_local_dir) dir type libdep_description = { ld_name : string; @@ -42,16 +41,16 @@ type libdep_description = { ld_include : string; ld_static : bool; ld_c_headers : string list -};; +} type ocaml_lib_description = { od_path : string; od_name : string; od_headers : string list; od_incdirs : string list -};; +} -let system_lib_dir = "/usr/lib";; +let system_lib_dir = "/usr/lib" let zlib_description = { ld_name = "zlib"; @@ -76,7 +75,7 @@ let aurochslib_description = { let aurochs_lib_description = { od_path = ""; (* (Lazy.force ocaml_local_dir)^"/aurochs_lib/";*) od_name = "aurochs"; - od_headers = [ "include/cnog.h"; + od_headers = [ "include/nog.h"; "include/peg.h"; "include/peg_lib.h"; "include/parse_tree.h"; @@ -85,7 +84,8 @@ let aurochs_lib_description = { "include/pushdown.h"; "include/alloc.h"; "include/parse_tree.h"; - "include/base_types.h" ]; + "include/base_types.h"; + "include/aurochs.h" ]; od_incdirs = [ "include"; "_build/include" ]; } @@ -115,14 +115,10 @@ let ocamllib old = flag ["link"; "library"; "ocaml"; "native"; "use_lib"^u] (S[(*A"-cclib";A("-L"^old.od_path);*)A"-cclib"; A("-l"^u)]); - (* When ocaml link something that use the libX - then one need that file to be up to date. *) dep ["link"; "ocaml"; "use_lib"^u] [old.od_path^"lib"^u^".a"]; - - (* As an approximation all our C files use the headers. - Note: This will import headers in the build directory. *) - dep ["compile"; "c"] old.od_headers; + dep ["compile"; "c"] old.od_headers ;; + (* ***) dispatch @@ -157,24 +153,12 @@ dispatch [(* local "java"; local "compiler" *)]; - (*ocaml_lib "java"; - ocaml_lib "compiler";*) - (*ocaml_lib "float32";*) - Log.dprintf 5 "Ready"; ocamllib aurochs_lib_description; - (*flag ["ocaml"; "byte"; "library"; "float32"] (S[A"-Lfloat32";A"-lfloat32"]);*) - (*flag ["ocaml"; "native"; "program"; "float32"] (S[A"-cclib"; A"float32/dllfloat32.so"]); WORKS *) - (*flag ["ocaml"; "native"; "program"; "float32"] (S[A"-verbose";A"-ccopt";A"-Lfloat32";A"-cclib"; A"float32/dllfloat32.so"]);*) - (*flag ["ocaml"; "link"; "native"; "use_float32"] (S[A"-cclib";A"-Lfloat32";A"-cclib"; A"-lfloat32"]); - flag ["ocaml"; "link"; "byte"; "use_float32"] (S[A"-dllpath";A"float32";A"-dllib"; A"-lfloat32"]);*) - (*dep ["use_float32"] ["float32/libfloat32.so"];*) - (*dep ["file:astivore/astivore.native"] ["float32/dllfloat32.so"];*) - rule "Generation" - ~prods:["cnog/cnog_unpack.c";"include/cnog_unpack.h";"backends/nog_packer.ml"] + ~prods:["c/nog_unpack.c";"include/nog_unpack.h";"backends/nog_packer.ml"] ~deps:["genmachine.byte";"nog/machine.ml"] begin fun env _build -> Seq[ @@ -184,10 +168,10 @@ dispatch end; rule "Program" - ~prods:["cnog/check"] - ~deps:["cnog/check.o"; "libaurochs.a"] + ~prods:["c/test/test_nog"] + ~deps:["c/test/test_nog.o"; "libaurochs.a"] begin fun env _build -> - Cmd(S[A"gcc"; A"cnog/check.o"; A"-L."; A"-laurochs"; A"-o"; A"cnog/check"]) + Cmd(S[A"gcc"; A"c/test/test_nog.o"; A"-L."; A"-laurochs"; A"-o"; A"c/test/test_nog"]) end; rule "Bootstrap" @@ -208,4 +192,3 @@ dispatch end | _ -> () end -;; diff --git a/util/util.ml b/util/util.ml index fedbd3f..1a36dbf 100644 --- a/util/util.ml +++ b/util/util.ml @@ -2,10 +2,10 @@ (* Copyright (C)2004-2006 Berke Durak *) (* Released under the GNU Lesser General Public License version 2.1 *) -let sf = Printf.sprintf;; -exception At of string * exn;; +let sf = Printf.sprintf + +exception At of string * exn -(*** first_line *) let first_line = let b = Buffer.create 256 in fun w -> @@ -20,9 +20,7 @@ let first_line = end in loop 0 -;; -(* ***) -(*** limit *) + let limit m w = let n = String.length w in if n <= m then @@ -32,9 +30,7 @@ let limit m w = String.make m '.' else (String.sub w 0 (min (m - 3) n))^"..." -;; -(* ***) -(*** limit_left *) + let limit_left m w = let n = String.length w in if n <= m then @@ -45,9 +41,7 @@ let limit_left m w = else let m' = min (m - 3) n in "..."^(String.sub w (m - m') m') -;; -(* ***) -(*** for_all_chars *) + let for_all_chars f w = let m = String.length w in let rec loop i = @@ -57,9 +51,7 @@ let for_all_chars f w = f w.[i] && loop (i + 1) in loop 0 -;; -(* ***) -(*** split_once_at *) + let split_once_at f s = let m = String.length s in let rec loop1 i = @@ -82,28 +74,20 @@ let split_once_at f s = String.sub s j (m - j)) with | Not_found -> (s, "") -;; -(* ***) -(*** is_alpha *) + let is_alpha = function | 'a'..'z' -> true | 'A'..'Z' -> true | _ -> false -;; -(* ***) -(*** is_digit *) + let is_digit = function | '0'..'9' -> true | _ -> false -;; -(* ***) -(*** is_space *) + let is_space = function | ' '|'\t'|'\n' -> true | _ -> false -;; -(* ***) -(*** parse_strings *) + let parse_strings u = let m = String.length u in let b = Buffer.create m in @@ -170,9 +154,7 @@ let parse_strings u = end in loop0 [] 0 -;; -(* ***) -(*** split_at *) + let split_at c u = let m = String.length u in let b = Buffer.create m in @@ -198,18 +180,14 @@ let split_at c u = end in loop0 [] 0 -;; -(* ***) -(*** list_intersect *) + let list_intersect l1 l2 = let rec loop r = function | [] -> r | x::y -> loop (if List.mem x l2 then x::r else r) y in loop [] l1 -;; -(* ***) -(*** once *) + let once f = let x = ref true in fun () -> @@ -220,15 +198,11 @@ let once f = end else () -;; -(* ***) -(*** list_has_more_than_one_element *) + let list_has_more_than_one_element = function | []|[_] -> false | _ -> true -;; -(* ***) -(*** count_lines *) + let count_lines w = let m = String.length w in let rec loop x i = @@ -238,9 +212,7 @@ let count_lines w = loop (if w.[i] = '\n' then x + 1 else x) (i + 1) in loop 1 0 -;; -(* ***) -(*** first_matching_char_from *) + let first_matching_char_from i f w = let m = String.length w in let rec loop i = @@ -253,39 +225,29 @@ let first_matching_char_from i f w = loop (i + 1) in loop i -;; -(* ***) -(*** first_matching_char *) -let first_matching_char = first_matching_char_from 0;; -(* ***) -(*** longest_matching_prefix *) + +let first_matching_char = first_matching_char_from 0 let longest_matching_prefix f w = try let i = first_matching_char (fun c -> not (f c)) w in String.sub w 0 i, String.sub w i (String.length w - i) with | Not_found -> (w,"") -;; -(* ***) -(*** remove_leading_spaces *) + let remove_leading_spaces w = try let i = first_matching_char (fun c -> not (is_space c)) w in String.sub w i (String.length w - i) with | Not_found -> w -;; -(* ***) -(*** delete_first_chars *) + let delete_first_chars n w = let m = String.length w in if m > n then String.sub w n (m - n) else "" -;; -(* ***) -(*** hierarchical *) + let hierarchical x y = let m = String.length x and n = String.length y @@ -296,9 +258,7 @@ let hierarchical x y = 1 else compare x y -;; -(* ***) -(*** wind *) + let wind f x g y = begin try @@ -310,25 +270,19 @@ let wind f x g y = g y; raise z end -;; -(* ***) -(*** list_change_nth *) + let rec list_change_nth l n z = match l,n with | [],_ -> raise Not_found | x::y,0 -> z::y | x::y,_ -> x::(list_change_nth y (n - 1) z) -;; -(* ***) -(*** list_remove_nth *) + let rec list_remove_nth l n = match l,n with | [],_ -> raise Not_found | x::y,0 -> y | x::y,_ -> x::(list_remove_nth y (n - 1)) -;; -(* ***) -(*** word_wrap *) + let word_wrap oc ?(columns=75) u = let m = String.length u in let f c = output_char oc c @@ -416,9 +370,7 @@ let word_wrap oc ?(columns=75) u = loop2 i0 (i + 1) j k in loop0 0 0 -;; -(* ***) -(*** reg_of_string *) + let reg_of_string w = let m = String.length w in let b = Buffer.create m in @@ -429,9 +381,7 @@ let reg_of_string w = | c -> Buffer.add_char b c done; Buffer.contents b -;; -(* ***) -(*** flip_array *) + let flip_array a = let m = Array.length a in for i = 0 to m / 2 - 1 do @@ -439,25 +389,19 @@ let flip_array a = a.(i) <- a.(m - 1 - i); a.(m - 1 - i) <- t done -;; -(* ***) -(*** substitute_variables *) + let substitute_variables env w = let b = Buffer.create (String.length w) in Buffer.add_substitute b (fun v -> List.assoc v env) w; Buffer.contents b -;; -(* ***) -(*** list_sub_rev *) + let list_sub_rev l start length = let rec loop r j = function | [] -> r (* shall we raise an exception ? *) | x::y -> loop (if j < start or j >= start + length then r else x::r) (j + 1) y in loop [] 0 l -;; -(* ***) -(*** is_prefix *) + let is_prefix u v = let m = String.length u and n = String.length v @@ -465,18 +409,14 @@ let is_prefix u v = m <= n && let rec loop i = i = m or u.[i] = v.[i] && loop (i + 1) in loop 0 -;; -(* ***) -(*** remove_prefix *) + let remove_prefix u v = if is_prefix u v then let m = String.length u in String.sub v m (String.length v - m) else v -;; -(* ***) -(*** is_suffix *) + let is_suffix u v = let m = String.length u and n = String.length v @@ -484,18 +424,14 @@ let is_suffix u v = m <= n && let rec loop i = i = m or u.[m - 1 - i] = v.[n - 1 - i] && loop (i + 1) in loop 0 -;; -(* ***) -(*** remove_suffix *) + let remove_suffix u v = if is_suffix u v then let m = String.length u in String.sub v 0 (String.length v - m) else v -;; -(* ***) -(*** lowercase_compare *) + let lowercase_compare u v = let m = String.length u and n = String.length v @@ -505,29 +441,21 @@ let lowercase_compare u v = else let rec loop i = i = m || (Char.lowercase u.[i] = Char.lowercase v.[i] && loop (i + 1)) in loop 0 -;; -(* ***) -(*** call_if *) + let call_if f x = match f with | None -> () | Some g -> g x -;; -(* ***) -(*** optional *) + let optional f x = match x with | None -> () | Some y -> f y -;; -(* ***) -(*** mandatory *) + let mandatory = function | None -> raise Not_found | Some x -> x -;; -(* ***) -(*** wrap *) + let wrap x g f = begin try @@ -539,9 +467,7 @@ let wrap x g f = g x; raise z end -;; -(* ***) -(*** binary_search *) + let binary_search compare a x = let m = Array.length a in let rec loop i0 m = @@ -568,9 +494,7 @@ let binary_search compare a x = end in loop 0 m -;; -(* ***) -(*** randomize *) + let randomize a = let m = Array.length a in let swap i j = @@ -582,9 +506,7 @@ let randomize a = let j = i + 1 + Random.int (m - i - 1) in swap i j done -;; -(* ***) -(*** array_mem_assoc *) + let array_mem_assoc x a = let m = Array.length a in let rec loop i = @@ -594,9 +516,7 @@ let array_mem_assoc x a = x = y or loop (i + 1) in loop 0 -;; -(* ***) -(*** array_assoc *) + let array_assoc x a = let m = Array.length a in let rec loop i = @@ -609,25 +529,19 @@ let array_assoc x a = loop (i + 1) in loop 0 -;; -(* ***) -(*** inside *) + let inside msg f x = try f x with | x -> raise (At(msg, x)) -;; -(* ***) -(*** display_exception *) + let rec display_exception = function | At(location, x) -> Printf.eprintf " At %s:\n" location; display_exception x | x -> Printf.eprintf " %s.\n" (Printexc.to_string x) -;; -(* ***) -(*** catch *) + let catch f = try f () @@ -636,10 +550,8 @@ let catch f = Printf.eprintf "Caught exception:\n"; display_exception x; Printf.eprintf "%!"; - exit 1; -;; -(* ***) -(*** sanitize_filename *) + exit 1 + let sanitize_filename ?(is_safe= (function @@ -659,9 +571,7 @@ let sanitize_filename Printf.bprintf b "%%%02x" (Char.code c) done; Buffer.contents b -;; -(* ***) -(*** unsanitize_filename *) + let unsanitize_filename ?(buffer=Buffer.create 256) ?(prefix="") fn = if not (is_prefix prefix fn) then invalid_arg "unsanitize_filename: no prefix"; let b = buffer in @@ -693,9 +603,7 @@ let unsanitize_filename ?(buffer=Buffer.create 256) ?(prefix="") fn = end in loop (String.length prefix) -;; -(* ***) -(*** with_file_input *) + let with_file_input fn f = let ic = open_in fn in try @@ -704,9 +612,7 @@ let with_file_input fn f = | x -> close_in ic; raise x -;; -(* ***) -(*** with_file_output *) + let with_file_output fn f = let oc = open_out fn in try @@ -717,9 +623,7 @@ let with_file_output fn f = | x -> close_out oc; raise x -;; -(* ***) -(*** with_binary_file_output *) + let with_binary_file_output fn f = let oc = open_out_bin fn in try @@ -730,9 +634,7 @@ let with_binary_file_output fn f = | x -> close_out oc; raise x -;; -(* ***) -(*** with_binary_file_input *) + let with_binary_file_input fn f = let ic = open_in_bin fn in try @@ -741,15 +643,9 @@ let with_binary_file_input fn f = | x -> close_in ic; raise x -;; -(* ***) -(*** save *) -let save fn x = with_binary_file_output fn (fun oc -> Marshal.to_channel oc x []);; -(* ***) -(*** load *) -let load fn = with_binary_file_input fn (fun ic -> Marshal.from_channel ic);; -(* ***) -(*** iter_over_lines *) + +let save fn x = with_binary_file_output fn (fun oc -> Marshal.to_channel oc x []) +let load fn = with_binary_file_input fn (fun ic -> Marshal.from_channel ic) let iter_over_lines ic f = try while true do @@ -759,9 +655,7 @@ let iter_over_lines ic f = assert false with | End_of_file -> () -;; -(* ***) -(*** unsigned_int64_of_decimal *) + let unsigned_int64_of_decimal u = let m = String.length u in let rec loop q i = @@ -772,15 +666,19 @@ let unsigned_int64_of_decimal u = loop (Int64.add (Int64.mul q 10L) (Int64.of_int x)) (i + 1) in loop 0L 0 -;; -(* ***) -(*** Syntax *) + module Syntax = struct - let (&) f x = f x;; (* From Nicolas Pouillard *) - let ( += ) l x = l := x :: !l;; - let ( |> ) x f = f x;; - let ( ) x f = f x + let ( 'a -> unit val load : string -> 'a val iter_over_lines : in_channel -> (string -> unit) -> unit val unsigned_int64_of_decimal : string -> int64 +val read_file : string -> string module Syntax : sig