aboutsummaryrefslogtreecommitdiff
path: root/src/Expr.hs
blob: 4bf2c70499611011928be8b3e2b98e0245d61382 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
module Expr where

import Data.List

data Expr
    = Rational Float
    | Imaginary Float
    | Complex Float Float
    | Matrix [[Expr]]
    | Add Expr Expr
    | Sub Expr Expr
    | Mul Expr Expr
    | Div Expr Expr
    | Mod Expr Expr
    | Exp Expr Expr
    | Dot Expr Expr
    | Variable String
    | Function String Expr
    deriving (Eq)

data Expr
    = Atom
    | BinOp
    | Variable String
    | Function String Expr


instance Show Expr where
    show (Rational r) = show r
    show (Imaginary i) = show i ++ "i"
    show (Complex a b) = show a ++ " + " ++ show b ++ "i"
    show (Matrix m) = intercalate "\n" (map showRow m)
        where showRow r = "[ " ++ intercalate ", " (map show r) ++ " ]"
    show (Add e1 e2) = show e1 ++ " + " ++ show e2
    show (Sub e1 e2) = show e1 ++ " - " ++ show e2
    show (Mul e1 e2) = show e1 ++ " * " ++ show e2
    show (Div e1 e2) = show e1 ++ " / " ++ show e2
    show (Mod e1 e2) = show e1 ++ " % " ++ show e2
    show (Exp e1 e2) = show e1 ++ " ^ " ++ show e2
    show (Dot e1 e2) = show e1 ++ " ** " ++ show e2
    show (Variable name) = name
    show (Function name e) = name ++ "(" ++ show e ++ ")"