diff options
Diffstat (limited to 'src/Base.hs')
-rw-r--r-- | src/Base.hs | 51 |
1 files changed, 39 insertions, 12 deletions
diff --git a/src/Base.hs b/src/Base.hs index 08131e8..422e534 100644 --- a/src/Base.hs +++ b/src/Base.hs | |||
@@ -1,10 +1,41 @@ | |||
1 | module Base (Expr (..) | 1 | module Base (Expr (..) |
2 | , Env (..) | 2 | , Env (..) |
3 | , Function (..) | 3 | , LispNumber (..) |
4 | ) where | 4 | ) where |
5 | 5 | ||
6 | import Data.IORef | 6 | import Data.IORef |
7 | 7 | ||
8 | data LispNumber = I Integer | ||
9 | | F Double | ||
10 | deriving (Eq, Ord) | ||
11 | |||
12 | instance Num LispNumber where | ||
13 | -- addition | ||
14 | (I a) + (I b) = I $ a + b | ||
15 | (F a) + (F b) = F $ a + b | ||
16 | (F a) + (I b) = F $ a + fromIntegral b | ||
17 | (I a) + (F b) = F b + I a | ||
18 | |||
19 | -- subtraction | ||
20 | (I a) - (I b) = I $ a - b | ||
21 | (F a) - (F b) = F $ a - b | ||
22 | (F a) - (I b) = F $ a - fromIntegral b | ||
23 | (I a) - (F b) = F b - I a | ||
24 | |||
25 | -- multiplication | ||
26 | (I a) * (I b) = I $ a * b | ||
27 | (F a) * (F b) = F $ a * b | ||
28 | (F a) * (I b) = F $ a * fromIntegral b | ||
29 | (I a) * (F b) = F b * I a | ||
30 | |||
31 | instance Fractional LispNumber where | ||
32 | (I a) / (I b) = F $ fromIntegral a / fromIntegral b | ||
33 | (F a) / (I b) = F $ a / fromIntegral b | ||
34 | (I a) / (F b) = recip $ F b / I a | ||
35 | (F a) / (F b) = F $ a / b | ||
36 | recip (F x) = F $ 1 / x | ||
37 | recip (I x) = F $ 1 / fromIntegral x | ||
38 | |||
8 | -- TODO: use LispNumber (src/Operators.hs) here instead of IntLiteral and FloatLiteral | 39 | -- TODO: use LispNumber (src/Operators.hs) here instead of IntLiteral and FloatLiteral |
9 | -- TODO: add character literals: \#a \#b \#c \#space \#newline | 40 | -- TODO: add character literals: \#a \#b \#c \#space \#newline |
10 | -- TODO: add support for complex numbers, oct and hex numbers | 41 | -- TODO: add support for complex numbers, oct and hex numbers |
@@ -12,19 +43,15 @@ data Expr = List [Expr] | |||
12 | | Vector [Expr] | 43 | | Vector [Expr] |
13 | | DottedList [Expr] Expr | 44 | | DottedList [Expr] Expr |
14 | | StringLiteral String | 45 | | StringLiteral String |
15 | | IntLiteral Integer | 46 | | Number LispNumber |
16 | | FloatLiteral Double | ||
17 | | BoolLiteral Bool | 47 | | BoolLiteral Bool |
18 | | Id String | 48 | | Id String |
49 | | Function { params :: [String] | ||
50 | , body :: Expr | ||
51 | , extendedEnv :: Env | ||
52 | } | ||
19 | deriving (Eq) | 53 | deriving (Eq) |
20 | 54 | ||
21 | data Function = | ||
22 | Function { | ||
23 | params :: [String] | ||
24 | , body :: Expr | ||
25 | , environment :: Env | ||
26 | } | ||
27 | |||
28 | type Env = IORef [(String, IORef Expr)] | 55 | type Env = IORef [(String, IORef Expr)] |
29 | 56 | ||
30 | showLispList :: [Expr] -> String | 57 | showLispList :: [Expr] -> String |
@@ -35,8 +62,8 @@ instance Show Expr where | |||
35 | show (List xs) = "(" ++ showLispList xs ++ ")" | 62 | show (List xs) = "(" ++ showLispList xs ++ ")" |
36 | show (Vector xs) = "#(" ++ showLispList xs ++ ")" | 63 | show (Vector xs) = "#(" ++ showLispList xs ++ ")" |
37 | show (StringLiteral s) = "\"" ++ s ++ "\"" | 64 | show (StringLiteral s) = "\"" ++ s ++ "\"" |
38 | show (IntLiteral n) = show n | 65 | show (Number (I n)) = show n |
39 | show (FloatLiteral n) = show n | 66 | show (Number (F n)) = show n |
40 | show (BoolLiteral True) = "#t" | 67 | show (BoolLiteral True) = "#t" |
41 | show (BoolLiteral False) = "#f" | 68 | show (BoolLiteral False) = "#f" |
42 | show (Id i) = i | 69 | show (Id i) = i |