Commit 554f532f authored by Job Cuppen's avatar Job Cuppen

small improvements

parent 2ae11e4c
......@@ -25,25 +25,23 @@ import qualified Data.Map as Map
:: Parse a = P (State -> (Maybe a, State))
instance Functor Parse where
fmap f (P g) = P \s1.let (a, s2) = g s1 in (fmap f a, s2)
fmap f (P st) = P \s1.
let (x, s2) = st s1
in (fmap f x, s2)
instance pure Parse where
pure a = P \s->(pure a, s)
instance <*> Parse where
(<*>) (P f) (P g)
= P \s1-> case f s1 of
(Just fa, s2) = case g s2 of
(Just ga, s3) = (Just (fa ga), s3)
(_, s3) = (Nothing, s3)
(_, s2) = (Nothing, s2)
(<*>) (P stab) (P sta) = P \s1 ->
let (mab, s2) = stab s1
(ma, s3) = sta s2
in (mab <*> ma, s3)
instance Alternative Parse where
(<|>) (P a1) (P a2) = P (\s -> case a1 s of
(<|>) (P a1) (P a2) = P \s -> case a1 s of
(Just a, sP) -> (Just a, sP)
(Nothing, sP) -> case a2 s of
(Just a, sPP) -> (Just a, sPP)
(Nothing, sPP) -> (Nothing, sPP))
(Nothing, sP) -> a2 s
empty = P (\s -> (Nothing, s))
(?) infix 1 :: Bool (v a) -> (v a) | Alternative v
......@@ -61,8 +59,8 @@ unParse (P f) = f
next :: Parse String // yield the next input token and move it to seen
next = P \s -> case s.input of
[] = (Nothing, s)
_ = ((Just (hd s.input)), {s & input = tl s.input})
[] = (Nothing, s)
[x:xs] = (Just x, {s & input = xs})
setGram :: Name Gram -> Parse Gram //use the given name to store the grammar
setGram n g = P \s->(pure g, {s & store = 'Map'.put n g s.store})
......@@ -98,16 +96,16 @@ listIntGram = Def "list" (
(Seq [Idn, Lit "=", Var "list"])
listArithGram :: Gram
listArithGram = Def "arithmetic" (
Alt [
Seq[
Int,
Alt [Lit "*", Lit "+", Lit "-"],
Var "arithmetic"
],
Int
]
)
listArithGram =
Def "arithmetic" (
Alt [
Seq[
Int,
Alt [Lit "*", Lit "+", Lit "-"],
Var "arithmetic"
],
Int
])
(Var "arithmetic")
listIntInput = ["mylist", "=", "[", "7", ":", "[", "42", ":", "[]", "]", "]"]
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment