from tokens import * def std_print(arg): print(arg.value) #return [] # TODO this should return empty list return NebLiteral(NebType.BOOL, True) std = { "print": { "func": NebFunction("print", [NebType.STRING], NebType.BOOL), "impl": std_print } } def peek(inp): if len(inp) == 0: return None return inp[0] def evaluate(items, pop): nxt = peek(items) if nxt is None: return pop elif isinstance(nxt, NebLiteral): pop = nxt.value return evaluate(items[1:], pop) elif isinstance(nxt, NebSymbol): if not nxt.name in std: raise Exception(f"no such symbol: '{nxt.name}'") this_func = std[nxt.name] return evaluate(items[1:], this_func["impl"]) 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] #expected_sig = " ".join(x.type_.name for x in nxt.args) #if this_func["func"].in_sig() != expected_sig: if this_func["func"].in_sig() != nxt.maybe_sig(): raise Exception(f"{nxt.symbol.name} expects '{this_func['func'].in_sig()}', got '{nxt.maybe_sig()}'") ret = this_func["impl"](*(nxt.args)) return evaluate(items[1:], ret) else: raise Exception("expected a literal or an expression")