rename to nanoev.sm

This commit is contained in:
Simon Cruanes 2024-12-04 23:22:45 -05:00
parent 2713f92dcf
commit a1dfe51cab
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
6 changed files with 41 additions and 17 deletions

View file

@ -1,4 +0,0 @@
(library
(name nanoev_cps)
(public_name nanoev.cps))

5
src/sm/dune Normal file
View file

@ -0,0 +1,5 @@
(library
(name nanoev_sm)
(public_name nanoev.sm)
(synopsis "state machines for sans-io"))

View file

@ -1,4 +1,4 @@
(tests (tests
(names t_readline) (names t_readline)
(libraries nanoev.cps)) (libraries nanoev.sm))

View file

@ -0,0 +1,23 @@
## test on ["hello\nwor";"ld\n\n123\n"]
feed input "hello\nwor"
yielded line "hello"
feed input "ld\n\n123\n"
yielded line "world"
yielded line ""
yielded line "123"
done
## end test
## test on ["a very very long";" line over here, wow, ";"damn!\noh well\n";"\nanother ";"one?\n";"all done\n"]
feed input "a very very long"
feed input " line over here, wow, "
feed input "damn!\noh well\n"
yielded line "a very very long line over here, wow, damn!"
yielded line "oh well"
feed input "\nanother "
yielded line ""
feed input "one?\n"
yielded line "another one?"
feed input "all done\n"
yielded line "all done"
done
## end test

View file

@ -1,4 +1,4 @@
module CPS = Nanoev_cps module SM = Nanoev_sm
let spf = Printf.sprintf let spf = Printf.sprintf
let pf = Printf.printf let pf = Printf.printf
@ -39,30 +39,30 @@ type input =
| `Input of bytes * int * int | `Input of bytes * int * int
] ]
let readline () : ([ `Yield of string ], input, unit, CPS.void) CPS.t = let readline () : ([ `Yield of string ], input, unit, SM.void) SM.t =
let self = St.create () in let self = St.create () in
CPS.create_rec @@ fun ~aq ~recurse -> SM.create_rec @@ fun ~aq ~recurse ->
match match
for i = 0 to self.len - 1 do for i = 0 to self.len - 1 do
if Bytes.get self.bs i = '\n' then raise_notrace (Found i) if Bytes.get self.bs i = '\n' then raise_notrace (Found i)
done done
with with
| () -> | () ->
CPS.await @@ fun input -> SM.await @@ fun input ->
(match input with (match input with
| `End_of_input when self.len = 0 -> CPS.return () | `End_of_input when self.len = 0 -> SM.return ()
| `End_of_input -> | `End_of_input ->
let line = Bytes.sub_string self.bs 0 self.len in let line = Bytes.sub_string self.bs 0 self.len in
St.shift_left self self.len; St.shift_left self self.len;
CPS.perform ~aq (`Yield line); SM.perform ~aq (`Yield line);
CPS.return () SM.return ()
| `Input (buf, i, len) -> | `Input (buf, i, len) ->
St.add_bytes self buf i len; St.add_bytes self buf i len;
recurse ()) recurse ())
| exception Found i -> | exception Found i ->
let line = Bytes.sub_string self.bs 0 i in let line = Bytes.sub_string self.bs 0 i in
St.shift_left self (i + 1); St.shift_left self (i + 1);
CPS.perform ~aq (`Yield line); SM.perform ~aq (`Yield line);
recurse () recurse ()
let test (inputs : string list) = let test (inputs : string list) =
@ -72,21 +72,21 @@ let test (inputs : string list) =
let rd = readline () in let rd = readline () in
let work () = let work () =
(match CPS.work rd with (match SM.work rd with
| Ok () -> pf "done\n" | Ok () -> pf "done\n"
| Error _ -> . | Error _ -> .
| Working -> ()); | Working -> ());
CPS.pop_actions rd (function `Yield line -> pf "yielded line %S\n%!" line) SM.pop_actions rd (function `Yield line -> pf "yielded line %S\n%!" line)
in in
work (); work ();
while not (Queue.is_empty inputs) do while not (Queue.is_empty inputs) do
let s = Queue.pop inputs in let s = Queue.pop inputs in
pf "feed input %S\n%!" s; pf "feed input %S\n%!" s;
CPS.add_input rd (`Input (Bytes.of_string s, 0, String.length s)); SM.add_input rd (`Input (Bytes.of_string s, 0, String.length s));
work () work ()
done; done;
CPS.add_input rd `End_of_input; SM.add_input rd `End_of_input;
work (); work ();
pf "## end test\n" pf "## end test\n"