aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-10-09 07:06:10 +0100
committerAkshay <[email protected]>2020-10-09 07:06:10 +0100
commitc785a95f14f8bb3887cdc411ef3329533a2c819a (patch)
tree4b6dac72ee004fbc1dfe4c61aec47b9c16901158
parent06bf4c656377572859846767cb9af5db8fc27893 (diff)
allow eval without entering repl
-rw-r--r--bin/Main.hs23
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 @@
1module Main where 1module Main where
2 2
3import Control.Monad (liftM)
4import Control.Monad.Except (throwError)
5import Error (LispError (..), LispResult (..),
6 unwrap)
3import Evaluator (eval) 7import Evaluator (eval)
4import Parser (Expr (..), parseLispValue) 8import Parser (Expr (..), parseLispValue)
5import System.Console.Readline 9import System.Console.Readline
10import System.Environment (getArgs)
6import Text.ParserCombinators.Parsec 11import Text.ParserCombinators.Parsec
7 12
8readExpr :: String -> Expr 13readExpr :: String -> LispResult Expr
9readExpr inp = 14readExpr 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
14repl :: IO () 20repl :: IO ()
15repl = do 21repl = 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
25main :: IO () 32main :: IO ()
26main = repl 33main = 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)