aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-11-12 05:54:34 +0000
committerAkshay <[email protected]>2020-11-12 05:54:34 +0000
commit29d8f8ea04ca651616b4bdd8d3a61da9f0cfae29 (patch)
treedddceec87742ddea4365501d437c40fc887ffda9
parent6bc5de761069b1acbe47ba8aa0a8a8deae6ab43c (diff)
use maps over assoc lists
-rw-r--r--lisk.cabal1
-rw-r--r--src/Operators.hs39
2 files changed, 8 insertions, 32 deletions
diff --git a/lisk.cabal b/lisk.cabal
index 47e111b..2971b76 100644
--- a/lisk.cabal
+++ b/lisk.cabal
@@ -22,6 +22,7 @@ library
22 build-depends: 22 build-depends:
23 base == 4.*, 23 base == 4.*,
24 parsec == 3.*, 24 parsec == 3.*,
25 containers >= 0.6,
25 mtl >= 2.1 26 mtl >= 2.1
26 exposed-modules: 27 exposed-modules:
27 Base, 28 Base,
diff --git a/src/Operators.hs b/src/Operators.hs
index bab888d..23b17ea 100644
--- a/src/Operators.hs
+++ b/src/Operators.hs
@@ -3,9 +3,11 @@ module Operators (primitives) where
3import Base 3import Base
4import Control.Monad.Except 4import Control.Monad.Except
5import Error.Base (LispError (..), LispResult (..)) 5import Error.Base (LispError (..), LispResult (..))
6import qualified Data.Map as M
6 7
7primitives :: [(String, [Expr] -> LispResult Expr)] 8-- primitives :: [(String, [Expr] -> LispResult Expr)]
8primitives = map (\(n, f) -> (n, f n)) 9primitives :: M.Map String ([Expr] -> LispResult Expr)
10primitives = M.fromList $ map (\(n, f) -> (n, f n))
9 [ 11 [
10 ("+", arithmetic (+)) 12 ("+", arithmetic (+))
11 , ("-", arithmetic (-)) 13 , ("-", arithmetic (-))
@@ -26,28 +28,6 @@ primitives = map (\(n, f) -> (n, f n))
26 , ("null?", isNull) 28 , ("null?", isNull)
27 ] 29 ]
28 30
29data LispNumber = I Integer
30 | F Double
31 deriving (Eq, Ord)
32
33instance Num LispNumber where
34 -- TODO:
35 -- float op anything = float
36 -- int op int = int
37 -- int op float = float
38 (I a) + (I b) = I $ a + b
39 (F a) + (F b) = F $ a + b
40 (I a) - (I b) = I $ a - b
41 (F a) - (F b) = F $ a - b
42 (I a) * (I b) = I $ a * b
43 (F a) * (F b) = F $ a * b
44
45instance Fractional LispNumber where
46 (I a) / (I b) = F $ fromIntegral a / fromIntegral b
47 (F a) / (I b) = F $ a / fromIntegral b
48 (I a) / (F b) = F $ fromIntegral a / b
49 (F a) / (F b) = F $ a / b
50
51type FName = String 31type FName = String
52type Arithmetic = LispNumber -> LispNumber -> LispNumber 32type Arithmetic = LispNumber -> LispNumber -> LispNumber
53type Comparator = LispNumber -> LispNumber -> Bool 33type Comparator = LispNumber -> LispNumber -> Bool
@@ -59,7 +39,7 @@ arithmetic op name args
59 | null args = throwError $ ArgCount name 1 args 39 | null args = throwError $ ArgCount name 1 args
60 | otherwise = do 40 | otherwise = do
61 as <- mapM unwrapNum args 41 as <- mapM unwrapNum args
62 return . wrapNum $ foldl1 op as 42 return . Number $ foldl1 op as
63 43
64comparator :: Comparator -> FName -> [Expr] -> LispResult Expr 44comparator :: Comparator -> FName -> [Expr] -> LispResult Expr
65comparator op name args 45comparator op name args
@@ -81,13 +61,8 @@ naryBool op name args
81 return . BoolLiteral $ foldl1 op as 61 return . BoolLiteral $ foldl1 op as
82 62
83unwrapNum :: Expr -> LispResult LispNumber 63unwrapNum :: Expr -> LispResult LispNumber
84unwrapNum (IntLiteral n) = return $ I n 64unwrapNum (Number x) = return x
85unwrapNum (FloatLiteral n) = return $ F n 65unwrapNum x = throwError $ TypeMismatch "number" x
86unwrapNum x = throwError $ TypeMismatch "number" x
87
88wrapNum :: LispNumber -> Expr
89wrapNum (I n) = IntLiteral n
90wrapNum (F n) = FloatLiteral n
91 66
92unwrapBool :: Expr -> LispResult Bool 67unwrapBool :: Expr -> LispResult Bool
93unwrapBool (BoolLiteral s) = return s 68unwrapBool (BoolLiteral s) = return s