From 5770838213b7b1eec90642c47c94cf730d35726f Mon Sep 17 00:00:00 2001 From: mryouse Date: Tue, 10 May 2022 03:12:57 +0000 Subject: add '+' functions, fix lexer so they're not numeric --- lexer.py | 9 +++++++++ std.py | 24 +++++++++++++++++------- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/lexer.py b/lexer.py index 913a9aa..11e02f1 100644 --- a/lexer.py +++ b/lexer.py @@ -125,6 +125,15 @@ def lex(inp, tokens): return lex(inp[1:], tokens) # numbers elif nxt in list(DIGITS) or nxt in ("+", "-", "."): + # + and - are symbols, too + if nxt in ("+", "-"): + after = peek(inp[1:]) + if after not in DIGITS: # parse a symbol + token, remainder = lex_symbol(inp) + if peek(remainder) not in (None, CLOSE_PAREN, " "): + raise Exception("spaces required between tokens") + tokens.append(token) + return lex(remainder, tokens) token, remainder = lex_number(inp) tokens.append(token) return lex(remainder, tokens) diff --git a/std.py b/std.py index dfa1cef..96988f3 100644 --- a/std.py +++ b/std.py @@ -15,18 +15,28 @@ def std_print(arg): #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"] = { - print_string.func.in_sig(): print_string - } + 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) - STD["exit"] = { - exit_.func.in_sig(): exit_, - exit_int.func.in_sig(): 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() -- cgit v1.2.3