From 7e70b177b76d632dc2975e17e14321e8bc3387d8 Mon Sep 17 00:00:00 2001 From: Jed Barber Date: Sun, 9 Mar 2014 23:35:26 +1100 Subject: Added Unlambda interpreter, currently without support for C,E,@,|,? --- Unlambda/Interpreter.hs | 68 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 Unlambda/Interpreter.hs (limited to 'Unlambda') diff --git a/Unlambda/Interpreter.hs b/Unlambda/Interpreter.hs new file mode 100644 index 0000000..170b2a7 --- /dev/null +++ b/Unlambda/Interpreter.hs @@ -0,0 +1,68 @@ +module Unlambda.Interpreter ( + unlambda + ) where + + +import Unlambda.Parser + + + + +unlambda :: UnlambdaTerm -> IO UnlambdaTerm +unlambda term = eval term + + + +eval :: UnlambdaTerm -> IO UnlambdaTerm +eval term = + case term of + App f x -> do + t <- eval f + apply t x + _ -> return term + + + +apply :: UnlambdaTerm -> UnlambdaTerm -> IO UnlambdaTerm +apply firstTerm secondTerm = + case firstTerm of + K -> eval secondTerm >>= return . Kpartial + + Kpartial x -> eval secondTerm >> return x + + S -> eval secondTerm >>= return . Spartial + + Spartial x -> eval secondTerm >>= return . (Sapp x) + + Sapp x y -> do + z <- eval secondTerm + result <- eval (App (App x z) (App y z)) + return result + + I -> eval secondTerm + + V -> eval secondTerm >> return V + + C -> return I --placeholder + + D -> return (Promise secondTerm) + + Promise x -> do + y <- eval secondTerm + result <- eval (App x y) + return result + + Dot c -> putChar c >> eval secondTerm + + R -> putChar '\n' >> eval secondTerm + + E -> return I --placeholder + + Reed -> return I --placeholder + + Bar -> return I --placeholder + + Compare c -> return I --placeholder + + Continuation cont -> return I --placeholder + -- cgit