-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.hs
53 lines (47 loc) · 1.69 KB
/
main.hs
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
46
47
48
49
50
51
52
53
import Control.Monad
import Data.IORef
import qualified Data.Map as Map
import System.Exit
import System.Environment
import System.IO
import Text.Parsec
import Expr
import Parse
import Eval
import ExprTypeCheck
readExpr :: String -> Expr
readExpr input = case parse parseExprs "hslisp" input of
Left err -> LispError $ "\nNo match: " ++ show err
Right val -> val
re :: String -> Context -> (Context, Expr) -- Read, Eval
re str ctx = eval ctx (readExpr str)
repl :: Context -> IO() -- Read, Eval, Print, Loop
repl initCtx = do
ctxRef <- newIORef initCtx
let loop = do
putStr "λ> "
hFlush stdout
line <- getLine
when (line == "quit") exitSuccess
ctx <- readIORef ctxRef
let val = re line ctx
print $ snd val
writeIORef ctxRef (fst val)
loop
loop
main :: IO()
main = do args <- getArgs
case args of
[] -> repl builtinMap
filenames -> do
ctxRef <- newIORef builtinMap
forM_ filenames $ \filename -> do
putStrLn $ "Loading " ++ filename ++ " ..."
content <- readFile filename
ctx <- readIORef ctxRef
let val = re content ctx
when (isErrorExpr $ snd val) (print (snd val) >> exitFailure)
writeIORef ctxRef (fst val)
putStrLn $ "Successfully loaded " ++ filename ++ "."
ctx <- readIORef ctxRef
repl ctx