🐐 SCINDE
This commit is contained in:
@@ -1328,6 +1328,18 @@ class BuiltIn(Node):
|
||||
except re.error as e:
|
||||
raise CentvrionError(f"Invalid regex: {e}")
|
||||
return vtable, ValStr(result)
|
||||
case "SCINDE":
|
||||
string = params[0]
|
||||
delimiter = params[1]
|
||||
if not isinstance(string, ValStr) or not isinstance(delimiter, ValStr):
|
||||
raise CentvrionError("SCINDE requires two strings")
|
||||
s = string.value()
|
||||
d = delimiter.value()
|
||||
if d == "":
|
||||
parts = [ValStr(c) for c in s]
|
||||
else:
|
||||
parts = [ValStr(p) for p in s.split(d)]
|
||||
return vtable, ValList(parts)
|
||||
case "PETE":
|
||||
if "RETE" not in vtable["#modules"]:
|
||||
raise CentvrionError("Cannot use 'PETE' without module 'RETE'")
|
||||
|
||||
@@ -303,6 +303,9 @@ def _emit_builtin(node, ctx):
|
||||
case "SVBSTITVE":
|
||||
lines.append(f"CentValue {tmp} = cent_svbstitve({param_vars[0]}, {param_vars[1]}, {param_vars[2]});")
|
||||
|
||||
case "SCINDE":
|
||||
lines.append(f"CentValue {tmp} = cent_scinde({param_vars[0]}, {param_vars[1]});")
|
||||
|
||||
case "PETE":
|
||||
if not ctx.has_module("RETE"):
|
||||
lines.append('cent_runtime_error("RETE module required for PETE");')
|
||||
|
||||
@@ -1015,6 +1015,40 @@ CentValue cent_svbstitve(CentValue pattern, CentValue replacement, CentValue tex
|
||||
return cent_str(result);
|
||||
}
|
||||
|
||||
CentValue cent_scinde(CentValue str, CentValue delim) {
|
||||
if (str.type != CENT_STR || delim.type != CENT_STR)
|
||||
cent_type_error("'SCINDE' requires two strings");
|
||||
const char *s = str.sval;
|
||||
const char *d = delim.sval;
|
||||
size_t dlen = strlen(d);
|
||||
CentValue result = cent_list_new(8);
|
||||
if (dlen == 0) {
|
||||
/* empty delimiter: split into individual characters */
|
||||
for (const char *p = s; *p; p++) {
|
||||
char *buf = cent_arena_alloc(cent_arena, 2);
|
||||
buf[0] = *p;
|
||||
buf[1] = '\0';
|
||||
cent_list_push(&result, cent_str(buf));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
const char *cursor = s;
|
||||
for (;;) {
|
||||
const char *found = strstr(cursor, d);
|
||||
if (!found) {
|
||||
cent_list_push(&result, cent_str(cursor));
|
||||
break;
|
||||
}
|
||||
size_t len = found - cursor;
|
||||
char *buf = cent_arena_alloc(cent_arena, len + 1);
|
||||
memcpy(buf, cursor, len);
|
||||
buf[len] = '\0';
|
||||
cent_list_push(&result, cent_str(buf));
|
||||
cursor = found + dlen;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Networking (RETE) */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
@@ -234,6 +234,7 @@ void cent_scribe(CentValue path, CentValue content); /* SCRIBE */
|
||||
void cent_adivnge(CentValue path, CentValue content); /* ADIVNGE */
|
||||
CentValue cent_qvaere(CentValue pattern, CentValue text); /* QVAERE */
|
||||
CentValue cent_svbstitve(CentValue pattern, CentValue replacement, CentValue text); /* SVBSTITVE */
|
||||
CentValue cent_scinde(CentValue str, CentValue delim); /* SCINDE */
|
||||
CentValue cent_pete(CentValue url); /* PETE */
|
||||
void cent_petitvr(CentValue path, CentValue handler, CentScope scope); /* PETITVR */
|
||||
void cent_avscvlta(CentValue port); /* AVSCVLTA */
|
||||
|
||||
@@ -60,6 +60,7 @@ builtin_tokens = [("BUILTIN", i) for i in [
|
||||
"ADIVNGE",
|
||||
"QVAERE",
|
||||
"SVBSTITVE",
|
||||
"SCINDE",
|
||||
"PETE",
|
||||
"PETITVR",
|
||||
"AVSCVLTA"
|
||||
|
||||
Reference in New Issue
Block a user