From: pimjager Date: Fri, 26 Feb 2016 13:36:00 +0000 (+0100) Subject: nu met gratis de library waar de borige commit over ging X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=89c1b0d0eb0851e93ae19ab934033af3ec50518f;p=cc1516.git nu met gratis de library waar de borige commit over ging --- diff --git a/yard.dcl b/yard.dcl new file mode 100644 index 0000000..9f40539 --- /dev/null +++ b/yard.dcl @@ -0,0 +1,23 @@ +definition module yard + +from Data.Either import :: Either +from StdClass import class Eq +from Data.Functor import class Functor +from Control.Monad import class Monad +from Control.Applicative import class Applicative, class Alternative + +:: Parser a b = Parser ([a] -> (Either Error b, [a])) +:: Error = ParseException + +runParser :: (Parser a b) [a] -> (Either Error b, [a]) + +instance Functor (Parser a) +instance Applicative (Parser a) +instance Monad (Parser a) +instance Alternative (Parser a) + +fail :: Parser a b +top :: Parser a a +satisfy :: (a -> Bool) -> Parser a a +item :: a -> Parser a a | Eq a +list :: [a] -> Parser a [a] | Eq a \ No newline at end of file diff --git a/yard.icl b/yard.icl new file mode 100644 index 0000000..59b4e8c --- /dev/null +++ b/yard.icl @@ -0,0 +1,53 @@ +implementation module yard + +import StdTuple +from StdClass import Eq +import Data.Functor +import Control.Monad +import Control.Applicative +from Data.Func import $ + +runParser :: (Parser a b) [a] -> (Either Error b, [a]) +runParser (Parser f) i = f i + +instance Functor (Parser a) where + //fmap f m = liftM f m + fmap g p = Parser \i -> case runParser p i of + (Right r, rest) = (Right $ g r, rest) + (Left e, rest) = (Left e, rest) + +instance Applicative (Parser a) where + pure a = Parser \i -> (Right a, i) + //(<*>) sf p = ap sf p + (<*>) pf p = Parser \i -> case runParser pf i of + (Right f, rest) = runParser (fmap f p) rest + (Left e, rest) = (Left e, rest) + +instance Monad (Parser a) where + bind p f = Parser \i -> case runParser p i of + (Right r, rest) = runParser (f r) rest + (Left e, rest) = (Left e, rest) + +//some, many, optional and l +instance Alternative (Parser a) where + empty = Parser \i -> (Left ParseException, i) + (<|>) p1 p2 = Parser \i -> case runParser p1 i of + (Right r, rest) = (Right r, rest) + (Left _, rest) = runParser p2 i + +fail :: Parser a b +fail = empty + +top :: Parser a a +top = Parser \i -> case i of + [] = (Left ParseException, []) + [x:xs] = (Right x, xs) + +satisfy :: (a -> Bool) -> Parser a a +satisfy f = one >>= \r -> if (f r) (return r) fail + +item :: a -> Parser a a | Eq a +item a = satisfy ((==)a) + +list :: [a] -> Parser a [a] | Eq a +list as = mapM item as \ No newline at end of file