Commit 2de85e1f authored by POTTIER Francois's avatar POTTIER Francois

Added --dry-run option to generator. Everything seems to work.

parent 370743e6
.PHONY: all clean .PHONY: all clean
OCAMLBUILD := ocamlbuild -use-ocamlfind # Build an absolute path for Menhir.
# This is useful because ocamlbuild descends into _build.
ifndef MENHIR
READLINK := $(shell if which -s greadlink ; then echo greadlink ; else echo readlink ; fi)
MENHIR := $(shell $(READLINK) -f ../../src/_stage1/menhir.native)
endif
OCAMLBUILD := ocamlbuild -use-ocamlfind -menhir "$(MENHIR) --table"
all: all:
$(OCAMLBUILD) gene.native $(OCAMLBUILD) gene.native
......
...@@ -41,12 +41,13 @@ let rec map f xs () = ...@@ -41,12 +41,13 @@ let rec map f xs () =
| More (x, xs) -> | More (x, xs) ->
More (f x, map f xs) More (f x, map f xs)
(* An infinite, imperative stream. *) (* A finite or infinite imperative stream. By convention, end-of-stream is
signaled via an exception. *)
type 'a infinite_imperative_stream = type 'a imperative_stream =
unit -> 'a unit -> 'a
let fresh (xs : 'a stream) : 'a infinite_imperative_stream = let fresh (xs : 'a stream) : 'a imperative_stream =
let r = ref xs in let r = ref xs in
fun () -> fun () ->
match !r() with match !r() with
...@@ -56,3 +57,18 @@ let fresh (xs : 'a stream) : 'a infinite_imperative_stream = ...@@ -56,3 +57,18 @@ let fresh (xs : 'a stream) : 'a infinite_imperative_stream =
r := xs; r := xs;
x x
(* Beware that [find] will diverge if the stream is infinite. *)
let find (p : 'a -> bool) (xs : 'a imperative_stream) : 'a option =
try
let rec loop() =
let x = xs() in
if p x then
Some x
else
loop()
in
loop()
with End_of_file ->
None
...@@ -3,6 +3,8 @@ open Parser ...@@ -3,6 +3,8 @@ open Parser
open Stream open Stream
open Generator open Generator
(* ---------------------------------------------------------------------------- *)
(* A token printer, for debugging. *) (* A token printer, for debugging. *)
let print_token = function let print_token = function
...@@ -26,13 +28,42 @@ let print_token = function ...@@ -26,13 +28,42 @@ let print_token = function
let print_token_stream = let print_token_stream =
iter print_token iter print_token
(* Hooking the stream to the parser. *) (* ---------------------------------------------------------------------------- *)
(* Parse the command line. *)
(* [--dry-run] offers a choice between running just the generator, or both the
generator and the parser. *)
let dry_run =
ref false
let options = Arg.align [
"--dry-run", Arg.Set dry_run, "Run only the generator, not the parser";
]
let usage =
sprintf "Usage: %s <options>" Sys.argv.(0)
let () =
Arg.parse options (fun _ -> ()) usage
(* ---------------------------------------------------------------------------- *)
(* Run. *)
let parse xs = let wrap token =
MenhirLib.Convert.Simplified.traditional2revised Parser.main (token, Lexing.dummy_pos, Lexing.dummy_pos)
(fresh (map (fun token -> (token, Lexing.dummy_pos, Lexing.dummy_pos)) xs))
let () = let () =
let i : int = parse (produce 10000000) in let tks : token stream = produce 10000000 in
printf "%d\n%!" i let tks = fresh (map wrap tks) in
if !dry_run then begin
let _ = find (fun _ -> false) tks in
printf "Done.\n"
end
else begin
let i : int = MenhirLib.Convert.Simplified.traditional2revised Parser.main tks in
printf "%d\n%!" i
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment