0 | module System.REPL
 1 |
 2 | import System.File
 3 |
 4 | %default covering
 5 |
 6 | ||| A basic read-eval-print loop, maintaining a state
 7 | |||
 8 | ||| @ state   the input state
 9 | ||| @ prompt  the prompt to show
10 | ||| @ onInput the function to run on reading input, returning a String to
11 | |||           output and a new state. Returns Nothing if the repl should exit
12 | export
13 | replWith : HasIO io =>
14 |            (state : a) -> (prompt : String) ->
15 |            (onInput : a -> String -> Maybe (String, a)) -> io ()
16 | replWith acc prompt fn
17 |    = do eof <- fEOF stdin
18 |         if eof
19 |           then pure ()
20 |           else do putStr prompt
21 |                   fflush stdout
22 |                   x <- getLine
23 |                   case fn acc x of
24 |                        Just (out, acc') =>
25 |                            do putStr out
26 |                               replWith acc' prompt fn
27 |                        Nothing => pure ()
28 |
29 | ||| A basic read-eval-print loop
30 | |||
31 | ||| @ prompt  the prompt to show
32 | ||| @ onInput the function to run on reading input, returning a String to
33 | |||           output
34 | export
35 | repl : HasIO io =>
36 |        (prompt : String) -> (onInput : String -> String) -> io ()
37 | repl prompt fn
38 |    = replWith () prompt (\x, s => Just (fn s, ()))
39 |