diff options
Diffstat (limited to 'src/parser')
| -rw-r--r-- | src/parser/Core.hs | 21 | ||||
| -rw-r--r-- | src/parser/Expr.hs | 35 | ||||
| -rw-r--r-- | src/parser/Statement.hs | 11 |
3 files changed, 31 insertions, 36 deletions
diff --git a/src/parser/Core.hs b/src/parser/Core.hs index 08ac1a9..5928fe4 100644 --- a/src/parser/Core.hs +++ b/src/parser/Core.hs @@ -3,9 +3,9 @@ module Parser.Core where import Control.Applicative -import Control.Monad import Data.Char + newtype Parser a = Parser { runParser :: String -> Either String (a, String) } runParserStrict :: Parser a -> String -> Either String a @@ -76,12 +76,6 @@ string s = sequenceA $ char <$> s sepBy :: Parser a -> Parser b -> Parser [a] sepBy x sepatator = (:) <$> x <*> (many (sepatator *> x)) --- sepByMap :: (b -> a -> a) -> Parser b -> Parser a -> Parser [a] --- sepByMap f sep x = (:) <$> x <*> (many (f <$> sep <*> x)) - --- chainl :: Parser a -> Parser (a -> a -> a) -> a -> Parser a --- chainl p op a = chainl1 p op <|> pure a - -- Parse one or more occurences of p separated by op -- Apply op in a left associative maner on each value in p chainl1 :: Parser a -> Parser (a -> a -> a) -> Parser a @@ -106,8 +100,13 @@ choice :: [Parser a] -> Parser a choice [] = empty choice (p:ps) = p <|> choice ps -alphaStringP :: Parser String -alphaStringP = some (satisfyChar isAlpha) +-- verify :: (a -> Bool) -> Parser a -> Parser a +-- verify predicate p = do a <- p +-- if predicate a then p else Parser (\_ -> Left "Bonjour") + +-- Parse a string of alpha character, converted to lower case +labelP :: Parser String +labelP = (map toLower) <$> some (satisfyChar isAlpha) floatP :: Parser Float @@ -116,7 +115,7 @@ floatP = signed unsignedP unsignedP :: Parser Float unsignedP = read <$> p where p = do pos <- digitsP - char '.' + _ <- char '.' dec <- digitsP return (pos ++ "." ++ dec) <|> digitsP @@ -124,7 +123,7 @@ floatP = signed unsignedP digitsP = some $ satisfyChar isDigit -- at least one digit to avoid read exception signed :: Num a => Parser a -> Parser a - signed p = do char '-' + signed p = do _ <- char '-' x <- p return (-x) <|> p diff --git a/src/parser/Expr.hs b/src/parser/Expr.hs index 6a721f8..221d669 100644 --- a/src/parser/Expr.hs +++ b/src/parser/Expr.hs @@ -1,4 +1,4 @@ -module Parser.Expr where +module Parser.Expr (exprP) where import Control.Applicative @@ -6,20 +6,6 @@ import Parser.Core import Expr -imaginaryP :: Parser Expr -imaginaryP = Imaginary <$> (floatP <* char 'i') - -rationalP :: Parser Expr -rationalP = Rational <$> floatP - --- Parse a matrix in the following format: --- [ [a, b]; [c, d] ] -matrixP :: Parser Expr -matrixP = Matrix <$> brackets (matrixRowP `sepBy` (char ';')) - where matrixRowP = brackets (exprP `sepBy` (char ',')) - brackets = between "[" "]" - - -- Parse expression separated by one infix operator of the operator list operatorChoiceChain :: Parser a -> [Parser (a -> a -> a)] -> Parser a operatorChoiceChain x operators = x `chainl1` choice operators @@ -42,12 +28,19 @@ termP = operatorChoiceChain factorP factorP :: Parser Expr factorP = choice [ parenthesizedExprP , imaginaryP - , rationalP + , Rational <$> floatP , matrixP - , functionP - , variableP + , Function <$> labelP <*> parenthesizedExprP + , Variable <$> labelP ] `chainl1` (infixOp "^" Exp) + where + parenthesizedExprP = parenthesis exprP + + imaginaryP = Imaginary <$> (floatP <|> pure 1.0) <* char 'i' - where variableP = Variable <$> alphaStringP - functionP = Function <$> alphaStringP <*> parenthesizedExprP - parenthesizedExprP = parenthesis exprP + -- Parse a matrix in the following format: + -- [ [a, b]; [c, d] ] + matrixP :: Parser Expr + matrixP = Matrix <$> brackets (matrixRowP `sepBy` (char ';')) + where matrixRowP = brackets (exprP `sepBy` (char ',')) + brackets = between "[" "]" diff --git a/src/parser/Statement.hs b/src/parser/Statement.hs index ca16eca..889f24f 100644 --- a/src/parser/Statement.hs +++ b/src/parser/Statement.hs @@ -9,19 +9,22 @@ import Parser.Expr data Statement = Evaluation Expr + | PolynomEvaluation Expr Expr | VariableDeclaration String Expr | FunctionDeclaration String String Expr statementP :: Parser Statement -statementP = functionDeclarationP <|> variableDeclarationP <|> evaluationP +statementP = functionDeclarationP <|> variableDeclarationP <|> polynomEvaluationP <|> evaluationP where functionDeclarationP = FunctionDeclaration - <$> alphaStringP - <*> parenthesis alphaStringP + <$> labelP + <*> parenthesis labelP <*> (char '=' *> exprP) variableDeclarationP = VariableDeclaration - <$> alphaStringP + <$> labelP <*> (char '=' *> exprP) + polynomEvaluationP = PolynomEvaluation <$> exprP <*> (char '=' *> exprP <* char '?') + evaluationP = Evaluation <$> exprP <* char '=' <* char '?' |
