aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Eval.hs
blob: 72c89850d77eac66b19043c664de15aaaeb866c0 (plain) (blame)
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
42
43
44
45
module Eval
  ( evalMain
  ) where

import           Control.Exception
import           Control.Monad.State
import           System.Console.Haskeline
import           System.Environment
import           System.Exit
import           System.IO

type Environment = [String]

eval :: String -> IO ()
eval code = putStrLn "ok"

evalFile :: String -> IO ()
evalFile path = do
  file <- try $ readFile path :: IO (Either IOError String)
  case file of
    Left  exception -> print (exception :: IOError)
    Right file      -> eval file

evalRepl :: String -> Environment -> InputT IO Environment
evalRepl line env = outputStrLn (show env) >> pure env

repl :: Environment -> InputT IO ()
repl env =
  getInputLine ":: "
    >>= (\case
          Nothing   -> pure ()
          Just line -> evalRepl line env >>= repl
        )

usage :: IO ()
usage = putStrLn "Invalid arguments. Use 'bruijn [file]' instead"

evalMain :: IO ()
evalMain = do
  args <- getArgs
  case args of
    [] -> runInputT defaultSettings { historyFile = Just ".brown-history" }
      $ repl []
    [path] -> evalFile path
    _      -> usage