aboutsummaryrefslogtreecommitdiffstats
path: root/src/Builtins.hs
diff options
context:
space:
mode:
authorJan Tuomi <jans.tuomi@gmail.com>2022-09-25 17:39:15 +0300
committerJan Tuomi <jans.tuomi@gmail.com>2022-12-05 14:21:53 +0200
commit218ad3f54ef0be7e0b2e288e543e6c0959ddd7e3 (patch)
treef35df8da7bb0ac3bdb36bde02376e776111e90e7 /src/Builtins.hs
parent09e74ca747d49c69eebd0013f12c9fd5b26c07d7 (diff)
Make builtinAdd2 polymorphic in numeric types
Diffstat (limited to 'src/Builtins.hs')
-rw-r--r--src/Builtins.hs19
1 files changed, 15 insertions, 4 deletions
diff --git a/src/Builtins.hs b/src/Builtins.hs
index dc6b44c..1ac532a 100644
--- a/src/Builtins.hs
+++ b/src/Builtins.hs
@@ -16,14 +16,25 @@ builtinEnv = M.fromList [
("prepend", builtinPrepend)
]
+-- maybe make a builtinBinaryFunction?
+-- builtinAdd2 = builtinBinaryFunction (ASTInteger a) (ASTInteger b) (\a b -> a + b)
builtinAdd2 :: AST
builtinAdd2 =
- let outer _ ast1 = do
- (ASTInteger a) <- assertIsASTInteger ast1
- let inner _ ast2 = do
- (ASTInteger b) <- assertIsASTInteger ast2
+ let outer :: LFunction
+ outer _ (ASTInteger a) = do
+ let inner :: LFunction
+ inner _ (ASTInteger b) =
return $ ASTInteger $ a + b
+ inner _ other = throwError $ LException $ "invalid argument to integer add: " ++ show other
+ return $ ASTFunction $ inner
+ outer _ (ASTDouble a) = do
+ let inner :: LFunction
+ inner _ (ASTDouble b) =
+ return $ ASTDouble $ a + b
+ inner _ other = throwError $ LException $ "invalid argument to double add: " ++ show other
return $ ASTFunction $ inner
+ outer _ other = throwError $ LException $ "non-numeric argument to add: " ++ show other
+
in ASTFunction outer
builtinSubtract2 :: AST