From 9ac2c37464ae95d78e9d410fa8ec71543fe46d8e Mon Sep 17 00:00:00 2001 From: mryouse Date: Fri, 13 May 2022 22:37:43 +0000 Subject: have arithmetic functions take many numbers --- std.py | 60 ++++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 42 insertions(+), 18 deletions(-) (limited to 'std.py') diff --git a/std.py b/std.py index 5e41935..7d69345 100644 --- a/std.py +++ b/std.py @@ -44,26 +44,50 @@ def std_debug_off(): return NebBool(True) # math -def std_add(arg1, arg2): - res = arg1.value + arg2.value - if isinstance(arg1, NebFloat) or isinstance(arg2, NebFloat): - return NebFloat(res) +def std_add(arg, rest): + float_type = True if isinstance(arg, NebFloat) else False + if isinstance(rest, NebNumber): + acc = arg.value + rest.value + float_type = float_type or isinstance(rest, NebFloat) else: - return NebInt(res) + acc = arg.value + for item in rest: + acc += item.value + float_type = float_type or isinstance(item, NebFloat) + if float_type: + return NebFloat(acc) + else: + return NebInt(acc) -def std_subtract(arg1, arg2): - res = arg1.value - arg2.value - if isinstance(arg1, NebFloat) or isinstance(arg2, NebFloat): - return NebFloat(res) +def std_subtract(arg, rest): + float_type = True if isinstance(arg, NebFloat) else False + if isinstance(rest, NebNumber): + acc = arg.value - rest.value + float_type = float_type or isinstance(rest, NebFloat) + else: + acc = arg.value + for item in rest: + acc -= item.value + float_type = float_type or isinstance(item, NebFloat) + if float_type: + return NebFloat(acc) else: - return NebInt(res) + return NebInt(acc) -def std_multiply(arg1, arg2): - res = arg1.value * arg2.value - if isinstance(arg1, NebFloat) or isinstance(arg2, NebFloat): - return NebFloat(res) +def std_multiply(arg, rest): + float_type = True if isinstance(arg, NebFloat) else False + if isinstance(rest, NebNumber): + acc = arg.value * rest.value + float_type = float_type or isinstance(rest, NebFloat) + else: + acc = arg.value + for item in rest: + acc *= item.value + float_type = float_type or isinstance(item, NebFloat) + if float_type: + return NebFloat(acc) else: - return NebInt(res) + return NebInt(acc) # strings def std_concat(arg, rest): @@ -234,13 +258,13 @@ def build_std(): STD["debug-off"] = [debug_off] # arithmetic - add = FuncImpl(NebFunction("+", [NebNumber, NebNumber], NebNumber), std_add) + add = FuncImpl(NebFunction("+", [NebNumber, NebNumber], NebNumber, [NebNumber]), std_add) STD["+"] = [add] - subtract = FuncImpl(NebFunction("-", [NebNumber, NebNumber], NebNumber), std_subtract) + subtract = FuncImpl(NebFunction("-", [NebNumber, NebNumber], NebNumber, [NebNumber]), std_subtract) STD["-"] = [subtract] - multiply = FuncImpl(NebFunction("*", [NebNumber, NebNumber], NebNumber), std_multiply) + multiply = FuncImpl(NebFunction("*", [NebNumber, NebNumber], NebNumber, [NebNumber]), std_multiply) STD["*"] = [multiply] # strings -- cgit v1.2.3