diff options
author | Akshay <[email protected]> | 2020-10-09 07:05:56 +0100 |
---|---|---|
committer | Akshay <[email protected]> | 2020-10-09 07:05:56 +0100 |
commit | 06bf4c656377572859846767cb9af5db8fc27893 (patch) | |
tree | 489b727cf1e6d31f2b1de66a7ee919c78cca19a5 /src/Operators.hs | |
parent | 9160b3648a69303c2ed288edec3d8e9bcec52f11 (diff) |
use mtl to generate errors
Diffstat (limited to 'src/Operators.hs')
-rw-r--r-- | src/Operators.hs | 18 |
1 files changed, 12 insertions, 6 deletions
diff --git a/src/Operators.hs b/src/Operators.hs index e57f885..3b96281 100644 --- a/src/Operators.hs +++ b/src/Operators.hs | |||
@@ -2,9 +2,11 @@ module Operators ( | |||
2 | primitives | 2 | primitives |
3 | ) where | 3 | ) where |
4 | 4 | ||
5 | import Control.Monad.Except | ||
6 | import Error (LispError (..), LispResult (..)) | ||
5 | import Parser | 7 | import Parser |
6 | 8 | ||
7 | primitives :: [(String, [Expr] -> Expr)] | 9 | primitives :: [(String, [Expr] -> LispResult Expr)] |
8 | primitives = | 10 | primitives = |
9 | [ | 11 | [ |
10 | ("+", arithmetic (+)) | 12 | ("+", arithmetic (+)) |
@@ -13,10 +15,14 @@ primitives = | |||
13 | , ("/", arithmetic div) | 15 | , ("/", arithmetic div) |
14 | ] | 16 | ] |
15 | 17 | ||
16 | arithmetic :: (Integer -> Integer -> Integer) -> [Expr] -> Expr | 18 | arithmetic :: (Integer -> Integer -> Integer) -> [Expr] -> LispResult Expr |
17 | arithmetic op args = IntLiteral $ foldl1 op $ map unwrapNum args | 19 | arithmetic op args |
20 | | length args < 2 = throwError $ ArgCount 2 args | ||
21 | | otherwise = do | ||
22 | as <- mapM unwrapNum args | ||
23 | return . IntLiteral $ foldl1 op as | ||
18 | 24 | ||
19 | unwrapNum :: Expr -> Integer | 25 | unwrapNum :: Expr -> LispResult Integer |
20 | unwrapNum (IntLiteral n) = n | 26 | unwrapNum (IntLiteral n) = return n |
21 | unwrapNum _ = undefined | 27 | unwrapNum x = throwError $ TypeMismatch "number" x |
22 | 28 | ||