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 std_add(arg1, arg2): typ = NebType.INT if NebType.FLOAT in (arg1.type_, arg2.type_): typ = NebType.FLOAT return NebLiteral(typ, arg1.value + arg2.value) def build_sig_dict(*args): return {arg.func.in_sig(): arg for arg in args} def build_std(): print_string = FuncImpl(NebFunction("print", [NebType.STRING], NebType.BOOL), std_print) STD["print"] = build_sig_dict(print_string) exit_ = FuncImpl(NebFunction("exit", [], NebType.BOOL), std_exit) exit_int = FuncImpl(NebFunction("exit", [NebType.INT], NebType.BOOL), std_exit) STD["exit"] = build_sig_dict(exit_, exit_int) # arithmetic add_int_int = FuncImpl(NebFunction("+", [NebType.INT, NebType.INT], NebType.INT), std_add) add_int_float = FuncImpl(NebFunction("+", [NebType.INT, NebType.FLOAT], NebType.FLOAT), std_add) add_float_int = FuncImpl(NebFunction("+", [NebType.FLOAT, NebType.INT], NebType.FLOAT), std_add) add_float_float = FuncImpl(NebFunction("+", [NebType.FLOAT, NebType.FLOAT], NebType.FLOAT), std_add) STD["+"] = build_sig_dict(add_int_int, add_int_float, add_float_int, add_float_float) build_std()