aboutsummaryrefslogtreecommitdiff
path: root/src/parser/Expr.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/parser/Expr.hs')
-rw-r--r--src/parser/Expr.hs35
1 files changed, 14 insertions, 21 deletions
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 "[" "]"