-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.hs
48 lines (38 loc) · 1.17 KB
/
Main.hs
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
46
47
48
{-# LANGUAGE LambdaCase #-}
module Main where
import Control.Monad.Trans
import System.IO
import System.Environment
import System.Console.Haskeline
import Ylang.Syntax
import Ylang.Parser
import Ylang.Value
import Ylang.Eval
import Ylang.Display as D
main :: IO ()
main = getArgs >>= \case
[] -> repl
[fname] -> processFile fname >> return ()
repl :: IO ()
repl = runInputT defaultSettings $ loop initEnv
where
loop env = getInputLine "ylsh> " >>= \case
Nothing -> outputStrLn "Goodbye."
Just input -> liftIO (process env input) >>= \case
Just newEnv -> loop newEnv
Nothing -> loop env
initEnv :: Env1
initEnv = defaultEnv1
process :: Env1 -> String -> IO (Maybe Env1)
process env line = case parseTopLevel line of
Left err -> print err >> return Nothing
Right exprs -> evaluate env exprs >>= return . Just
processFile :: String -> IO (Maybe Env1)
processFile fname = readFile fname >>= process initEnv
evaluate :: Env1 -> [Expr] -> IO Env1
evaluate env exs = do
let exec = runEval1 env $ mapM eval1 exs
getResult1 exec >>= \case
Left err -> liftIO $ print err
Right val -> liftIO $ mapM_ D.display val
getEnv1 exec