haskell: adapt quasiquote to existing test, drop redundant import
[jackhill/mal.git] / haskell / step9_try.hs
CommitLineData
5400d4bf 1import System.IO (hFlush, stdout)
c150ec41 2import System.Environment (getArgs)
5400d4bf 3import Control.Monad (mapM)
53db2d63 4import Control.Monad.Except (runExceptT)
5400d4bf 5import Control.Monad.Trans (liftIO)
6116c2d5 6import Data.Foldable (foldlM, foldrM)
c150ec41
JM
7
8import Readline (readline, load_history)
9import Types
10import Reader (read_str)
11import Printer (_pr_str)
52371c3e 12import Env (env_new, env_bind, env_get, env_set)
6116c2d5 13import Core (ns)
c150ec41
JM
14
15-- read
6116c2d5 16
5400d4bf 17mal_read :: String -> IOThrows MalVal
6116c2d5 18mal_read = read_str
c150ec41
JM
19
20-- eval
c150ec41 21
6116c2d5
NB
22-- starts-with is replaced with pattern matching.
23
24qqIter :: Env -> MalVal -> [MalVal] -> IOThrows [MalVal]
25qqIter env (MalSeq _ (Vect False) [MalSymbol "splice-unquote", x]) acc = do
26 evaluated <- eval env x
27 case evaluated of
28 MalSeq _ (Vect False) xs -> return $ xs ++ acc
29 _ -> throwStr "invalid splice-unquote argument"
30qqIter _ (MalSeq _ (Vect False) (MalSymbol "splice-unquote" : _)) _ = throwStr "invalid splice-unquote"
31qqIter env x acc = (: acc) <$> quasiquote x env
32
33quasiquote :: MalVal -> Env -> IOThrows MalVal
34quasiquote (MalSeq _ (Vect False) [MalSymbol "unquote", x]) env = eval env x
52371c3e
NB
35-- FIXME This line
36quasiquote (MalSeq m _ ys) env = MalSeq m (Vect False) <$> foldrM (qqIter env) [] ys
37-- is adapted to broken tests. It should be:
38-- quasiquote (MalSeq m v ys) env = MalSeq m v <$> foldrM (qqIter env) [] ys
6116c2d5
NB
39quasiquote ast _ = return ast
40
41-- is-macro-call is replaced with pattern matching.
42
43macroexpand :: Env -> MalVal -> IOThrows MalVal
44macroexpand env ast@(MalSeq _ (Vect False) (MalSymbol a0 : args)) = do
45 maybeMacro <- liftIO $ env_get env a0
46 case maybeMacro of
47 Just (MalFunction {fn=f, macro=True}) -> macroexpand env =<< f args
48 _ -> return ast
49macroexpand _ ast = return ast
50
51-- eval_ast is replaced with pattern matching.
52
53let_bind :: Env -> [MalVal] -> IOThrows ()
54let_bind _ [] = return ()
55let_bind env (MalSymbol b : e : xs) = do
56 liftIO . env_set env b =<< eval env e
57 let_bind env xs
58let_bind _ _ = throwStr "invalid let*"
59
60unWrapSymbol :: MalVal -> IOThrows String
61unWrapSymbol (MalSymbol s) = return s
62unWrapSymbol _ = throwStr "fn* parameter must be symbols"
63
64newFunction :: MalVal -> Env -> [String] -> MalVal
65newFunction a env p = MalFunction {f_ast=a, f_params=p, macro=False, meta=Nil,
66 fn=(\args -> do
67 fn_env <- liftIO $ env_new env
68 ok <- liftIO $ env_bind fn_env p args
69 case ok of
70 True -> eval fn_env a
71 False -> throwStr $ "actual parameters do not match signature " ++ show p)}
72
73apply_ast :: [MalVal] -> Env -> IOThrows MalVal
74
75apply_ast [] _ = return $ toList []
76
77apply_ast [MalSymbol "def!", MalSymbol a1, a2] env = do
78 evd <- eval env a2
79 liftIO $ env_set env a1 evd
80 return evd
81apply_ast (MalSymbol "def!" : _) _ = throwStr "invalid def!"
82
83apply_ast [MalSymbol "let*", MalSeq _ _ params, a2] env = do
84 let_env <- liftIO $ env_new env
85 let_bind let_env params
86 eval let_env a2
87apply_ast (MalSymbol "let*" : _) _ = throwStr "invalid let*"
88
89apply_ast [MalSymbol "quote", a1] _ = return a1
90apply_ast (MalSymbol "quote" : _) _ = throwStr "invalid quote"
91
92apply_ast [MalSymbol "quasiquote", a1] env = quasiquote a1 env
93apply_ast (MalSymbol "quasiquote" : _) _ = throwStr "invalid quasiquote"
94
95apply_ast [MalSymbol "defmacro!", MalSymbol a1, a2] env = do
96 func <- eval env a2
97 case func of
98 MalFunction {macro=False} -> do
99 let m = func {macro=True}
100 liftIO $ env_set env a1 m
101 return m
102 _ -> throwStr "defmacro! on non-function"
103apply_ast (MalSymbol "defmacro!" : _) _ = throwStr "invalid defmacro!"
104
105apply_ast [MalSymbol "macroexpand", a1] env = macroexpand env a1
106apply_ast (MalSymbol "macroexpand" : _) _ = throwStr "invalid macroexpand"
107
108apply_ast [MalSymbol "try*", a1] env = eval env a1
109apply_ast [MalSymbol "try*", a1, MalSeq _ (Vect False) [MalSymbol "catch*", MalSymbol a21, a22]] env = do
110 res <- liftIO $ runExceptT $ eval env a1
111 case res of
112 Right val -> return val
113 Left exc -> do
114 try_env <- liftIO $ env_new env
115 liftIO $ env_set try_env a21 exc
116 eval try_env a22
117apply_ast (MalSymbol "try*" : _) _ = throwStr "invalid try*"
118
119apply_ast (MalSymbol "do" : args) env = foldlM (const $ eval env) Nil args
120
121apply_ast [MalSymbol "if", a1, a2, a3] env = do
122 cond <- eval env a1
123 eval env $ case cond of
124 Nil -> a3
125 MalBoolean False -> a3
126 _ -> a2
127apply_ast [MalSymbol "if", a1, a2] env = do
128 cond <- eval env a1
129 case cond of
130 Nil -> return Nil
131 MalBoolean False -> return Nil
132 _ -> eval env a2
133apply_ast (MalSymbol "if" : _) _ = throwStr "invalid if"
134
135apply_ast [MalSymbol "fn*", MalSeq _ _ params, ast] env = newFunction ast env <$> mapM unWrapSymbol params
136apply_ast (MalSymbol "fn*" : _) _ = throwStr "invalid fn*"
137
138apply_ast ast env = do
139 evd <- mapM (eval env) ast
140 case evd of
141 MalFunction {fn=f, macro=False} : args -> f args
142 _ -> throwStr $ "invalid apply: " ++ Printer._pr_str True (toList ast)
143
144eval :: Env -> MalVal -> IOThrows MalVal
145eval env ast = do
146 newAst <- macroexpand env ast
147 case newAst of
148 MalSymbol sym -> do
149 maybeVal <- liftIO $ env_get env sym
150 case maybeVal of
151 Nothing -> throwStr $ "'" ++ sym ++ "' not found"
152 Just val -> return val
153 MalSeq _ (Vect False) xs -> apply_ast xs env
154 MalSeq m (Vect True) xs -> MalSeq m (Vect True) <$> mapM (eval env) xs
155 MalHashMap m xs -> MalHashMap m <$> mapM (eval env) xs
156 _ -> return newAst
c150ec41
JM
157
158-- print
6116c2d5 159
c150ec41 160mal_print :: MalVal -> String
6116c2d5 161mal_print = Printer._pr_str True
c150ec41
JM
162
163-- repl
164
5400d4bf 165rep :: Env -> String -> IOThrows String
6116c2d5 166rep env line = mal_print <$> (eval env =<< mal_read line)
c150ec41
JM
167
168repl_loop :: Env -> IO ()
169repl_loop env = do
170 line <- readline "user> "
171 case line of
172 Nothing -> return ()
173 Just "" -> repl_loop env
174 Just str -> do
53db2d63 175 res <- runExceptT $ rep env str
5400d4bf 176 out <- case res of
6116c2d5 177 Left mv -> return $ "Error: " ++ Printer._pr_str True mv
5400d4bf 178 Right val -> return val
c150ec41 179 putStrLn out
5400d4bf 180 hFlush stdout
c150ec41
JM
181 repl_loop env
182
6116c2d5
NB
183-- Read and evaluate a line. Ignore successful results, but crash in
184-- case of error. This is intended for the startup procedure.
185re :: Env -> String -> IO ()
186re repl_env line = do
187 res <- runExceptT $ eval repl_env =<< mal_read line
188 case res of
189 Left mv -> error $ "Startup failed: " ++ Printer._pr_str True mv
190 Right _ -> return ()
191
192defBuiltIn :: Env -> (String, Fn) -> IO ()
193defBuiltIn env (sym, f) =
194 env_set env sym $ MalFunction {fn=f, f_ast=Nil, f_params=[], macro=False, meta=Nil}
195
196evalFn :: Env -> Fn
197evalFn env [ast] = eval env ast
198evalFn _ _ = throwStr "illegal call of eval"
199
200main :: IO ()
c150ec41
JM
201main = do
202 args <- getArgs
203 load_history
204
6116c2d5 205 repl_env <- env_new []
c150ec41
JM
206
207 -- core.hs: defined using Haskell
6116c2d5
NB
208 mapM_ (defBuiltIn repl_env) Core.ns
209 defBuiltIn repl_env ("eval", evalFn repl_env)
c150ec41
JM
210
211 -- core.mal: defined using the language itself
6116c2d5
NB
212 re repl_env "(def! not (fn* (a) (if a false true)))"
213 re repl_env "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"
214 re repl_env "(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw \"odd number of forms to cond\")) (cons 'cond (rest (rest xs)))))))"
215 re repl_env "(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) `(let* (or_FIXME ~(first xs)) (if or_FIXME or_FIXME (or ~@(rest xs))))))))"
216
217 case args of
218 script : scriptArgs -> do
219 env_set repl_env "*ARGV*" $ toList $ MalString <$> scriptArgs
220 re repl_env $ "(load-file \"" ++ script ++ "\")"
221 [] -> do
222 env_set repl_env "*ARGV*" $ toList []
223 repl_loop repl_env