From c3a175e808b279eb415bcb5dbcb9db5f34035f98 Mon Sep 17 00:00:00 2001 From: Arnaud Bailly Date: Fri, 17 Oct 2025 09:52:06 +0200 Subject: feat: add basic application to evaluate "programs" The main simply reads from its stdin, evaluates its input, and dump the result of the evaluation. --- lambda-calcul/haskell/test/Minilang/IOSpec.hs | 37 +++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 lambda-calcul/haskell/test/Minilang/IOSpec.hs (limited to 'lambda-calcul/haskell/test/Minilang/IOSpec.hs') diff --git a/lambda-calcul/haskell/test/Minilang/IOSpec.hs b/lambda-calcul/haskell/test/Minilang/IOSpec.hs new file mode 100644 index 0000000..038877d --- /dev/null +++ b/lambda-calcul/haskell/test/Minilang/IOSpec.hs @@ -0,0 +1,37 @@ +module Minilang.IOSpec where + +import Control.Exception (bracket) +import Minilang.IO (runEval) +import System.Directory (getTemporaryDirectory, removeFile) +import System.FilePath ((<.>), ()) +import System.IO (IOMode (..), hClose, readFile, withFile, writeFile) +import System.Posix (mkstemp) +import Test.Hspec (Spec, around, describe, it, parallel, shouldBe) +import Prelude hiding (lines, readFile, writeFile) + +spec :: Spec +spec = parallel $ + describe "MiniLang I/O Evaluator" $ do + around withTempFile $ + it "evaluates a simple MiniLang 'program' and dumps result" $ \fileName -> do + let outputFileName = fileName <> ".out" + program = + [ "(App (Lam \"x\" (Var \"x\")) (Var \"y\"))" + ] + + writeFile fileName (unlines program) + + _ <- withFile fileName ReadMode $ \hin -> + withFile outputFileName AppendMode $ \hout -> + runEval hin hout + + out <- readFile outputFileName + + out `shouldBe` "Var \"y\"\n" + +withTempFile :: (String -> IO ()) -> IO () +withTempFile = + bracket mkTempFile rmTempFile + where + mkTempFile = getTemporaryDirectory >>= \dir -> mkstemp (dir "test-repl") >>= \(fp, h) -> hClose h >> pure fp + rmTempFile fn = removeFile fn >> removeFile (fn <.> "out") -- cgit v1.2.3