mirror of
https://github.com/c-cube/nanoev.git
synced 2025-12-06 03:05:32 -05:00
rename to nanoev.sm
This commit is contained in:
parent
2713f92dcf
commit
a1dfe51cab
6 changed files with 41 additions and 17 deletions
|
|
@ -1,4 +0,0 @@
|
|||
|
||||
(library
|
||||
(name nanoev_cps)
|
||||
(public_name nanoev.cps))
|
||||
5
src/sm/dune
Normal file
5
src/sm/dune
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
|
||||
(library
|
||||
(name nanoev_sm)
|
||||
(public_name nanoev.sm)
|
||||
(synopsis "state machines for sans-io"))
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
|
||||
(tests
|
||||
(names t_readline)
|
||||
(libraries nanoev.cps))
|
||||
(libraries nanoev.sm))
|
||||
23
tests/sm/t_readline.expected
Normal file
23
tests/sm/t_readline.expected
Normal 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
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
module CPS = Nanoev_cps
|
||||
module SM = Nanoev_sm
|
||||
|
||||
let spf = Printf.sprintf
|
||||
let pf = Printf.printf
|
||||
|
|
@ -39,30 +39,30 @@ type input =
|
|||
| `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
|
||||
CPS.create_rec @@ fun ~aq ~recurse ->
|
||||
SM.create_rec @@ fun ~aq ~recurse ->
|
||||
match
|
||||
for i = 0 to self.len - 1 do
|
||||
if Bytes.get self.bs i = '\n' then raise_notrace (Found i)
|
||||
done
|
||||
with
|
||||
| () ->
|
||||
CPS.await @@ fun input ->
|
||||
SM.await @@ fun input ->
|
||||
(match input with
|
||||
| `End_of_input when self.len = 0 -> CPS.return ()
|
||||
| `End_of_input when self.len = 0 -> SM.return ()
|
||||
| `End_of_input ->
|
||||
let line = Bytes.sub_string self.bs 0 self.len in
|
||||
St.shift_left self self.len;
|
||||
CPS.perform ~aq (`Yield line);
|
||||
CPS.return ()
|
||||
SM.perform ~aq (`Yield line);
|
||||
SM.return ()
|
||||
| `Input (buf, i, len) ->
|
||||
St.add_bytes self buf i len;
|
||||
recurse ())
|
||||
| exception Found i ->
|
||||
let line = Bytes.sub_string self.bs 0 i in
|
||||
St.shift_left self (i + 1);
|
||||
CPS.perform ~aq (`Yield line);
|
||||
SM.perform ~aq (`Yield line);
|
||||
recurse ()
|
||||
|
||||
let test (inputs : string list) =
|
||||
|
|
@ -72,21 +72,21 @@ let test (inputs : string list) =
|
|||
let rd = readline () in
|
||||
|
||||
let work () =
|
||||
(match CPS.work rd with
|
||||
(match SM.work rd with
|
||||
| Ok () -> pf "done\n"
|
||||
| Error _ -> .
|
||||
| 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
|
||||
|
||||
work ();
|
||||
while not (Queue.is_empty inputs) do
|
||||
let s = Queue.pop inputs in
|
||||
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 ()
|
||||
done;
|
||||
CPS.add_input rd `End_of_input;
|
||||
SM.add_input rd `End_of_input;
|
||||
work ();
|
||||
|
||||
pf "## end test\n"
|
||||
Loading…
Add table
Reference in a new issue