summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOwen Jacobson <owen@grimoire.ca>2017-11-13 21:10:24 -0500
committerOwen Jacobson <owen@grimoire.ca>2017-11-13 21:10:24 -0500
commit9e48ca28fe335f07d00d4c4469255ecde8df7325 (patch)
tree8fa388d4204602e44d72daf3aa9b1cf33fd9fea5
parentedb9ab3157eb4cdb5e03e106c400b4d7b22c4455 (diff)
Flatten arguments inside the evaluator.
This makes it a bit easier to pass and return multiple values, and cuts down on the number of extra tuples created and dismantled.
-rw-r--r--actinide/evaluator.py33
1 files changed, 18 insertions, 15 deletions
diff --git a/actinide/evaluator.py b/actinide/evaluator.py
index bf97911..af25908 100644
--- a/actinide/evaluator.py
+++ b/actinide/evaluator.py
@@ -46,7 +46,7 @@ from . import types as t
# forms which do not produce a value, this returns the empty tuple.
def run(continuation, environment, args=()):
while continuation is not None:
- continuation, environment, args = continuation(environment, *args)
+ continuation, environment, *args = continuation(environment, *args)
return args
# ## FLAT CONTINUATIONS
@@ -57,13 +57,13 @@ def run(continuation, environment, args=()):
# Returns a continuation which yields a single value, verbatim, and chains to a
# known target continuation. This implements evaluation for literals.
def literal(value, continuation):
- return lambda environment: (continuation, environment, (value,))
+ return lambda environment: (continuation, environment, value)
# Returns a continuation which looks up a symbol in an environment, yields the
# result, and chains to a known target continuation. This implements evaluation
# for variable lookups.
def symbol(symb, continuation):
- return lambda environment: (continuation, environment, (environment.find(symb),))
+ return lambda environment: (continuation, environment, environment.find(symb))
# Returns a continuation which yields a newly-created procedure, and chains to a
# known target continuation. This implements evaluation for the tail of a lambda
@@ -74,7 +74,7 @@ def lambda_(defn, symbols, continuation):
body = t.head(t.tail(defn))
def lambda__(environment):
proc = t.Procedure(body, formals, environment, symbols)
- return continuation, environment, (proc,)
+ return (continuation, environment, proc)
return lambda__
# Returns a continuation which takes a value and binds that value to a symbol in
@@ -83,7 +83,7 @@ def lambda_(defn, symbols, continuation):
def bind(symbol, continuation):
def bind_(environment, value):
environment.define(symbol, value)
- return (continuation, environment, ())
+ return (continuation, environment)
return bind_
# Returns a continuation which takes a value and returns one of two known target
@@ -92,14 +92,14 @@ def bind(symbol, continuation):
# this chains to the `on_false` continuation. In either case, the continuation
# not chained to is discarded.
def branch(on_true, on_false):
- return lambda environment, val: (on_true if val else on_false, environment, ())
+ return lambda environment, val: (on_true if val else on_false, environment)
# Returns a continuation which receives values, and appends them to the values
# passed to this factory, before chaining to a known target continuation. This
# implements intermediate evaluation of list forms, where part of the list is
# already known, as well as splicing for forms that yield multiple values.
def append(args, continuation):
- return lambda environment, *tail: (continuation, environment, (*args, *tail))
+ return lambda environment, *tail: (continuation, environment, *args, *tail)
# Transforms a continuation which should receive function results into a
# function call continuation. A function call continuation receives a function
@@ -117,11 +117,11 @@ def invoke(continuation):
call_env = fn.invocation_environment(*args)
call_cont = fn.continuation
return_cont = tail_graft(continuation, environment, call_cont)
- return return_cont, call_env, ()
+ return (return_cont, call_env)
def builtin(environment, fn, *args):
result = fn(*args)
- return (continuation, environment, result)
+ return (continuation, environment, *result)
return invoke_
# Continuation transformer. Given a guarded continuation, and a graft
@@ -140,10 +140,10 @@ def tail_graft(continuation, environment, guarded):
return guarded
def guard(env, *args):
- next, env, args = guarded(env, *args)
+ next, env, *args = guarded(env, *args)
if next is None:
- return continuation, environment, args
- return tail_graft(continuation, environment, next), env, args
+ return (continuation, environment, *args)
+ return (tail_graft(continuation, environment, next), env, *args)
return guard
@@ -188,7 +188,7 @@ def define(value, symbols, continuation):
bind_cont = bind(symb, continuation)
eval_cont = eval(expr, symbols, bind_cont)
- return lambda environment: (eval_cont, environment, ())
+ return lambda environment: (eval_cont, environment)
# Returns a continuation which fully evaluates an `(if cond if-true if-false)`
# form, before chaining to a known target continuation. First, the returned
@@ -214,6 +214,9 @@ def if_(value, symbols, continuation):
# calling `apply` on the tail of the list.
def apply(list, symbols, continuation):
if t.nil_p(list):
- return lambda environment, *args: (continuation, environment, args)
+ return lambda environment, *args: (continuation, environment, *args)
tail_cont = apply(t.tail(list), symbols, continuation)
- return lambda environment, *args: (eval(t.head(list), symbols, append(args, tail_cont)), environment, ())
+ return lambda environment, *args: (
+ eval(t.head(list), symbols, append(args, tail_cont)),
+ environment,
+ )