🐐 DECIMATIO
This commit is contained in:
@@ -957,6 +957,16 @@ class BuiltIn(Node):
|
||||
raise CentvrionError("SEMEN requires an integer seed")
|
||||
random.seed(seed)
|
||||
return vtable, ValNul()
|
||||
case "DECIMATIO":
|
||||
if "FORS" not in vtable["#modules"]:
|
||||
raise CentvrionError("Cannot use 'DECIMATIO' without module 'FORS'")
|
||||
if not isinstance(params[0], ValList):
|
||||
raise CentvrionError("DECIMATIO requires an array")
|
||||
arr = list(params[0].value())
|
||||
to_remove = len(arr) // 10
|
||||
for _ in range(to_remove):
|
||||
arr.pop(random.randint(0, len(arr) - 1))
|
||||
return vtable, ValList(arr)
|
||||
case "LONGITVDO":
|
||||
if isinstance(params[0], (ValList, ValStr)):
|
||||
return vtable, ValInt(len(params[0].value()))
|
||||
|
||||
@@ -186,6 +186,13 @@ def _emit_builtin(node, ctx):
|
||||
else:
|
||||
lines.append(f"CentValue {tmp} = cent_fortis_electionis({param_vars[0]});")
|
||||
|
||||
case "DECIMATIO":
|
||||
if not ctx.has_module("FORS"):
|
||||
lines.append('cent_runtime_error("FORS module required for DECIMATIO");')
|
||||
lines.append(f"CentValue {tmp} = cent_null();")
|
||||
else:
|
||||
lines.append(f"CentValue {tmp} = cent_decimatio({param_vars[0]});")
|
||||
|
||||
case "SEMEN":
|
||||
if not ctx.has_module("FORS"):
|
||||
lines.append('cent_runtime_error("FORS module required for SEMEN");')
|
||||
|
||||
@@ -533,6 +533,25 @@ CentValue cent_fortis_electionis(CentValue lst) {
|
||||
return lst.lval.items[rand() % lst.lval.len];
|
||||
}
|
||||
|
||||
CentValue cent_decimatio(CentValue lst) {
|
||||
if (lst.type != CENT_LIST)
|
||||
cent_type_error("'DECIMATIO' requires a list");
|
||||
int len = lst.lval.len;
|
||||
/* Copy the list so we can remove in-place */
|
||||
CentValue result = cent_list_new(len);
|
||||
for (int i = 0; i < len; i++)
|
||||
cent_list_push(&result, lst.lval.items[i]);
|
||||
int to_remove = result.lval.len / 10;
|
||||
for (int i = 0; i < to_remove; i++) {
|
||||
int idx = rand() % result.lval.len;
|
||||
/* Shift remaining elements left */
|
||||
for (int j = idx; j < result.lval.len - 1; j++)
|
||||
result.lval.items[j] = result.lval.items[j + 1];
|
||||
result.lval.len--;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void cent_semen(CentValue seed) {
|
||||
if (seed.type != CENT_INT)
|
||||
cent_type_error("'SEMEN' requires an integer seed");
|
||||
|
||||
@@ -177,6 +177,7 @@ CentValue cent_avdi_numerus(void); /* AVDI_NVMERVS */
|
||||
CentValue cent_longitudo(CentValue v); /* LONGITVDO */
|
||||
CentValue cent_fortis_numerus(CentValue lo, CentValue hi); /* FORTIS_NVMERVS */
|
||||
CentValue cent_fortis_electionis(CentValue lst); /* FORTIS_ELECTIONIS */
|
||||
CentValue cent_decimatio(CentValue lst); /* DECIMATIO */
|
||||
void cent_semen(CentValue seed); /* SEMEN */
|
||||
void cent_everro(void); /* EVERRO */
|
||||
|
||||
|
||||
@@ -39,6 +39,7 @@ keyword_tokens = [("KEYWORD_"+i, i) for i in [
|
||||
builtin_tokens = [("BUILTIN", i) for i in [
|
||||
"AVDI_NVMERVS",
|
||||
"AVDI",
|
||||
"DECIMATIO",
|
||||
"DICE",
|
||||
"EVERRO",
|
||||
"FORTIS_NVMERVS",
|
||||
|
||||
Reference in New Issue
Block a user