diff options
-rw-r--r-- | bin/Main.hs | 23 |
1 files changed, 18 insertions, 5 deletions
diff --git a/bin/Main.hs b/bin/Main.hs index 2942566..591fc1e 100644 --- a/bin/Main.hs +++ b/bin/Main.hs | |||
@@ -1,15 +1,21 @@ | |||
1 | module Main where | 1 | module Main where |
2 | 2 | ||
3 | import Control.Monad (liftM) | ||
4 | import Control.Monad.Except (throwError) | ||
5 | import Error (LispError (..), LispResult (..), | ||
6 | unwrap) | ||
3 | import Evaluator (eval) | 7 | import Evaluator (eval) |
4 | import Parser (Expr (..), parseLispValue) | 8 | import Parser (Expr (..), parseLispValue) |
5 | import System.Console.Readline | 9 | import System.Console.Readline |
10 | import System.Environment (getArgs) | ||
6 | import Text.ParserCombinators.Parsec | 11 | import Text.ParserCombinators.Parsec |
7 | 12 | ||
8 | readExpr :: String -> Expr | 13 | readExpr :: String -> LispResult Expr |
9 | readExpr inp = | 14 | readExpr inp = |
10 | case parse parseLispValue "(unknown)" inp of | 15 | case parse parseLispValue "(unknown)" inp of |
11 | Left err -> StringLiteral $ show err | 16 | Left err -> throwError $ Parse err |
12 | Right val -> val | 17 | Right val -> return val |
18 | |||
13 | 19 | ||
14 | repl :: IO () | 20 | repl :: IO () |
15 | repl = do | 21 | repl = do |
@@ -19,8 +25,15 @@ repl = do | |||
19 | Just ",q" -> return () | 25 | Just ",q" -> return () |
20 | Just line -> do | 26 | Just line -> do |
21 | addHistory line | 27 | addHistory line |
22 | print . eval . readExpr $ line | 28 | -- TODO: don't directly print Either values |
29 | print $ eval =<< readExpr line | ||
23 | repl | 30 | repl |
24 | 31 | ||
25 | main :: IO () | 32 | main :: IO () |
26 | main = repl | 33 | main = do |
34 | args <- getArgs | ||
35 | if null args | ||
36 | then do | ||
37 | print ";;; Entering lisk repl ..." | ||
38 | repl | ||
39 | else print $ eval =<< readExpr (head args) | ||