=== modified file 'Makefile' --- Makefile 2019-04-15 17:00:06 +0000 +++ Makefile 2019-04-12 23:47:50 +0000 @@ -3,11 +3,8 @@ clean: rm -rf _build *.o *.bc *.cm[iox] -.PHONY: all clean - -OBJ:= ring.cmo subprocess.cmo ppio.cmo -ppio.bc: $(OBJ) compile - ./compile $(OBJ) -o ppio.bc +ppio.bc: subprocess.cmo ppio.cmo compile + ./compile subprocess.cmo ppio.cmo -o ppio.bc %.cmo: %.ml compile ./compile -c $< === modified file 'ppio.ml' --- ppio.ml 2019-04-15 17:00:06 +0000 +++ ppio.ml 2019-04-13 08:40:40 +0000 @@ -58,7 +58,6 @@ type display_mode = NoFormat | Normal | Escaped;; type pretty_state = {to_print: Buffer.t; mutable dm: display_mode};; -module CharSet = Set.Make(Char);; let pp_writer hideendl endl colors = let s = {to_print = Buffer.create 80; dm = NoFormat} in @@ -122,51 +121,7 @@ if c = endl then add_char '\n' else pp_char c; done; write_out ())) - | (l, false) -> ( - let ring = Ring.create l in - fun (len:int) (buffer:Bytes.t) -> ( - for n = 0 to len - 1 do - let c = Bytes.get buffer n in - pp_char c; - let _ = Ring.add_char ring c in - if Ring.compare ring endl = 0 then ( - add_char '\n'; - Ring.clear ring; - ) - done; - write_out () - ) - ) - | (l, true) -> ( - let ring = Ring.create l - and endl_chars = String.fold ~init:CharSet.empty ~f:CharSet.add endl - in - fun (len:int) (buffer:Bytes.t) -> if len = 0 then ( - (* end of input, dump ring buffer *) - Sequence.iter ~f:pp_char (Ring.fwd ring); - Ring.clear ring; - write_out (); - ) else ( - for n = 0 to len - 1 do - let c = Bytes.get buffer n in - if CharSet.mem endl_chars c then ( - (match Ring.add_char ring c with - Some buffered_c -> pp_char c | None -> ()); - if Ring.compare ring endl = 0 then ( - add_char '\n'; - Ring.clear ring; - ) - ) else ( - if Ring.length ring > 0 then ( - Sequence.iter ~f:pp_char (Ring.fwd ring); - Ring.clear ring - ); - pp_char c - ) - done; - write_out (); - ) - ) + | _ -> failwith "Not implemented" ;; (* Copy data from the reader to the writer, using the provided buffer === removed file 'ring.ml' --- ring.ml 2019-04-15 17:00:06 +0000 +++ ring.ml 1970-01-01 00:00:00 +0000 @@ -1,76 +0,0 @@ -open Base;; - -type ring = { - buf: char Array.t; (* array serving as data store *) - mutable len: int; (* amount of characters currently retained *) - mutable pos: int; (* position in buffer of last character written *) -};; - -let create size = { - buf = Array.create size '\x00'; - len = 0; - pos = -1; -};; - -let length r = r.len;; - -let clear r = - r.len <- 0; - r.pos <- -1; -;; - -let add_char r ch = - if r.len < Array.length r.buf then ( - r.pos <- r.pos + 1; - Array.set r.buf r.pos ch; - r.len <- r.pos + 1; - None - ) else ( - r.pos <- (r.pos + 1) % (Array.length r.buf); - let prev_ch = Array.get r.buf r.pos in - Array.set r.buf r.pos ch; - Some prev_ch - ) -;; - -let rev_str s = Sequence.unfold_step ~init:(String.length s - 1) ~f:( - function n -> - if n < 0 - then Sequence.Step.Done - else Sequence.Step.Yield (String.get s n, n - 1) - ) -;; - -let rev r = Sequence.unfold_step ~init:(r.len, r.pos) ~f:( - function (remaining, pos) -> - if remaining <= 0 - then Sequence.Step.Done - else Sequence.Step.Yield ( - Array.get r.buf pos, - (remaining - 1, (pos - 1) % (Array.length r.buf)) - ) - ) -;; - -let fwd r = - Sequence.unfold_step - ~init:( - r.len, - if r.len < (Array.length r.buf) then 0 else (r.pos + 1) % (Array.length r.buf) - ) - ~f:(function (remaining, pos) -> - if remaining <= 0 - then Sequence.Step.Done - else Sequence.Step.Yield ( - Array.get r.buf pos, - (remaining - 1, (pos + 1) % (Array.length r.buf)) - ) - ) -;; - -let compare (r:ring) (s:string) = - let size_diff = r.len - (String.length s) in - if size_diff = 0 then ( - Sequence.compare Char.compare (rev r) (rev_str s) - ) else if size_diff < 0 then -1 else 1 -;;