From b19a4a35db4cd951c52e179f3340518c9e2dcc1e Mon Sep 17 00:00:00 2001 From: Akshay Date: Fri, 16 Oct 2020 18:54:46 +0530 Subject: add initial support for variable definition --- bin/Main.hs | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) (limited to 'bin') diff --git a/bin/Main.hs b/bin/Main.hs index 54ed6b2..56ad0cd 100644 --- a/bin/Main.hs +++ b/bin/Main.hs @@ -1,7 +1,8 @@ module Main where import Control.Monad (liftM) -import Control.Monad.Except (throwError) +import Control.Monad.Except (liftIO, runExceptT, throwError) +import Environment import Error.Base (LispError (..), LispResult (..), unwrap) import Error.Pretty (defaults, showError) @@ -17,25 +18,31 @@ readExpr inp = Left err -> throwError $ Parse err Right val -> return val +evalExpr :: Env -> String -> IO (LispResult String) +evalExpr env inp = runExceptT $ fmap show $ + (liftLispResult $ readExpr inp) >>= eval env -repl :: IO () -repl = do - -- \u2020 † - obelisk +repl :: Env -> IO () +repl env = do + let pp = showError defaults "(lisk-repl)" inp <- readline "† " case inp of Nothing -> return () Just ",q" -> return () - Just line -> do - addHistory line - let pp = showError defaults - either (putStrLn . pp line) print $ readExpr line >>= eval - repl + Just i -> do + out <- evalExpr env i + either (putStrLn . pp) putStrLn out + repl env + main :: IO () main = do args <- getArgs + initEnv <- newEnv if null args then do putStrLn ";;; Entering lisk repl ..." - repl - else print $ eval =<< readExpr (head args) + repl initEnv + else do + let pp = showError defaults "(lisk-repl)" + evalExpr initEnv (head args) >>= (either (putStrLn . pp) print) -- cgit v1.2.3