aboutsummaryrefslogtreecommitdiff
path: root/parser.hs
blob: 61565c3d29a31a5644055367ba54b63ae384b7f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
module Parser where

import Control.Applicative


newtype Parser a = Parser { runParser :: String -> Maybe (a, String) }

instance Functor Parser where
    fmap f (Parser p) = Parser new_p
        where new_p s = do
                (x, s') <- p s
                return (f x, s')

instance Applicative Parser where
    pure x = Parser (\s -> Just (x, s))
    (Parser p1) <*> (Parser p2) = Parser new_p
        where new_p s = do
                (f, s') <- p1 s
                (x, s'') <- p2 s'
                return (f x, s'')
    -- (Parser p1) *> (Parser p2) = Parser new_p
    -- (Parser p1) <* (Parser p2) = Parser new_p

instance Alternative Parser where
    empty = Parser (\_ -> Nothing)
    (Parser p1) <|> (Parser p2) = Parser new_p
        where new_p s = p1 s <|> p2 s


data Equation
    = EquationAdd
    | EquationSub
    | EquationMul
    -- | EquationDiv ?
    | EquationExp
    | EquationEqu
    | EquationNum Float

charP :: Char -> Parser Char
charP c = Parser f
    where f ""     = Nothing
          f (c:cs) = Just (c, cs)

-- equationAddP :: Parser Equation
-- equationAddP = charP '+' *> EquationAdd

-- equationP :: Parser Equation
-- equationP =