From 370847f0326e1cdf848d674b7cfd89565f3e8757 Mon Sep 17 00:00:00 2001
From: Jed Barber <jjbarber@y7mail.com>
Date: Fri, 14 Mar 2014 01:58:00 +1100
Subject: Added wrapper command line program for unlambda interpreter

---
 makefile    |  7 +++++--
 unlambda.hs | 30 ++++++++++++++++++++++++++++++
 2 files changed, 35 insertions(+), 2 deletions(-)
 create mode 100644 unlambda.hs

diff --git a/makefile b/makefile
index b702844..4388627 100644
--- a/makefile
+++ b/makefile
@@ -1,11 +1,11 @@
 
 OUTPUTDIR = bin
 
-EXECUTABLES = ${OUTPUTDIR}/test ${OUTPUTDIR}/fractran ${OUTPUTDIR}/thue
+EXECUTABLES = ${OUTPUTDIR}/test ${OUTPUTDIR}/fractran ${OUTPUTDIR}/thue ${OUTPUTDIR}/unlambda
 
 
 
-all: testprog fractranprog thueprog
+all: testprog fractranprog thueprog unlambdaprog
 
 
 clean:
@@ -26,3 +26,6 @@ fractranprog:
 thueprog:
 	ghc --make thue.hs -o ${OUTPUTDIR}/thue
 
+unlambdaprog:
+	ghc -XDeriveDataTypeable --make unlambda.hs -o ${OUTPUTDIR}/unlambda
+
diff --git a/unlambda.hs b/unlambda.hs
new file mode 100644
index 0000000..b06b42f
--- /dev/null
+++ b/unlambda.hs
@@ -0,0 +1,30 @@
+
+import System.Environment( getArgs )
+import Control.Exception( ErrorCall(..), Handler(..), catches )
+import Unlambda.Parser
+import Unlambda.Interpreter
+
+
+
+
+usageString :: String
+usageString = "Usage: unlambda <program file>"
+
+
+
+program :: IO ()
+program = do
+    args <- getArgs
+    fileContents <- if (length args /= 1)
+                    then error usageString
+                    else readFile (head args)
+
+    case (parseUnlambda fileContents) of
+        Left x -> putStrLn (show x)
+        Right x -> unlambda x >>= putStrLn . show
+
+
+
+main = catches program
+               [ Handler ((\e -> putStrLn . show $ e) :: ErrorCall -> IO ()) ]
+
-- 
cgit