Browse Source

add a few more numeric builtins

Getty Ritter 1 year ago
parent
commit
7aa59dd51a
1 changed files with 26 additions and 0 deletions
  1. 26 0
      src/builtins.rs

+ 26 - 0
src/builtins.rs

@@ -69,6 +69,19 @@ pub fn builtins() -> Vec<BuiltinFunc> {
             }),
         },
 
+        BuiltinFunc {
+            name: "add",
+            callback: Box::new(|state: &State, exprs: &[ExprRef], env: &Env| -> Result<Value, Error> {
+                if let [x, y] = exprs {
+                    let x = state.eval(*x, env)?.as_num(&state.ast.borrow())?;
+                    let y = state.eval(*y, env)?.as_num(&state.ast.borrow())?;
+                    Ok(Value::Lit(Literal::Num(x + y)))
+                } else {
+                    bail!("`add`: expected 2 arguments, got {}", exprs.len());
+                }
+            }),
+        },
+
         BuiltinFunc {
             name: "sub",
             callback: Box::new(|state: &State, exprs: &[ExprRef], env: &Env| -> Result<Value, Error> {
@@ -82,6 +95,19 @@ pub fn builtins() -> Vec<BuiltinFunc> {
             }),
         },
 
+        BuiltinFunc {
+            name: "mul",
+            callback: Box::new(|state: &State, exprs: &[ExprRef], env: &Env| -> Result<Value, Error> {
+                if let [x, y] = exprs {
+                    let x = state.eval(*x, env)?.as_num(&state.ast.borrow())?;
+                    let y = state.eval(*y, env)?.as_num(&state.ast.borrow())?;
+                    Ok(Value::Lit(Literal::Num(x * y)))
+                } else {
+                    bail!("`mul`: expected 2 arguments, got {}", exprs.len());
+                }
+            }),
+        },
+
         BuiltinFunc {
             name: "tuple/len",
             callback: Box::new(|state: &State, exprs: &[ExprRef], env: &Env| -> Result<Value, Error> {