aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/expr.hs29
-rw-r--r--src/imag.hs6
-rw-r--r--src/manifest7
-rw-r--r--src/matrix.hs4
-rw-r--r--src/parser.hs41
5 files changed, 58 insertions, 29 deletions
diff --git a/src/expr.hs b/src/expr.hs
index 3cada63..ccc0e36 100644
--- a/src/expr.hs
+++ b/src/expr.hs
@@ -1,15 +1,24 @@
module Expr where
+import Imag
+import Matrix
+
-- data X = Expr | Imag | Matrix
-data Expr = Expr Term Expr | ExprSingle Term
+-- class ExprElement where
+-- subExpr :: ExprElement a => a -> a
+-- composed :: ExprElement a -> a -> (a, a)
+
+data AExpr = AExpr Term AExpr | AExprSingle Term
data Term = Term Factor Term | TermSingle Factor
data Factor = Factor Base Factor | FactorSingle Base
-data Base = Base Expr | BaseSingle Float
+data Base = Base AExpr | BaseSingle Expr
-instance Show Expr where
- show (ExprSingle t) = show t
- show (Expr t e) = show t ++ " + " ++ show e
+data Expr = ExprF Float | ExprI Imag | ExprM (Matrix AExpr)
+
+instance Show AExpr where
+ show (AExprSingle t) = show t
+ show (AExpr t e) = show t ++ " + " ++ show e
instance Show Term where
show (TermSingle f) = show f
@@ -21,4 +30,12 @@ instance Show Factor where
instance Show Base where
show (BaseSingle x) = show x
- show (Base e) = "( " ++ show e ++ " )"
+ show (Base e) = "(" ++ show e ++ ")"
+
+instance Show Expr where
+ show (ExprF f) = show f
+ show (ExprI i) = show i
+ show (ExprM m) = show m
+
+-- eval :: Expr -> Float
+-- eval
diff --git a/src/imag.hs b/src/imag.hs
new file mode 100644
index 0000000..71c0607
--- /dev/null
+++ b/src/imag.hs
@@ -0,0 +1,6 @@
+module Imag where
+
+newtype Imag = Imag { getImag :: Float }
+
+instance Show Imag where
+ show (Imag i) = show i ++ "i"
diff --git a/src/manifest b/src/manifest
index dc03948..60f7683 100644
--- a/src/manifest
+++ b/src/manifest
@@ -10,10 +10,11 @@ State Data struct:
- variable
-1. ( )
-2. ^
-3. * / % **
+5. real imag matrix
4. + -
+3. * / % **
+2. ^
+1. ( )
expr ::= <term> (+ | -) <expr> | <term>
term ::= <factor> (* | / | % | **)? <term> | <factor> -- default to '*'
diff --git a/src/matrix.hs b/src/matrix.hs
index 21c85f0..c2e6654 100644
--- a/src/matrix.hs
+++ b/src/matrix.hs
@@ -2,8 +2,8 @@ module Matrix where
import Data.List
-
-newtype Matrix a = Matrix { getMatrix :: [[a]] }
+newtype Matrix a = Matrix { getMatrix :: [MatrixRow a] }
+type MatrixRow a = [a]
instance Show a => Show (Matrix a) where
show (Matrix m) = intercalate "\n" (map showLine m)
diff --git a/src/parser.hs b/src/parser.hs
index f402b80..d708a6c 100644
--- a/src/parser.hs
+++ b/src/parser.hs
@@ -1,15 +1,12 @@
module Parser where
--- ( parse
--- , equationP
--- ) where
import Control.Applicative
import Control.Monad
import Data.Char
import Expr
--- import Equation
--- import Complex
+import Imag
+import Matrix
newtype Parser a = Parser (String -> Maybe (a, String))
@@ -58,11 +55,10 @@ satisfyChar f = Parser p
else Nothing
sepBy :: Parser b -> Parser a -> Parser [a]
-sepBy sep x = many (sep *> x)
+sepBy sep x = (:) <$> x <*> (many (sep *> x))
sepByMap :: (b -> a -> a) -> Parser b -> Parser a -> Parser [a]
-sepByMap f sep x = many (f <$> sep <*> x)
-
+sepByMap f sep x = (:) <$> x <*> (many (f <$> sep <*> x))
signed :: Num a => Parser a -> Parser a
signed p = do charP '-'
x <- p
@@ -101,15 +97,21 @@ unsignedFloatP = readParser p
floatP :: Parser Float
floatP = signed unsignedFloatP
--- imaginaryP :: Parser Imaginary
--- imaginaryP = floatP <* charP 'i'
+imagP :: Parser Imag
+imagP = Imag <$> (floatP <* charP 'i')
+
+matrixP :: Parser (Matrix AExpr)
+matrixP = Matrix <$> (charP '[' *> (sepBy (charP ';') matrixRowP) <* charP ']')
-exprP :: Parser Expr
-exprP = do x <- termP
- charP '+'
- y <- exprP
- return (Expr x y)
- <|> (ExprSingle <$> termP)
+matrixRowP :: Parser (MatrixRow AExpr)
+matrixRowP = charP '[' *> (sepBy (charP ',') aExprP) <* charP ']'
+
+aExprP :: Parser AExpr
+aExprP = do x <- termP
+ charP '+'
+ y <- aExprP
+ return (AExpr x y)
+ <|> (AExprSingle <$> termP)
termP :: Parser Term
termP = do f <- factorP
@@ -127,5 +129,8 @@ factorP = do b <- baseP
<|> (FactorSingle <$> baseP)
baseP :: Parser Base
-baseP = (charP '(' *> (Base <$> exprP) <* charP ')')
- <|> (BaseSingle <$> floatP)
+baseP = (charP '(' *> (Base <$> aExprP) <* charP ')')
+ <|> (BaseSingle <$> ExprI <$> imagP)
+ <|> (BaseSingle <$> ExprF <$> floatP)
+ <|> (BaseSingle <$> ExprM <$> matrixP)
+