diff options
| -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 |
