aboutsummaryrefslogtreecommitdiffstats
path: root/src/Builtins.hs
diff options
context:
space:
mode:
authorJan Tuomi <jan.tuomi@valuemotive.com>2022-09-24 20:48:21 +0300
committerJan Tuomi <jans.tuomi@gmail.com>2022-12-05 14:21:53 +0200
commita2e9732fb8f432e89d820601b1035c36c69e9340 (patch)
tree318f863c032b7573ae1b156adef4feed3965a013 /src/Builtins.hs
parent4e94267a9f97865ebeb19bcbe4945322dd74a1b3 (diff)
Add builtins
Diffstat (limited to 'src/Builtins.hs')
-rw-r--r--src/Builtins.hs23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/Builtins.hs b/src/Builtins.hs
index ce47ac9..e7f0cda 100644
--- a/src/Builtins.hs
+++ b/src/Builtins.hs
@@ -8,6 +8,8 @@ builtinEnv :: Env
builtinEnv = M.fromList [
("+", builtinAdd2),
("-", builtinSubtract2),
+ ("*", builtinMultiply2),
+ ("/", builtinDivide2),
("head", builtinHead),
("tail", builtinTail),
("prepend", builtinPrepend)
@@ -33,6 +35,27 @@ builtinSubtract2 =
return $ ASTFunction $ inner
in ASTFunction outer
+builtinMultiply2 :: AST
+builtinMultiply2 =
+ 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
+
+builtinDivide2 :: AST
+builtinDivide2 =
+ let outer _ ast1 = do
+ (ASTInteger a) <- assertIsASTInteger ast1
+ let inner _ ast2 = do
+ (ASTInteger b) <- assertIsASTInteger ast2
+ when (b == 0) $ throwError $ LException $ "division by zero"
+ return $ ASTInteger $ a `div` b
+ return $ ASTFunction $ inner
+ in ASTFunction outer
+
builtinHead :: AST
builtinHead =
let outer _ ast = do