mirror of
https://github.com/zoriya/HAL.git
synced 2026-06-01 05:17:35 +00:00
Adding - and *
This commit is contained in:
+28
-2
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user