summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJed Barber <jjbarber@y7mail.com>2014-03-08 00:23:28 +1100
committerJed Barber <jjbarber@y7mail.com>2014-03-08 00:23:28 +1100
commitd214e0cfee802e9eba8c925af54490fd782e2d5c (patch)
tree4617ae5d898f39c049e43bb01b267d24d6cb948f
parente25b703805a6d5b20ddcb70e406830ca8d16cd22 (diff)
Added Unlambda parser
-rw-r--r--Unlambda/Parser.hs71
1 files changed, 71 insertions, 0 deletions
diff --git a/Unlambda/Parser.hs b/Unlambda/Parser.hs
new file mode 100644
index 0000000..b6afbea
--- /dev/null
+++ b/Unlambda/Parser.hs
@@ -0,0 +1,71 @@
+module Unlambda.Parser (
+ UnlambdaTerm(..),
+
+ parseUnlambda
+ ) where
+
+
+import Control.Applicative( some )
+import Text.ParserCombinators.Parsec
+
+
+
+data UnlambdaTerm = S | K | I | V | R | D | C
+ | Dot { cha :: Char }
+ | App { func :: UnlambdaTerm
+ , arg :: UnlambdaTerm }
+ deriving (Eq, Show)
+
+
+
+
+parseUnlambda :: String -> Either ParseError UnlambdaTerm
+parseUnlambda = parse unlambda "error"
+
+
+
+unlambda = do
+ whiteSpace
+ t <- term
+ eof
+ return t
+
+
+term = (try app)
+ <|> (try s)
+ <|> (try k)
+ <|> (try i)
+ <|> (try v)
+ <|> (try r)
+ <|> (try d)
+ <|> (try c)
+ <|> (try dot)
+ <?> "unlambda term"
+
+
+app = do
+ char '`'
+ whiteSpace
+ f <- term
+ x <- term
+ return (App f x)
+
+
+s = char 's' >> whiteSpace >> return S
+k = char 'k' >> whiteSpace >> return K
+i = char 'i' >> whiteSpace >> return I
+v = char 'v' >> whiteSpace >> return V
+r = char 'r' >> whiteSpace >> return R
+d = char 'd' >> whiteSpace >> return D
+c = char 'c' >> whiteSpace >> return C
+
+
+dot = do
+ char '.'
+ c <- noneOf("")
+ whiteSpace
+ return (Dot c)
+
+
+whiteSpace = many (oneOf "\t\n\r ")
+