2024-12-20 11:38:09 -06:00
|
|
|
module REPL where
|
|
|
|
|
|
|
|
import Eval
|
2025-01-01 18:05:21 -06:00
|
|
|
import FileEval
|
2024-12-20 11:38:09 -06:00
|
|
|
import Lexer
|
|
|
|
import Parser
|
|
|
|
import Research
|
|
|
|
|
2024-12-29 20:29:41 -06:00
|
|
|
import Control.Exception (SomeException, catch)
|
|
|
|
import Control.Monad.IO.Class (liftIO)
|
2025-01-02 19:08:14 -06:00
|
|
|
import Data.Char (isSpace)
|
|
|
|
import Data.List (dropWhile, dropWhileEnd, intercalate)
|
2024-12-27 20:46:30 -06:00
|
|
|
import System.Console.Haskeline
|
2024-12-29 20:29:41 -06:00
|
|
|
|
|
|
|
import qualified Data.Map as Map
|
2024-12-20 11:38:09 -06:00
|
|
|
|
2024-12-29 21:49:57 -06:00
|
|
|
repl :: Env -> IO ()
|
2024-12-27 20:46:30 -06:00
|
|
|
repl env = runInputT defaultSettings (loop env)
|
|
|
|
where
|
2024-12-29 21:49:57 -06:00
|
|
|
loop :: Env -> InputT IO ()
|
2024-12-27 20:46:30 -06:00
|
|
|
loop env = do
|
2024-12-29 08:29:25 -06:00
|
|
|
minput <- getInputLine "tricu < "
|
2024-12-27 20:46:30 -06:00
|
|
|
case minput of
|
2025-01-02 19:08:14 -06:00
|
|
|
Nothing -> outputStrLn "Exiting tricu"
|
|
|
|
Just s -> case strip s of
|
|
|
|
"!exit" -> outputStrLn "Exiting tricu"
|
|
|
|
"!load" -> do
|
|
|
|
path <- getInputLine "File path to load < "
|
|
|
|
case path of
|
|
|
|
Nothing -> do
|
|
|
|
outputStrLn "No input received; stopping import."
|
|
|
|
loop env
|
|
|
|
Just path -> do
|
|
|
|
loadedEnv <- liftIO $ evaluateFileWithContext env (strip path)
|
|
|
|
loop $ Map.delete "__result" (Map.union loadedEnv env)
|
|
|
|
"" -> do
|
|
|
|
outputStrLn ""
|
|
|
|
loop env
|
|
|
|
input -> do
|
|
|
|
case (take 2 input) of
|
|
|
|
"--" -> loop env
|
|
|
|
_ -> do
|
|
|
|
newEnv <- liftIO $ (processInput env input `catch` errorHandler env)
|
|
|
|
loop newEnv
|
2024-12-30 14:19:43 -06:00
|
|
|
|
|
|
|
processInput :: Env -> String -> IO Env
|
2024-12-29 20:29:41 -06:00
|
|
|
processInput env input = do
|
2024-12-30 14:19:43 -06:00
|
|
|
let asts = parseTricu input
|
|
|
|
newEnv = evalTricu env asts
|
2024-12-29 20:29:41 -06:00
|
|
|
case Map.lookup "__result" newEnv of
|
|
|
|
Just r -> do
|
2025-01-02 19:08:14 -06:00
|
|
|
putStrLn $ "tricu > " ++ decodeResult r
|
2024-12-29 20:29:41 -06:00
|
|
|
Nothing -> return ()
|
|
|
|
return newEnv
|
2024-12-30 14:19:43 -06:00
|
|
|
|
2024-12-29 21:49:57 -06:00
|
|
|
errorHandler :: Env -> SomeException -> IO (Env)
|
2024-12-29 20:29:41 -06:00
|
|
|
errorHandler env e = do
|
|
|
|
putStrLn $ "Error: " ++ show e
|
|
|
|
return env
|
2025-01-02 19:08:14 -06:00
|
|
|
|
|
|
|
strip :: String -> String
|
|
|
|
strip = dropWhileEnd isSpace . dropWhile isSpace
|
2024-12-29 20:29:41 -06:00
|
|
|
|
2024-12-27 15:40:50 -06:00
|
|
|
decodeResult :: T -> String
|
2024-12-28 07:24:19 -06:00
|
|
|
decodeResult tc = case toNumber tc of
|
2024-12-27 20:46:30 -06:00
|
|
|
Right num -> show num
|
2024-12-28 07:24:19 -06:00
|
|
|
Left _ -> case toString tc of
|
2025-01-02 19:08:14 -06:00
|
|
|
Right str -> "\"" ++ str ++ "\""
|
|
|
|
Left _ -> formatResult TreeCalculus tc
|