aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--parser.hs48
1 files changed, 48 insertions, 0 deletions
diff --git a/parser.hs b/parser.hs
new file mode 100644
index 0000000..61565c3
--- /dev/null
+++ b/parser.hs
@@ -0,0 +1,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 =