aboutsummaryrefslogtreecommitdiff
path: root/src/parser
diff options
context:
space:
mode:
Diffstat (limited to 'src/parser')
-rw-r--r--src/parser/Core.hs15
-rw-r--r--src/parser/Expr.hs13
-rw-r--r--src/parser/Statement.hs8
3 files changed, 21 insertions, 15 deletions
diff --git a/src/parser/Core.hs b/src/parser/Core.hs
index 5928fe4..64e0b84 100644
--- a/src/parser/Core.hs
+++ b/src/parser/Core.hs
@@ -100,13 +100,16 @@ choice :: [Parser a] -> Parser a
choice [] = empty
choice (p:ps) = p <|> choice ps
--- verify :: (a -> Bool) -> Parser a -> Parser a
--- verify predicate p = do a <- p
--- if predicate a then p else Parser (\_ -> Left "Bonjour")
+verify :: (a -> Bool) -> a -> Parser a
+verify predicate x = if predicate x then pure x else empty
--- Parse a string of alpha character, converted to lower case
-labelP :: Parser String
-labelP = (map toLower) <$> some (satisfyChar isAlpha)
+-- Parse a string of alpha character
+-- Convert to lower case and check that the label isn't `i`
+varLabelP :: Parser String
+varLabelP = (map toLower <$> some (satisfyChar isAlpha)) >>= verify (/= "i")
+
+funLabelP :: Parser String
+funLabelP = map toLower <$> some (satisfyChar isAlpha)
floatP :: Parser Float
diff --git a/src/parser/Expr.hs b/src/parser/Expr.hs
index 5334578..b462197 100644
--- a/src/parser/Expr.hs
+++ b/src/parser/Expr.hs
@@ -27,20 +27,23 @@ termP = operatorChoiceChain factorP
factorP :: Parser Expr
factorP = choice [ parenthesizedExprP
- , imaginaryP
, Rational <$> floatP
, matrixP
- , Function <$> labelP <*> parenthesizedExprP
- , Variable <$> labelP
+ , Function <$> funLabelP <*> parenthesizedExprP
+ , imaginaryP
+ , Variable <$> varLabelP
] `chainl1` (infixOp "^" Exp)
where
parenthesizedExprP = parenthesis exprP
- imaginaryP = Imaginary <$> (floatP <|> pure 1.0) <* char 'i'
+ imaginaryP = (Complex 0) <$> (floatP <|> pure 1.0) <* char 'i'
-- Parse a matrix in the following format:
-- [ [a, b]; [c, d] ]
matrixP :: Parser Expr
- matrixP = Matrix <$> brackets (matrixRowP `sepBy` (char ';'))
+ matrixP = Matrix <$> (brackets (matrixRowP `sepBy` (char ';')) >>= verify check)
where matrixRowP = brackets (exprP `sepBy` (char ','))
brackets = between "[" "]"
+ check rows
+ | length rows == 0 = False
+ | otherwise = all ((length (head rows) ==) . length) $ tail rows
diff --git a/src/parser/Statement.hs b/src/parser/Statement.hs
index 889f24f..cfeabe1 100644
--- a/src/parser/Statement.hs
+++ b/src/parser/Statement.hs
@@ -14,15 +14,15 @@ data Statement
| FunctionDeclaration String String Expr
statementP :: Parser Statement
-statementP = functionDeclarationP <|> variableDeclarationP <|> polynomEvaluationP <|> evaluationP
+statementP = functionDeclarationP <|> variableDeclarationP <|> evaluationP <|> polynomEvaluationP
where
functionDeclarationP = FunctionDeclaration
- <$> labelP
- <*> parenthesis labelP
+ <$> funLabelP
+ <*> parenthesis varLabelP
<*> (char '=' *> exprP)
variableDeclarationP = VariableDeclaration
- <$> labelP
+ <$> varLabelP
<*> (char '=' *> exprP)
polynomEvaluationP = PolynomEvaluation <$> exprP <*> (char '=' *> exprP <* char '?')