aboutsummaryrefslogtreecommitdiffstats
path: root/src/Builtins.hs
diff options
context:
space:
mode:
authorJan Tuomi <jan.tuomi@valuemotive.com>2022-09-24 15:04:03 +0300
committerJan Tuomi <jans.tuomi@gmail.com>2022-12-05 14:21:53 +0200
commitaec05f8d92a608f0fb34fd2453a647b43dcb777d (patch)
tree68c04086d48e458de3a9bd9f512da7a7604a027a /src/Builtins.hs
parent23d7d88e21eca2acf8649df9dc7b0a4c3277a8a4 (diff)
Separate Builtins module
Diffstat (limited to 'src/Builtins.hs')
-rw-r--r--src/Builtins.hs49
1 files changed, 49 insertions, 0 deletions
diff --git a/src/Builtins.hs b/src/Builtins.hs
new file mode 100644
index 0000000..71401d8
--- /dev/null
+++ b/src/Builtins.hs
@@ -0,0 +1,49 @@
+module Builtins where
+
+import qualified Data.Map as M
+import Control.Monad.Except
+import Types
+
+builtinEnv :: Env
+builtinEnv = M.fromList [
+ ("+", builtinAdd2),
+ ("-", builtinSubtract2),
+ ("head", builtinHead),
+ ("tail", builtinTail)
+ ]
+
+builtinAdd2 :: AST
+builtinAdd2 =
+ let outer ast1 = do
+ (ASTInteger a) <- assertIsASTInteger ast1
+ let inner ast2 = do
+ (ASTInteger b) <- assertIsASTInteger ast2
+ return $ ASTInteger $ a + b
+ return $ ASTFunction $ inner
+ in ASTFunction outer
+
+builtinSubtract2 :: AST
+builtinSubtract2 =
+ let outer ast1 = do
+ (ASTInteger a) <- assertIsASTInteger ast1
+ let inner ast2 = do
+ (ASTInteger b) <- assertIsASTInteger ast2
+ return $ ASTInteger $ a - b
+ return $ ASTFunction $ inner
+ in ASTFunction outer
+
+builtinHead :: AST
+builtinHead =
+ let outer ast = do
+ (ASTVector vec) <- assertIsASTVector ast
+ when (length vec == 0) $ throwError $ LException $ "head of empty vector"
+ return $ head vec
+ in ASTFunction outer
+
+builtinTail :: AST
+builtinTail =
+ let outer ast = do
+ (ASTVector vec) <- assertIsASTVector ast
+ when (length vec == 0) $ throwError $ LException $ "tail of empty vector"
+ return $ ASTVector $ tail vec
+ in ASTFunction outer \ No newline at end of file