Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/ast and defs #36

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion example.lambda

This file was deleted.

6 changes: 6 additions & 0 deletions examples/church.lambda
Original file line number Diff line number Diff line change
@@ -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"
3 changes: 3 additions & 0 deletions examples/def.lambda
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const = (λ x (λ y x))
id = (λ x x)
main = (((const id) const) (λ y (λ x (λ z y))))
1 change: 1 addition & 0 deletions examples/helloworld.lambda
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
((λ x (λ y x)) "Hello, world!")
41 changes: 41 additions & 0 deletions src/AST.hs
Original file line number Diff line number Diff line change
@@ -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