Adding mod

This commit is contained in:
Zoe Roux
2021-11-08 18:10:43 +01:00
parent 5f3beea39c
commit 569ce977ae
3 changed files with 20 additions and 7 deletions
+4
View File
@@ -14,6 +14,10 @@ data Atom =
AFalse |
ANothing
_fromBool :: Bool -> Atom
_fromBool True = ATrue
_fromBool False = AFalse
newtype SExpr = SExpr [Statement]
instance Show Atom where
-5
View File
@@ -41,11 +41,6 @@ evalCdr [Expr expr] env = do
evalCdr [Atom bad] _ = Left $ "**Error: " ++ show bad ++ " is not a pair.**"
evalCdr _ _ = Left "**Error: incorect argument count in cdr**"
_fromBool :: Bool -> Atom
_fromBool True = ATrue
_fromBool False = AFalse
evalEq :: [Statement] -> LispEnv -> Either String (Atom, LispEnv)
evalEq [Atom (AInt f), Atom (AInt s)] env = Right (_fromBool (f == s), env)
evalEq [Atom (ASymbol f), Atom (ASymbol s)] env = Right (_fromBool (f == s), env)
+16 -2
View File
@@ -8,7 +8,8 @@ mathEnv = [
("-", ABuiltin "-" evalMinus),
("*", ABuiltin "*" evalMult),
("div", ABuiltin "div" evalDiv),
("mod", ABuiltin "mod" evalMod)
("mod", ABuiltin "mod" evalMod),
("<", ABuiltin "<" evalLessThan)
]
evalPlus :: [Statement] -> LispEnv -> Either String (Atom, LispEnv)
@@ -72,4 +73,17 @@ evalMod [Expr expr, other] env = do
evalMod [other, Expr expr] env = do
(res, nEnv) <- evalS expr env
evalMod [other, Atom res] nEnv
evalMod _ _ = Left "**Error: Invalid arguments in mod.**"
evalMod _ _ = Left "**Error: Invalid arguments in mod.**"
evalLessThan :: [Statement] -> LispEnv -> Either String (Atom, LispEnv)
evalLessThan [Atom (AInt f), Atom (AInt s)] env = Right (_fromBool (f < s), env)
evalLessThan [Atom (AFloat f), Atom (AFloat s)] env = Right (_fromBool (f < s), env)
evalLessThan [Atom (AFloat f), Atom (AInt s)] env = Right (_fromBool (f < fromIntegral s), env)
evalLessThan [Atom (AInt f), Atom (AFloat s)] env = Right (_fromBool (fromIntegral f < s), env)
evalLessThan [Expr expr, other] env = do
(res, nEnv) <- evalS expr env
evalLessThan [Atom res, other] nEnv
evalLessThan [other, Expr expr] env = do
(res, nEnv) <- evalS expr env
evalLessThan [other, Atom res] nEnv
evalLessThan _ _ = Left "**Error: Invalid arguments in <.**"