all: added primitives

This commit is contained in:
2017-05-22 18:52:00 -04:00
parent 139225dd14
commit 0fb92f60da
5 changed files with 35 additions and 2 deletions

26
src/BC/Primitives.hs Normal file
View File

@@ -0,0 +1,26 @@
module BC.Primitives where
import BC.Types
primitives :: [(String, Value)]
primitives = map valuize primitivesList
where valuize (x, y) = (x, BNative y)
primitivesList :: [(String, [Value] -> Value)]
primitivesList = [ ("cos", pcos)
, ("sqrt", psqrt)
]
psqrt :: [Value] -> Value
psqrt [BNum (BInt x)] = BNum $ BFloat $ sqrt $ fromIntegral x
psqrt [BNum (BFloat x)] = BNum $ BFloat $ sqrt x
psqrt [x] = BErr $ "Expected argument to be a number, got " ++ show x
pcos :: [Value] -> Value
pcos [BNum (BInt x)] = BNum $ BFloat $ cos $ fromIntegral x
pcos [BNum (BFloat x)] = BNum $ BFloat $ cos x
pcos [x] = BErr $ "Expected argument to be a number, got " ++ show x