-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathREPL.hs
More file actions
34 lines (32 loc) · 1.04 KB
/
REPL.hs
File metadata and controls
34 lines (32 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
--
-- Copyright (c) 2005 Lennart Augustsson
-- See LICENSE for licensing details.
--
module REPL(REPL(..), repl) where
import qualified Control.Exception
import System.Console.Readline(readline, addHistory)
data REPL s = REPL {
repl_init :: IO (String, s), -- prompt and initial state
repl_eval :: s -> String -> IO (Bool, s), -- quit flag and new state
repl_exit :: s -> IO ()
}
repl :: REPL s -> IO ()
repl p = do
(prompt, state) <- repl_init p
let loop s = (do
mline <- readline prompt
case mline of
Nothing -> loop s
Just line -> do
(quit, s') <- repl_eval p s line
if quit then
repl_exit p s'
else do
addHistory line
loop s'
) `Control.Exception.catch` ( \ exc ->
do
putStrLn $ "\nInterrupted (" ++ show exc ++ ")"
loop s
)
loop state