mirror of
https://github.com/zoriya/HAL.git
synced 2026-06-10 00:27:40 +00:00
Adding a +
This commit is contained in:
@@ -30,6 +30,7 @@ library
|
|||||||
Expressions
|
Expressions
|
||||||
LispEnv
|
LispEnv
|
||||||
LispParser
|
LispParser
|
||||||
|
Maths
|
||||||
other-modules:
|
other-modules:
|
||||||
Paths_hal
|
Paths_hal
|
||||||
hs-source-dirs:
|
hs-source-dirs:
|
||||||
|
|||||||
+2
-1
@@ -1,6 +1,7 @@
|
|||||||
module LispEnv where
|
module LispEnv where
|
||||||
import Expressions
|
import Expressions
|
||||||
import Evaluator
|
import Evaluator
|
||||||
|
import Maths
|
||||||
|
|
||||||
|
|
||||||
defaultEnv :: LispEnv
|
defaultEnv :: LispEnv
|
||||||
@@ -11,7 +12,7 @@ defaultEnv = [
|
|||||||
("eq?", ABuiltin "eq?" evalEq),
|
("eq?", ABuiltin "eq?" evalEq),
|
||||||
("atom?", ABuiltin "atom?" evalAtom),
|
("atom?", ABuiltin "atom?" evalAtom),
|
||||||
("cond", ABuiltin "cond" evalCond)
|
("cond", ABuiltin "cond" evalCond)
|
||||||
]
|
] ++ mathEnv
|
||||||
|
|
||||||
evalCons :: [Statement] -> LispEnv -> Either String (Atom, LispEnv)
|
evalCons :: [Statement] -> LispEnv -> Either String (Atom, LispEnv)
|
||||||
evalCons [first, second] env = do
|
evalCons [first, second] env = do
|
||||||
|
|||||||
@@ -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)
|
||||||
Reference in New Issue
Block a user