diff options
| author | mryouse | 2022-05-10 02:07:40 +0000 |
|---|---|---|
| committer | mryouse | 2022-05-10 02:07:40 +0000 |
| commit | 7bed8de9b493ca2a2b13d6293db6bd81b73325ce (patch) | |
| tree | 7efa56cddb8a0cec4b4c03495874474b510c6a09 /runner.py | |
initial commit
Diffstat (limited to 'runner.py')
| -rw-r--r-- | runner.py | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/runner.py b/runner.py new file mode 100644 index 0000000..a2c7e99 --- /dev/null +++ b/runner.py @@ -0,0 +1,43 @@ +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") + |
