aboutsummaryrefslogtreecommitdiff
path: root/haskell/step1_read_print.hs
blob: c7a4eef5ced7a544dbc7f26df3719d78999a2fdb (plain)
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
35
36
37
38
39
40
41
42
43
44
45
import System.IO (hFlush, stdout)
import Control.Monad.Error (runErrorT)

import Readline (readline, load_history)
import Types
import Reader (read_str)
import Printer (_pr_str)

-- read
mal_read :: String -> IOThrows MalVal
mal_read str = read_str str

-- eval
eval :: MalVal -> String -> MalVal
eval ast env = ast

-- print
mal_print :: MalVal -> String
mal_print exp = show exp

-- repl
rep :: String -> IOThrows String
rep line = do
    ast <- mal_read line
    return $ mal_print (eval ast "")

repl_loop :: IO ()
repl_loop = do
    line <- readline "user> "
    case line of
        Nothing -> return ()
        Just "" -> repl_loop
        Just str -> do
            res <- runErrorT $ rep str
            out <- case res of
                Left (StringError str) -> return $ "Error: " ++ str
                Left (MalValError mv) -> return $ "Error: " ++ (show mv)
                Right val -> return val
            putStrLn out
            hFlush stdout
            repl_loop

main = do
    load_history
    repl_loop