🐐 Zipping
This commit is contained in:
@@ -1522,6 +1522,29 @@ class BuiltIn(Node):
|
||||
if idx < 1 or idx > len(items) + 1:
|
||||
raise CentvrionError(f"Index {idx} out of range for array of length {len(items)}")
|
||||
return vtable, ValList(items[:idx - 1] + [params[2]] + items[idx - 1:])
|
||||
case "NECTE":
|
||||
if len(params) != 2:
|
||||
raise CentvrionError("NECTE takes exactly II arguments")
|
||||
if not isinstance(params[0], ValList) or not isinstance(params[1], ValList):
|
||||
raise CentvrionError("NECTE requires two arrays")
|
||||
a, b = list(params[0].value()), list(params[1].value())
|
||||
if len(a) != len(b):
|
||||
raise CentvrionError("NECTE requires arrays of equal length")
|
||||
return vtable, ValList([ValList([x, y]) for x, y in zip(a, b)])
|
||||
case "IVNGE":
|
||||
if len(params) != 2:
|
||||
raise CentvrionError("IVNGE takes exactly II arguments")
|
||||
if not isinstance(params[0], ValList) or not isinstance(params[1], ValList):
|
||||
raise CentvrionError("IVNGE requires two arrays")
|
||||
keys, vals = list(params[0].value()), list(params[1].value())
|
||||
if len(keys) != len(vals):
|
||||
raise CentvrionError("IVNGE requires arrays of equal length")
|
||||
d = {}
|
||||
for k, v in zip(keys, vals):
|
||||
if not isinstance(k, (ValStr, ValInt)):
|
||||
raise CentvrionError("Dict keys must be strings or integers")
|
||||
d[k.value()] = v
|
||||
return vtable, ValDict(d)
|
||||
case "ORDINA":
|
||||
if not isinstance(params[0], ValList):
|
||||
raise CentvrionError("ORDINA requires an array")
|
||||
|
||||
@@ -322,6 +322,12 @@ def _emit_builtin(node, ctx):
|
||||
case "INSERE":
|
||||
lines.append(f"CentValue {tmp} = cent_insere({param_vars[0]}, {param_vars[1]}, {param_vars[2]});")
|
||||
|
||||
case "NECTE":
|
||||
lines.append(f"CentValue {tmp} = cent_necte({param_vars[0]}, {param_vars[1]});")
|
||||
|
||||
case "IVNGE":
|
||||
lines.append(f"CentValue {tmp} = cent_ivnge({param_vars[0]}, {param_vars[1]});")
|
||||
|
||||
case "EVERRE":
|
||||
lines.append("cent_everre();")
|
||||
lines.append(f"CentValue {tmp} = cent_null();")
|
||||
|
||||
@@ -898,6 +898,38 @@ CentValue cent_insere(CentValue lst, CentValue idx, CentValue v) {
|
||||
return result;
|
||||
}
|
||||
|
||||
CentValue cent_necte(CentValue a, CentValue b) {
|
||||
if (a.type != CENT_LIST || b.type != CENT_LIST)
|
||||
cent_type_error("'NECTE' requires two arrays");
|
||||
if (a.lval.len != b.lval.len)
|
||||
cent_runtime_error("'NECTE' requires arrays of equal length");
|
||||
int len = a.lval.len;
|
||||
CentValue result = cent_list_new(len);
|
||||
for (int i = 0; i < len; i++) {
|
||||
CentValue pair = cent_list_new(2);
|
||||
cent_list_push(&pair, a.lval.items[i]);
|
||||
cent_list_push(&pair, b.lval.items[i]);
|
||||
cent_list_push(&result, pair);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
CentValue cent_ivnge(CentValue keys, CentValue vals) {
|
||||
if (keys.type != CENT_LIST || vals.type != CENT_LIST)
|
||||
cent_type_error("'IVNGE' requires two arrays");
|
||||
if (keys.lval.len != vals.lval.len)
|
||||
cent_runtime_error("'IVNGE' requires arrays of equal length");
|
||||
int len = keys.lval.len;
|
||||
CentValue result = cent_dict_new(len);
|
||||
for (int i = 0; i < len; i++) {
|
||||
CentValue k = keys.lval.items[i];
|
||||
if (k.type != CENT_INT && k.type != CENT_STR)
|
||||
cent_runtime_error("Dict keys must be strings or integers");
|
||||
cent_dict_set(&result, k, vals.lval.items[i]);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Array helpers */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
@@ -242,6 +242,8 @@ CentValue cent_ordina(CentValue lst); /* ORDINA */
|
||||
CentValue cent_adde(CentValue lst, CentValue v); /* ADDE */
|
||||
CentValue cent_tolle(CentValue lst, CentValue idx); /* TOLLE */
|
||||
CentValue cent_insere(CentValue lst, CentValue idx, CentValue v); /* INSERE */
|
||||
CentValue cent_necte(CentValue a, CentValue b); /* NECTE */
|
||||
CentValue cent_ivnge(CentValue keys, CentValue vals); /* IVNGE */
|
||||
CentValue cent_lege(CentValue path); /* LEGE */
|
||||
void cent_scribe(CentValue path, CentValue content); /* SCRIBE */
|
||||
void cent_adivnge(CentValue path, CentValue content); /* ADIVNGE */
|
||||
|
||||
@@ -57,10 +57,12 @@ builtin_tokens = [("BUILTIN", i) for i in [
|
||||
"FORTVITVS_NVMERVS",
|
||||
"FORTVITA_ELECTIO",
|
||||
"INSERE",
|
||||
"IVNGE",
|
||||
"LITTERA",
|
||||
"LONGITVDO",
|
||||
"MAIVSCVLA",
|
||||
"MINVSCVLA",
|
||||
"NECTE",
|
||||
"NVMERVS",
|
||||
"ORDINA",
|
||||
"SEMEN",
|
||||
|
||||
Reference in New Issue
Block a user