40 lines
929 B
Haskell
40 lines
929 B
Haskell
module REPL where
|
|
|
|
import Eval
|
|
import Lexer
|
|
import Parser
|
|
import Research
|
|
|
|
import Control.Monad (void)
|
|
import qualified Data.Map as Map
|
|
import System.IO (hFlush, stdout)
|
|
|
|
repl :: Map.Map String T -> IO ()
|
|
repl env = do
|
|
putStr "sapling < "
|
|
hFlush stdout
|
|
input <- getLine
|
|
case input of
|
|
":_exit" ->
|
|
putStrLn "Goodbye!"
|
|
"" -> do
|
|
putStrLn ""
|
|
repl env
|
|
_ -> do
|
|
let clearEnv = Map.delete "__result" env
|
|
newEnv = evalSingle clearEnv (parseSingle input)
|
|
case Map.lookup "__result" newEnv of
|
|
Just r -> do
|
|
putStrLn $ "sapling > " ++ show r
|
|
putStrLn $ "DECODE -: " ++ (decodeResult r)
|
|
Nothing -> pure ()
|
|
repl newEnv
|
|
|
|
decodeResult :: T -> String
|
|
decodeResult tc =
|
|
case ofString tc of
|
|
Right str -> "\"" ++ str ++ "\""
|
|
Left _ -> case ofNumber tc of
|
|
Right num -> "# " ++ show num
|
|
Left _ -> ""
|