diff options
| author | mryouse | 2022-05-13 22:19:16 +0000 |
|---|---|---|
| committer | mryouse | 2022-05-13 22:19:16 +0000 |
| commit | 9641657502b966ff3068688eb45885da41926ae8 (patch) | |
| tree | 2b84ab49ee2f2ee4e93b855011929e7e5211c316 /std.py | |
| parent | c6ce5c7f9f34b95504cb3286a677e106fa99c8e7 (diff) | |
initial commit of 'many'
Diffstat (limited to 'std.py')
| -rw-r--r-- | std.py | 48 |
1 files changed, 42 insertions, 6 deletions
@@ -66,8 +66,12 @@ def std_multiply(arg1, arg2): return NebInt(res) # strings -def std_concat(arg1, arg2): - return NebString(f"{arg1.value}{arg2.value}") +def std_concat(arg, rest): + out = f"{arg.value}" + if isinstance(rest, NebString): + return NebString(f"{out}{rest.value}") + else: + return NebString(f"{out}" + "".join(f"{item.value}" for item in rest)) # flow control def std_if(cond, t_branch, f_branch=None): @@ -162,17 +166,49 @@ def evaluate_expression(expr): validated = 0 in_sig = expr.args for value in this_func: - sig = value.func.args validated = 0 - if len(sig) != len(in_sig): + sig = value.func.args + + # if many is defined, it can take one or more of them + many = value.func.many + + # if we need exact arguments, the signatures must match length + if many is None and len(sig) != len(in_sig): + continue + # if we have "many", must have at least the exact length + elif len(in_sig) < len(sig): continue + + # loop through explicits for idx in range(len(sig)): if isinstance(in_sig[idx], sig[idx]): validated += 1 - if validated == len(sig): + + # return if we've found it + if validated == len(in_sig): ret = value.impl(*(expr.args)) return ret + # loop through the remainder of in_sig + many_idx = 0 + first_args = in_sig[0:len(sig)] + rest_args = in_sig[len(sig):] + for arg in rest_args: + if isinstance(arg, many[many_idx]): + validated += 1 + if len(many) - 1 == many_idx: + many_idx = 0 + else: + many_idx += 1 + + if validated == len(in_sig): + # move end of first list to beginning of second list + new_args = first_args[:-1] + new_rest = [first_args[-1]] + rest_args + new_args.append(new_rest) + ret = value.impl(*(new_args)) + return ret + # evaluate inner expressions, if possible/necessary for idx, arg in enumerate(expr.args): if isinstance(arg, NebExpression): @@ -208,7 +244,7 @@ def build_std(): STD["*"] = [multiply] # strings - concat_string_string = FuncImpl(NebFunction("concat", [NebString, NebString], NebString), std_concat) + concat_string_string = FuncImpl(NebFunction("concat", [NebString, NebString], NebString, [NebString]), std_concat) STD["concat"] = [concat_string_string] # flow control |
