Adding a +

This commit is contained in:
Zoe Roux
2021-11-08 16:33:41 +01:00
parent b5054ba12b
commit de5e375bd1
3 changed files with 23 additions and 1 deletions

View File

@@ -30,6 +30,7 @@ library
Expressions
LispEnv
LispParser
Maths
other-modules:
Paths_hal
hs-source-dirs:

View File

@@ -1,6 +1,7 @@
module LispEnv where
import Expressions
import Evaluator
import Maths
defaultEnv :: LispEnv
@@ -11,7 +12,7 @@ defaultEnv = [
("eq?", ABuiltin "eq?" evalEq),
("atom?", ABuiltin "atom?" evalAtom),
("cond", ABuiltin "cond" evalCond)
]
] ++ mathEnv
evalCons :: [Statement] -> LispEnv -> Either String (Atom, LispEnv)
evalCons [first, second] env = do

20
src/Maths.hs Normal file
View File

@@ -0,0 +1,20 @@
module Maths where
import Expressions
import Evaluator
mathEnv :: LispEnv
mathEnv = [
("+", ABuiltin "+" evalPlus)
]
evalPlus :: [Statement] -> LispEnv -> Either String (Atom, LispEnv)
evalPlus (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 **"
evalPlus [] env = Right (AInt 0, env)