aboutsummaryrefslogtreecommitdiff
path: root/bin/Main.hs
blob: 6207e02c99cabde3377d974eec4bb81fd188aa1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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 -> LispResult Expr
readExpr inp =
    case parse parseLispValue "(unknown)" inp of
      Left err  -> throwError $ Parse err
      Right val -> return val


repl :: IO ()
repl = do
    inp <- readline "(lisk)> "
    case inp of
      Nothing -> return ()
      Just ",q" -> return ()
      Just line -> do
          addHistory line
          -- TODO: don't directly print Either values
          print $ eval =<< readExpr line
          repl

main :: IO ()
main = do
    args <- getArgs
    if null args
       then do
           putStrLn ";;; Entering lisk repl ..."
           repl
       else print $ eval =<< readExpr (head args)