summaryrefslogtreecommitdiff
path: root/lambda-calcul/haskell/src/Minilang/REPL.hs
diff options
context:
space:
mode:
authorArnaud Bailly <arnaud@pankzsoft.com>2026-03-22 13:16:21 +0100
committerArnaud Bailly <arnaud@pankzsoft.com>2026-03-22 13:16:21 +0100
commit4fed459d89b6b3abcce8028b19af806b48a38bd7 (patch)
treee3bec85996ad03286b261d8f32eb456e35b164e0 /lambda-calcul/haskell/src/Minilang/REPL.hs
parentafb965ca75a106063c36195a1c180baebe610115 (diff)
downloadlambda-nantes-4fed459d89b6b3abcce8028b19af806b48a38bd7.tar.gz
Add builtin arithmetic operators
* also allow choosing evaluator * ensures equivalence of evaluators up-to α-renaming * checks server accepts answers from challenge
Diffstat (limited to 'lambda-calcul/haskell/src/Minilang/REPL.hs')
-rw-r--r--lambda-calcul/haskell/src/Minilang/REPL.hs35
1 files changed, 28 insertions, 7 deletions
diff --git a/lambda-calcul/haskell/src/Minilang/REPL.hs b/lambda-calcul/haskell/src/Minilang/REPL.hs
index a8d3c77..7da2042 100644
--- a/lambda-calcul/haskell/src/Minilang/REPL.hs
+++ b/lambda-calcul/haskell/src/Minilang/REPL.hs
@@ -23,6 +23,8 @@ import Minilang.REPL.Pure
import Minilang.REPL.Purer
import Minilang.REPL.Run
import Minilang.REPL.Types
+import System.Environment (lookupEnv)
+import Text.Read (readMaybe)
import System.Console.Haskeline
( defaultBehavior,
defaultSettings,
@@ -30,13 +32,32 @@ import System.Console.Haskeline
runInputTBehavior,
setComplete,
)
-import System.IO (Handle)
+import System.IO (Handle, stderr)
+
+-- | Read the evaluator selection from the @MINILANG_EVAL@ environment variable.
+-- Returns 'DeBruijn' when the variable is absent or unrecognised.
+evaluatorFromEnv :: IO Evaluator
+evaluatorFromEnv =
+ lookupEnv "MINILANG_EVAL" >>= \case
+ Nothing -> pure DeBruijn
+ Just raw -> case readMaybe raw of
+ Just ev -> pure ev
+ Nothing -> do
+ Text.hPutStrLn stderr $
+ "Warning: unknown MINILANG_EVAL value " <> Text.pack (show raw)
+ <> ", falling back to DeBruijn"
+ pure DeBruijn
+
+-- | Build the initial 'REPLEnv' using the given 'Evaluator'.
+replEnvWith :: Evaluator -> REPLEnv
+replEnvWith ev = initialREPL { evaluator = ev }
-- * Haskeline REPL
-withTerminal :: IO ()
-withTerminal =
- runInputTBehavior defaultBehavior settings $ evalStateT (runHaskeline runREPL) initialREPL
+withTerminal :: Evaluator -> IO ()
+withTerminal ev =
+ runInputTBehavior defaultBehavior settings $
+ evalStateT (runHaskeline runREPL) (replEnvWith ev)
where
settings = setComplete completion $ defaultSettings {historyFile = Just "~/.minilang.history"}
@@ -50,12 +71,12 @@ withHandles hin hout =
evalStateT (runConsole runREPL) (IOEnv hin hout initialREPL)
-- | Simple IO REPL, used to evaluate programs from stdin or file loading
-withIO :: Handle -> Handle -> IO ()
-withIO hin hout = do
+withIO :: Evaluator -> Handle -> Handle -> IO ()
+withIO ev hin hout = do
programText <- parseProgram <$> Text.hGetContents hin
case programText of
Left err -> Text.hPutStrLn hout (Text.pack (show err))
- Right prog -> case loadProgram Untyped newEnv newContext prog of
+ Right prog -> case loadProgram ev Untyped newEnv newContext prog of
Right (results, _, _) -> mapM_ (Text.hPutStrLn hout . render) results
Left err -> Text.hPutStrLn hout (Text.pack $ show err)
where