aboutsummaryrefslogtreecommitdiff
path: root/haskell/step2_eval.hs
diff options
context:
space:
mode:
authorJoel Martin <github@martintribe.org>2014-12-23 22:37:43 -0700
committerJoel Martin <github@martintribe.org>2015-01-09 16:16:52 -0600
commitfa9a9758e0d15abe670fbbfd8efa1fce013b1414 (patch)
tree7ee5dceceaa79cf0269ae74d10aa297504c69da3 /haskell/step2_eval.hs
parentb76aa73bc76a28d7c6bb3c5a43acc9afd9ec42c8 (diff)
downloadmal-fa9a9758e0d15abe670fbbfd8efa1fce013b1414.tar.gz
mal-fa9a9758e0d15abe670fbbfd8efa1fce013b1414.zip
Haskell: steps 4-6. Line editing. Simpler fn calls.
Diffstat (limited to 'haskell/step2_eval.hs')
-rw-r--r--haskell/step2_eval.hs47
1 files changed, 23 insertions, 24 deletions
diff --git a/haskell/step2_eval.hs b/haskell/step2_eval.hs
index 0a95fcc..2fba218 100644
--- a/haskell/step2_eval.hs
+++ b/haskell/step2_eval.hs
@@ -1,9 +1,9 @@
-import System.IO (hGetLine, hFlush, hIsEOF, stdin, stdout)
import Control.Monad (when, mapM)
import Control.Monad.Error (throwError)
import qualified Data.Map as Map
import qualified Data.Traversable as DT
+import Readline (readline, load_history)
import Types
import Reader (read_str)
import Printer (_pr_str)
@@ -33,8 +33,8 @@ apply_ast :: MalVal -> (Map.Map String MalVal) -> IO MalVal
apply_ast ast@(MalList _) env = do
el <- eval_ast ast env
case el of
- (MalList (MalFunc (FuncT f) : rest)) ->
- return $ f $ MalList rest
+ (MalList (Func (Fn f) : rest)) ->
+ f $ rest
el ->
error $ "invalid apply: " ++ (show el)
@@ -51,23 +51,23 @@ mal_print exp = show exp
-- repl
add args = case args of
- (MalList [MalNumber a, MalNumber b]) -> MalNumber $ a + b
+ [MalNumber a, MalNumber b] -> return $ MalNumber $ a + b
_ -> error $ "illegal arguments to +"
sub args = case args of
- (MalList [MalNumber a, MalNumber b]) -> MalNumber $ a - b
+ [MalNumber a, MalNumber b] -> return $ MalNumber $ a - b
_ -> error $ "illegal arguments to -"
mult args = case args of
- (MalList [MalNumber a, MalNumber b]) -> MalNumber $ a * b
+ [MalNumber a, MalNumber b] -> return $ MalNumber $ a * b
_ -> error $ "illegal arguments to *"
divd args = case args of
- (MalList [MalNumber a, MalNumber b]) -> MalNumber $ a `div` b
+ [MalNumber a, MalNumber b] -> return $ MalNumber $ a `div` b
_ -> error $ "illegal arguments to /"
repl_env :: Map.Map String MalVal
-repl_env = Map.fromList [("+", _malfunc add),
- ("-", _malfunc sub),
- ("*", _malfunc mult),
- ("/", _malfunc divd)]
+repl_env = Map.fromList [("+", _func add),
+ ("-", _func sub),
+ ("*", _func mult),
+ ("/", _func divd)]
rep :: String -> IO String
rep line = do
@@ -77,17 +77,16 @@ rep line = do
repl_loop :: IO ()
repl_loop = do
- putStr "user> "
- hFlush stdout
- ineof <- hIsEOF stdin
- when (not ineof) $ do
- line <- hGetLine stdin
- if null line
- then repl_loop
- else do
- out <- catchAny (rep line) $ \e -> do
- return $ "Error: " ++ (show e)
- putStrLn out
- repl_loop
+ line <- readline "user> "
+ case line of
+ Nothing -> return ()
+ Just "" -> repl_loop
+ Just str -> do
+ out <- catchAny (rep str) $ \e -> do
+ return $ "Error: " ++ (show e)
+ putStrLn out
+ repl_loop
-main = repl_loop
+main = do
+ load_history
+ repl_loop