diff --git a/example.lambda b/example.lambda deleted file mode 100644 index f79d9f3..0000000 --- a/example.lambda +++ /dev/null @@ -1 +0,0 @@ -((λ x (λ y x)) "Hello, world!") diff --git a/examples/church.lambda b/examples/church.lambda new file mode 100644 index 0000000..d4b19fb --- /dev/null +++ b/examples/church.lambda @@ -0,0 +1,6 @@ +zero = (λ f (λ x x)) +one = (λ f (λ x (f x))) +two = (λ f (λ x (f (f x)))) +three = (λ f (λ x (f ((two f) x)))) +const = (λ x (λ y x)) +main = (three const) "whoa" \ No newline at end of file diff --git a/examples/def.lambda b/examples/def.lambda new file mode 100644 index 0000000..91c0918 --- /dev/null +++ b/examples/def.lambda @@ -0,0 +1,3 @@ +const = (λ x (λ y x)) +id = (λ x x) +main = (((const id) const) (λ y (λ x (λ z y)))) \ No newline at end of file diff --git a/examples/helloworld.lambda b/examples/helloworld.lambda new file mode 100644 index 0000000..0280b9c --- /dev/null +++ b/examples/helloworld.lambda @@ -0,0 +1 @@ +((λ x (λ y x)) "Hello, world!") \ No newline at end of file diff --git a/src/AST.hs b/src/AST.hs new file mode 100644 index 0000000..8af97b4 --- /dev/null +++ b/src/AST.hs @@ -0,0 +1,41 @@ +{-# OPTIONS_GHC -fno-warn-unused-do-bind #-} + +module AST where + +import Control.Applicative (Alternative, some) +import Control.Monad (guard) + +import Lib (Expr, Identifier, Error, betaReduce, compile) +import Parser (Parser, char, identifier, expr) + +type Definition = (Identifier, Expr) + +data AST = AST + { defs :: [Definition] + , mainExpr :: Expr + } deriving (Show) + +definition :: (Alternative m, Monad m) => Parser String m Definition +definition = do + name <- identifier + char ' ' + char '=' + char ' ' + ex <- expr + pure (name, ex) + +ast :: (Alternative m, Monad m) => Parser String m AST +ast = AST <$> some p <*> mainP + where + p = do + def@(name, _) <- definition + guard $ name /= "main" + char '\n' + pure def + mainP = do + (name, ex) <- definition + guard $ name == "main" + pure ex + +compileAst :: AST -> Either Error String +compileAst (AST definitions mainEx) = betaReduce definitions mainEx >>= compile \ No newline at end of file