From 5097a20545bbeeafec191f97e9d0ae3d215ada90 Mon Sep 17 00:00:00 2001 From: Akshay Date: Fri, 9 Oct 2020 18:52:45 +0530 Subject: add binary boolean operators --- src/Error.hs | 2 +- src/Operators.hs | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Error.hs b/src/Error.hs index 165cc5c..5ba48bf 100644 --- a/src/Error.hs +++ b/src/Error.hs @@ -20,7 +20,7 @@ unwordsList = unwords . map show instance Show LispError where show (Parse e) = "Parser Error: " ++ show e show (BadForm s expr) = "Bad Form: " ++ s ++ ": " ++ show expr - show (ArgCount n es) = "Invalid arity, expected " ++ show n ++ ", got values: " ++ unwordsList es + show (ArgCount n es) = "Invalid arity, expected " ++ show n ++ ", got value(s): " ++ unwordsList es show (UnknownFunction fn) = "Cannot apply function: " ++ fn show (TypeMismatch msg got) = "Type mismatch, expected " ++ msg ++ ", got: " ++ show got diff --git a/src/Operators.hs b/src/Operators.hs index fc0608e..beb6364 100644 --- a/src/Operators.hs +++ b/src/Operators.hs @@ -13,10 +13,17 @@ primitives = , ("-", arithmetic (-)) , ("*", arithmetic (*)) , ("/", arithmetic (/)) + , (">", comparator (>)) + , ("<", comparator (<)) + , (">=", comparator (>=)) + , ("<=", comparator (<=)) + , ("=", comparator (==)) + , ("!=", comparator (/=)) ] data LispNumber = I Integer | F Double + deriving (Eq, Ord) instance Num LispNumber where -- TODO: @@ -41,6 +48,13 @@ arithmetic op args as <- mapM unwrapNum args return . wrapNum $ foldl1 op as +comparator :: (LispNumber -> LispNumber -> Bool) -> [Expr] -> LispResult Expr +comparator op args + | length args < 2 = throwError $ ArgCount 2 args + | otherwise = do + as <- mapM unwrapNum args + return . BoolLiteral . all (== True) $ zipWith op as (tail as) + unwrapNum :: Expr -> LispResult LispNumber unwrapNum (IntLiteral n) = return $ I n unwrapNum (FloatLiteral n) = return $ F n -- cgit v1.2.3