module REPL where import Eval import Lexer import Parser import Research import Data.List (intercalate) import qualified Data.Map as Map import System.Console.Haskeline import System.IO (hFlush, stdout) repl :: Map.Map String T -> IO () repl env = runInputT defaultSettings (loop env) where loop :: Map.Map String T -> InputT IO () loop env = do minput <- getInputLine "sapling < " case minput of Nothing -> outputStrLn "Goodbye!" Just ":_exit" -> outputStrLn "Goodbye!" Just "" -> do outputStrLn "" loop env Just input -> do let clearEnv = Map.delete "__result" env newEnv = evalSingle clearEnv (parseSingle input) case Map.lookup "__result" newEnv of Just r -> do outputStrLn $ "sapling > " ++ show r outputStrLn $ "DECODE -: " ++ decodeResult r Nothing -> return () loop newEnv decodeResult :: T -> String decodeResult tc = case toNumber tc of Right num -> show num Left _ -> case toString tc of Right str -> "\"" ++ str ++ "\"" Left _ -> case toList tc of Right list -> "[" ++ intercalate ", " (map decodeResult list) ++ "]" Left _ -> ""