diff options
| -rw-r--r-- | runner.py | 29 | ||||
| -rw-r--r-- | std.py | 32 | ||||
| -rw-r--r-- | tokens.py | 1 | 
3 files changed, 42 insertions, 20 deletions
| @@ -1,15 +1,6 @@  from tokens import * +from std import STD -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: @@ -24,19 +15,17 @@ def evaluate(items, pop):          pop = nxt.value          return evaluate(items[1:], pop)      elif isinstance(nxt, NebSymbol): -        if not nxt.name in std: +        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"]) +        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: +        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)) +        this_func = STD[nxt.symbol.name] +        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)      else:          raise Exception("expected a literal or an expression") @@ -0,0 +1,32 @@ +from tokens import * +import sys +from collections import namedtuple + +FuncImpl = namedtuple("FuncImpl", ("func", "impl")) + +STD = {} + +def std_exit(status=0): +    sys.exit(status) +    return NebLiteral(NebType.BOOL, True) + +def std_print(arg): +    print(arg.value) +    #return []  # TODO this should return empty list +    return NebLiteral(NebType.BOOL, True) + +def build_std(): +    print_string = FuncImpl(NebFunction("print", [NebType.STRING], NebType.BOOL), std_print) +    STD["print"] = { +        print_string.func.in_sig(): print_string +    } + +    exit_ = FuncImpl(NebFunction("exit", [], NebType.BOOL), std_exit) +    exit_int = FuncImpl(NebFunction("exit", [NebType.INT], NebType.BOOL), std_exit) + +    STD["exit"] = { +        exit_.func.in_sig(): exit_, +        exit_int.func.in_sig(): exit_int +    } +     +build_std() @@ -61,3 +61,4 @@ class NebFunction(NebToken):      def sig(self):          return (self.in_sig() + " > " + self.out_sig()).strip() + | 
