diff options
Diffstat (limited to 'runner.py')
| -rw-r--r-- | runner.py | 33 | 
1 files changed, 20 insertions, 13 deletions
| @@ -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") | 
