From 2b0b62b44a87536597050c525322c7bcc745bdb2 Mon Sep 17 00:00:00 2001 From: Charles Date: Wed, 3 Jun 2020 22:57:10 +0200 Subject: Added polynom solver from computorv1, Added matrix multipilcation --- src/parser/Expr.hs | 35 ++++++++++++++--------------------- 1 file changed, 14 insertions(+), 21 deletions(-) (limited to 'src/parser/Expr.hs') 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 "[" "]" -- cgit