aboutsummaryrefslogtreecommitdiff
path: root/interpreter.py
diff options
context:
space:
mode:
authormryouse2022-05-21 22:11:40 +0000
committermryouse2022-05-21 22:11:40 +0000
commit4eca7ce14c5ad11514a44de3cdadc38d32128810 (patch)
treeaa69c7614d3090178bfea74ea60cce3b182f4982 /interpreter.py
parent40b5285668629a54f5321b81a8c34539e92d601c (diff)
implement func
Diffstat (limited to 'interpreter.py')
-rw-r--r--interpreter.py17
1 files changed, 17 insertions, 0 deletions
diff --git a/interpreter.py b/interpreter.py
index 841f5cf..f133b1f 100644
--- a/interpreter.py
+++ b/interpreter.py
@@ -318,6 +318,23 @@ def interpretBranch(expr, env):
GLOBALS.register("branch", Builtin(interpretBranch))
+def interpretFunc(expr, env):
+ # func <name> (args) (exprs)
+ if len(expr.args) < 3:
+ raise Exception("'func' takes a name, arguments, and at least one expression")
+ if not isinstance(expr.args[0], Expr.Symbol):
+ raise Exception("'func' requires a string literal as a name")
+ name = expr.args[0].name # NOTE: we are not evaluating the name!!
+
+ # compose a lambda
+ lambda_expr = Expr.Nary("lambda", expr.args[1:])
+ func = interpretLambda(lambda_expr, env)
+
+ env.register(name, func)
+ return None
+
+GLOBALS.register("func", Builtin(interpretFunc))
+
def interpretEnv(expr, env_expr, env):
ev = evaluate(env_expr, env)
return ev # TODO more than this?