From 5e7d3a5ff586ac75b768a9a1c1f2d5b80960e821 Mon Sep 17 00:00:00 2001 From: Charles Date: Wed, 3 Jun 2020 15:41:17 +0200 Subject: Back to where I was but without the mess --- src/parser/Core.hs | 3 +-- src/parser/Statement.hs | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 src/parser/Statement.hs (limited to 'src/parser') diff --git a/src/parser/Core.hs b/src/parser/Core.hs index 8deea2f..08ac1a9 100644 --- a/src/parser/Core.hs +++ b/src/parser/Core.hs @@ -14,7 +14,6 @@ runParserStrict p input = case runParser p input of Right (_, rest) -> Left $ "Unconsumed input: \"" ++ rest ++ "\"" Left err -> Left err - ------------------------------------------------------------------------------- -- Parser instances ------------------------------------------------------------------------------- @@ -66,7 +65,7 @@ satisfyChar :: (Char -> Bool) -> Parser Char satisfyChar predicate = Parser p where p [] = Left "Expected input" p (c:cs) = if predicate c then Right (c, cs) - else Left "Expected char" + else Left $ "Unexpected char '" ++ [c] ++ "'" char :: Char -> Parser Char char c = satisfyChar (c ==) diff --git a/src/parser/Statement.hs b/src/parser/Statement.hs new file mode 100644 index 0000000..ca16eca --- /dev/null +++ b/src/parser/Statement.hs @@ -0,0 +1,27 @@ +module Parser.Statement where + +import Control.Applicative + +import Expr +import Parser.Core +import Parser.Expr + + +data Statement + = Evaluation Expr + | VariableDeclaration String Expr + | FunctionDeclaration String String Expr + +statementP :: Parser Statement +statementP = functionDeclarationP <|> variableDeclarationP <|> evaluationP + where + functionDeclarationP = FunctionDeclaration + <$> alphaStringP + <*> parenthesis alphaStringP + <*> (char '=' *> exprP) + + variableDeclarationP = VariableDeclaration + <$> alphaStringP + <*> (char '=' *> exprP) + + evaluationP = Evaluation <$> exprP <* char '=' <* char '?' -- cgit