From c785a95f14f8bb3887cdc411ef3329533a2c819a Mon Sep 17 00:00:00 2001 From: Akshay Date: Fri, 9 Oct 2020 11:36:10 +0530 Subject: allow eval without entering repl --- bin/Main.hs | 23 ++++++++++++++++++----- 1 file 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 @@ module Main where +import Control.Monad (liftM) +import Control.Monad.Except (throwError) +import Error (LispError (..), LispResult (..), + unwrap) import Evaluator (eval) import Parser (Expr (..), parseLispValue) import System.Console.Readline +import System.Environment (getArgs) import Text.ParserCombinators.Parsec -readExpr :: String -> Expr +readExpr :: String -> LispResult Expr readExpr inp = case parse parseLispValue "(unknown)" inp of - Left err -> StringLiteral $ show err - Right val -> val + Left err -> throwError $ Parse err + Right val -> return val + repl :: IO () repl = do @@ -19,8 +25,15 @@ repl = do Just ",q" -> return () Just line -> do addHistory line - print . eval . readExpr $ line + -- TODO: don't directly print Either values + print $ eval =<< readExpr line repl main :: IO () -main = repl +main = do + args <- getArgs + if null args + then do + print ";;; Entering lisk repl ..." + repl + else print $ eval =<< readExpr (head args) -- cgit v1.2.3