tricu/src/REPL.hs

43 lines
1.2 KiB
Haskell
Raw Normal View History

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
2024-12-29 08:29:25 -06:00
minput <- getInputLine "tricu < "
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
2024-12-29 08:29:25 -06:00
outputStrLn $ "tricu > " ++ show r
outputStrLn $ "DECODE -: " ++ decodeResult r
Nothing -> return ()
loop newEnv
decodeResult :: T -> String
2024-12-28 07:24:19 -06:00
decodeResult tc = case toNumber tc of
Right num -> show num
2024-12-28 07:24:19 -06:00
Left _ -> case toString tc of
Right str -> "\"" ++ str ++ "\""
2024-12-28 07:24:19 -06:00
Left _ -> case toList tc of
Right list -> "[" ++ intercalate ", " (map decodeResult list) ++ "]"
Left _ -> ""