aboutsummaryrefslogtreecommitdiff
path: root/src/parser
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-06-05 14:48:14 +0200
committerCharles <sircharlesaze@gmail.com>2020-06-05 14:48:14 +0200
commitf5ddd91d290a0c508e04cce2cb19c4c8bae32835 (patch)
tree3d3082c3d6fa977b12290f4cd2ed6c7ada4e9606 /src/parser
parent45cada8eade7242eb7f29af7b92858e9a1a7f68b (diff)
downloadcomputorv2-f5ddd91d290a0c508e04cce2cb19c4c8bae32835.tar.gz
computorv2-f5ddd91d290a0c508e04cce2cb19c4c8bae32835.tar.bz2
computorv2-f5ddd91d290a0c508e04cce2cb19c4c8bae32835.zip
Removed Imaginary type, Added variable name check (/= i), Added Matrix check if rectangular
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 '?')