Adding - and *

This commit is contained in:
Zoe Roux
2021-11-08 16:36:04 +01:00
parent de5e375bd1
commit f126bea6a9
+28 -2
View File
@@ -4,7 +4,9 @@ import Evaluator
mathEnv :: LispEnv
mathEnv = [
("+", ABuiltin "+" evalPlus)
("+", ABuiltin "+" evalPlus),
("-", ABuiltin "-" evalMinus),
("*", ABuiltin "*" evalMult)
]
evalPlus :: [Statement] -> LispEnv -> Either String (Atom, LispEnv)
@@ -16,5 +18,29 @@ evalPlus (x:xs) env = do
(AFloat f, AFloat s) -> Right (AFloat $ f + s, nnEnv)
(AFloat f, AInt s) -> Right (AFloat $ f + fromIntegral s, nnEnv)
(AInt f, AFloat s) -> Right (AFloat $ fromIntegral f + s, nnEnv)
(value, _) -> Left $ "**Error: \"" ++ show value ++"\" is not a number **"
(value, _) -> Left $ "**Error: \"" ++ show value ++ "\" is not a number **"
evalPlus [] env = Right (AInt 0, env)
evalMinus :: [Statement] -> LispEnv -> Either String (Atom, LispEnv)
evalMinus (x:xs) env = do
(value, nEnv) <- eval x env
(rest, nnEnv) <- evalPlus xs nEnv
case (value, rest) of
(AInt f, AInt s) -> Right (AInt $ f - s, nnEnv)
(AFloat f, AFloat s) -> Right (AFloat $ f - s, nnEnv)
(AFloat f, AInt s) -> Right (AFloat $ f - fromIntegral s, nnEnv)
(AInt f, AFloat s) -> Right (AFloat $ fromIntegral f - s, nnEnv)
(value, _) -> Left $ "**Error: \"" ++ show value ++ "\" is not a number **"
evalMinus [] env = Right (AInt 0, env)
evalMult :: [Statement] -> LispEnv -> Either String (Atom, LispEnv)
evalMult (x:xs) env = do
(value, nEnv) <- eval x env
(rest, nnEnv) <- evalPlus xs nEnv
case (value, rest) of
(AInt f, AInt s) -> Right (AInt $ f * s, nnEnv)
(AFloat f, AFloat s) -> Right (AFloat $ f * s, nnEnv)
(AFloat f, AInt s) -> Right (AFloat $ f * fromIntegral s, nnEnv)
(AInt f, AFloat s) -> Right (AFloat $ fromIntegral f * s, nnEnv)
(value, _) -> Left $ "**Error: \"" ++ show value ++ "\" is not a number **"
evalMult [] env = Right (AInt 1, env)