2024-12-20 11:38:09 -06:00
|
|
|
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
|
2024-12-27 15:40:50 -06:00
|
|
|
putStr "sapling < "
|
2024-12-20 11:38:09 -06:00
|
|
|
hFlush stdout
|
|
|
|
input <- getLine
|
|
|
|
if input == "_:exit"
|
|
|
|
then putStrLn "Goodbye!"
|
2024-12-27 14:10:13 -06:00
|
|
|
else if input == ""
|
2024-12-27 15:40:50 -06:00
|
|
|
then do
|
2024-12-27 14:10:13 -06:00
|
|
|
putStrLn ""
|
|
|
|
repl env
|
2024-12-20 11:38:09 -06:00
|
|
|
else do
|
|
|
|
let clearEnv = Map.delete "__result" env
|
|
|
|
let newEnv = evalSingle clearEnv (parseSingle input)
|
|
|
|
case Map.lookup "__result" newEnv of
|
2024-12-27 15:40:50 -06:00
|
|
|
Just r -> do
|
|
|
|
putStrLn $ "sapling > " ++ show r
|
|
|
|
putStrLn $ "DECODE -: " ++ (decodeResult r)
|
2024-12-20 11:38:09 -06:00
|
|
|
Nothing -> pure ()
|
|
|
|
repl newEnv
|
2024-12-27 15:40:50 -06:00
|
|
|
|
|
|
|
decodeResult :: T -> String
|
|
|
|
decodeResult tc =
|
|
|
|
case ofString tc of
|
|
|
|
Right str -> "\"" ++ str ++ "\""
|
|
|
|
Left _ -> case ofNumber tc of
|
2024-12-27 16:09:54 -06:00
|
|
|
Right num -> "# " ++ show num
|
2024-12-27 15:40:50 -06:00
|
|
|
Left _ -> "Failed to decode number from Tree"
|