aboutsummaryrefslogtreecommitdiff
path: root/runner.py
diff options
context:
space:
mode:
authormryouse2022-05-11 03:01:27 +0000
committermryouse2022-05-11 03:01:27 +0000
commitd372f1cca2ae357feb2d4524087dd15646356faa (patch)
tree909b7e7848e1d36527e947fc74519d545239125e /runner.py
parent99a327a942028ec33e746a7ffbb2a3731cb4a370 (diff)
refactor: only evaluate expressions as needed
Diffstat (limited to 'runner.py')
-rw-r--r--runner.py33
1 files changed, 20 insertions, 13 deletions
diff --git a/runner.py b/runner.py
index 1023473..7de2666 100644
--- a/runner.py
+++ b/runner.py
@@ -7,6 +7,25 @@ def peek(inp):
return None
return inp[0]
+def evaluate_expression(expr):
+ if not expr.symbol.name in STD:
+ raise Exception(f"no such symbol: {expr.symbol.name}")
+ this_func = STD[expr.symbol.name]
+
+ # try to match the signature
+ in_sig = expr.in_sig()
+ if in_sig in this_func:
+ ret = this_func[expr.in_sig()].impl(*(expr.args))
+ return ret
+
+ # evaluate inner expressions, if possible/necessary
+ for idx, arg in enumerate(expr.args):
+ if arg.type_ == NebType.EXPR:
+ expr.args[idx] = evaluate_expression(arg)
+ return evaluate_expression(expr)
+
+ raise Exception(f"'{expr.symbol.name}' called with unknown signature: '{expr.in_sig()}'")
+
def evaluate(items, pop):
nxt = peek(items)
if nxt is None:
@@ -19,19 +38,7 @@ def evaluate(items, pop):
this_func = STD[nxt.name]
return evaluate(items[1:], this_func[list(this_func.keys())[0]].impl) # TODO show all
elif isinstance(nxt, NebExpression):
- if not nxt.symbol.name in STD:
- raise Exception(f"no such symbol: {nxt.symbol.name}")
- this_func = STD[nxt.symbol.name]
-
- # evaluate inner expressions
- for idx, arg in enumerate(nxt.args):
- if isinstance(arg, NebExpression):
- nxt.args[idx] = evaluate([arg], pop)
-
- if nxt.maybe_sig() not in this_func:
- raise Exception(f"'{nxt.symbol.name}' called with unknown signature: '{nxt.maybe_sig()}'")
- ret = this_func[nxt.maybe_sig()].impl(*(nxt.args))
- return evaluate(items[1:], ret)
+ return evaluate(items[1:], evaluate_expression(nxt))
else:
raise Exception("expected a literal or an expression")