diff options
| author | Jan Tuomi <jan.tuomi@valuemotive.com> | 2022-09-24 20:48:21 +0300 |
|---|---|---|
| committer | Jan Tuomi <jans.tuomi@gmail.com> | 2022-12-05 14:21:53 +0200 |
| commit | a2e9732fb8f432e89d820601b1035c36c69e9340 (patch) | |
| tree | 318f863c032b7573ae1b156adef4feed3965a013 | |
| parent | 4e94267a9f97865ebeb19bcbe4945322dd74a1b3 (diff) | |
Add builtins
| -rw-r--r-- | examples/test.lisp | 3 | ||||
| -rw-r--r-- | src/Builtins.hs | 23 |
2 files changed, 26 insertions, 0 deletions
diff --git a/examples/test.lisp b/examples/test.lisp index 6608fae..276dcd5 100644 --- a/examples/test.lisp +++ b/examples/test.lisp @@ -36,3 +36,6 @@ (fibo (- n 2)))))) (fibo 10) + +(let mod (\[n k] + (- n (* k (/ n k))))) 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 |
