aboutsummaryrefslogtreecommitdiff
path: root/bin/Main.hs
blob: 54ed6b2645819e84d7d1d6468e0208f4ee1a6555 (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
40
41
module Main where

import           Control.Monad                 (liftM)
import           Control.Monad.Except          (throwError)
import           Error.Base                    (LispError (..), LispResult (..),
                                                unwrap)
import           Error.Pretty                  (defaults, showError)
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 "(lisk-repl)" inp of
      Left err  -> throwError $ Parse err
      Right val -> return val


repl :: IO ()
repl = do
    -- \u2020 † - obelisk
    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

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