Implement CHARVAL function (#1701)

This commit is contained in:
Rangi
2025-06-12 17:21:12 -04:00
committed by GitHub
parent fa9e29e4ce
commit 089e366ddc
7 changed files with 82 additions and 7 deletions

View File

@@ -287,6 +287,7 @@
%token OP_CHARLEN "CHARLEN"
%token OP_CHARSIZE "CHARSIZE"
%token OP_CHARSUB "CHARSUB"
%token OP_CHARVAL "CHARVAL"
%token OP_COS "COS"
%token OP_DEF "DEF"
%token OP_FDIV "FDIV"
@@ -1580,6 +1581,24 @@ relocexpr_no_str:
}
$$.makeNumber(charSize);
}
| OP_CHARVAL LPAREN string COMMA iconst RPAREN {
if (size_t len = charmap_CharSize($3); len != 0) {
uint32_t idx = adjustNegativeIndex($5, len, "CHARVAL");
if (std::optional<int32_t> val = charmap_CharValue($3, idx); val.has_value()) {
$$.makeNumber(*val);
} else {
warning(
WARNING_BUILTIN_ARG,
"CHARVAL: Index %" PRIu32 " is past the end of the character mapping\n",
idx
);
$$.makeNumber(0);
}
} else {
::error("CHARVAL: No character mapping for \"%s\"\n", $3.c_str());
$$.makeNumber(0);
}
}
| LPAREN relocexpr RPAREN {
$$ = std::move($2);
}