-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.hs
75 lines (64 loc) · 2.24 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
module Main where
import Control.Monad.IO.Class (MonadIO (liftIO))
import Eval (printVars, EvalState, runEvalM, evalString, insertBuiltins, emptyEvalState)
import System.IO (putStrLn)
import Util (ansiDefault, ansiRed, ansiMagenta, putStrLnErr)
import Data.Function ((&))
import Data.List (intercalate)
import qualified Data.Set as Set
import Core (builtinVals)
import Control.Monad (when)
import System.Environment (getArgs)
redError :: String -> IO ()
redError s = putStrLnErr $ ansiRed ++ "error: " ++ s ++ ansiDefault
-- Print variables that changed value. Compares by string representation so not perfect but good enough.
printChangedValues :: EvalState -> EvalState -> IO ()
printChangedValues state state' = do
(Right (vars, _)) <- runEvalM printVars state
(Right (vars', _)) <- runEvalM printVars state'
let newVars = (Set.fromList vars' Set.\\ Set.fromList vars) & Set.toAscList & intercalate "\n"
when (newVars /= "") $ liftIO $ putStrLn $ ansiMagenta ++ newVars ++ ansiDefault
repl' :: EvalState -> IO ()
repl' state = do
line <- liftIO getLine
case line of
":q" -> return ()
":t" -> do
(Right (vars, _)) <- runEvalM printVars state
liftIO $ putStrLn $ ansiMagenta ++ (vars & intercalate "\n") ++ ansiDefault
repl' state
_ -> do
result <- runEvalM (evalString line) state
case result of
Left err -> do
redError err
repl' state
Right (_, state') -> do
printChangedValues state state'
repl' state'
repl :: () -> IO ()
repl () = do
result <- runEvalM (insertBuiltins builtinVals) emptyEvalState
case result of
Left err -> do
redError $ "during initialization: " ++ err
Right (_, state) -> do
putStrLn $ ansiMagenta ++ "JPPML repl. Type :q to quit, :t to print bindings." ++ ansiDefault
repl' state
doFile :: String -> IO ()
doFile s = do
code <- readFile s
(Right (_, state)) <- runEvalM (insertBuiltins builtinVals) emptyEvalState
result <- runEvalM (evalString code) state
case result of
Left err -> do
putStrLnErr $ "error: " ++ err
Right _ -> do
return ()
main :: IO ()
main = do
args <- getArgs
case args of
[] -> repl ()
[s] -> doFile s
_ -> error "too many arguments"