🐐 String uppercase/lowercase functions

This commit is contained in:
2026-04-24 18:10:50 +02:00
parent 37050e3e3b
commit dbaf01b6a3
10 changed files with 109 additions and 3 deletions
+16
View File
@@ -1445,6 +1445,22 @@ class BuiltIn(Node):
if len(params) != 1:
raise CentvrionError("LITTERA takes exactly I argument")
return vtable, ValStr(make_string(params[0], magnvm, svbnvlla))
case "MAIVSCVLA":
if len(params) != 1:
raise CentvrionError("MAIVSCVLA takes exactly I argument")
val = params[0]
if not isinstance(val, ValStr):
raise CentvrionError(f"MAIVSCVLA expects a string, got {type(val).__name__}")
s = val.value()
return vtable, ValStr("".join(chr(ord(c) - 32) if "a" <= c <= "z" else c for c in s))
case "MINVSCVLA":
if len(params) != 1:
raise CentvrionError("MINVSCVLA takes exactly I argument")
val = params[0]
if not isinstance(val, ValStr):
raise CentvrionError(f"MINVSCVLA expects a string, got {type(val).__name__}")
s = val.value()
return vtable, ValStr("".join(chr(ord(c) + 32) if "A" <= c <= "Z" else c for c in s))
case "CLAVES":
if not isinstance(params[0], ValDict):
raise CentvrionError("CLAVES requires a dict")
+14
View File
@@ -240,6 +240,20 @@ def _emit_builtin(node, ctx):
case "LITTERA":
lines.append(f"CentValue {tmp} = cent_littera({param_vars[0]});")
case "MAIVSCVLA":
if len(param_vars) != 1:
lines.append(f'cent_runtime_error("MAIVSCVLA takes exactly I argument");')
lines.append(f"CentValue {tmp} = cent_null();")
else:
lines.append(f"CentValue {tmp} = cent_maivscvla({param_vars[0]});")
case "MINVSCVLA":
if len(param_vars) != 1:
lines.append(f'cent_runtime_error("MINVSCVLA takes exactly I argument");')
lines.append(f"CentValue {tmp} = cent_null();")
else:
lines.append(f"CentValue {tmp} = cent_minvscvla({param_vars[0]});")
case "FORTVITVS_NVMERVS":
if not ctx.has_module("FORS"):
lines.append('cent_runtime_error("FORS module required for FORTVITVS_NVMERVS");')
+24
View File
@@ -671,6 +671,30 @@ CentValue cent_littera(CentValue v) {
return cent_str(cent_make_string(v));
}
CentValue cent_maivscvla(CentValue v) {
if (v.type != CENT_STR) cent_type_error("'MAIVSCVLA' requires a string");
size_t len = strlen(v.sval);
char *out = cent_arena_alloc(cent_arena, len + 1);
for (size_t i = 0; i < len; i++) {
unsigned char c = (unsigned char)v.sval[i];
out[i] = (c >= 'a' && c <= 'z') ? (char)(c - ('a' - 'A')) : (char)c;
}
out[len] = '\0';
return cent_str(out);
}
CentValue cent_minvscvla(CentValue v) {
if (v.type != CENT_STR) cent_type_error("'MINVSCVLA' requires a string");
size_t len = strlen(v.sval);
char *out = cent_arena_alloc(cent_arena, len + 1);
for (size_t i = 0; i < len; i++) {
unsigned char c = (unsigned char)v.sval[i];
out[i] = (c >= 'A' && c <= 'Z') ? (char)(c + ('a' - 'A')) : (char)c;
}
out[len] = '\0';
return cent_str(out);
}
CentValue cent_typvs(CentValue v) {
switch (v.type) {
case CENT_INT: return cent_str("NVMERVS");
@@ -228,6 +228,8 @@ CentValue cent_avdi(void); /* AVDI */
CentValue cent_avdi_numerus(void); /* AVDI_NVMERVS */
CentValue cent_longitudo(CentValue v); /* LONGITVDO */
CentValue cent_littera(CentValue v); /* LITTERA */
CentValue cent_maivscvla(CentValue v); /* MAIVSCVLA */
CentValue cent_minvscvla(CentValue v); /* MINVSCVLA */
CentValue cent_fortuitus_numerus(CentValue lo, CentValue hi); /* FORTVITVS_NVMERVS */
CentValue cent_fortuita_electionis(CentValue lst); /* FORTVITA_ELECTIO */
CentValue cent_decimatio(CentValue lst); /* DECIMATIO */
+3 -1
View File
@@ -56,6 +56,8 @@ builtin_tokens = [("BUILTIN", i) for i in [
"FORTVITA_ELECTIO",
"LITTERA",
"LONGITVDO",
"MAIVSCVLA",
"MINVSCVLA",
"NVMERVS",
"ORDINA",
"SEMEN",
@@ -108,8 +110,8 @@ whitespace_tokens = [
]
all_tokens = (
keyword_tokens +
builtin_tokens +
keyword_tokens +
module_tokens +
data_tokens +
symbol_tokens +