aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author_ov4 <ov4@ov4.org>2026-10-20 11:04:06 +0000
committer_ov4 <ov4@ov4.org>2026-10-20 11:04:06 +0000
commit8975d167c849f54664be8d5da418cd909694d26f (patch)
treeddefc52af631d09157c91a17046c333da8acec91
parent079764b8f04f4ae69a3a014224a45a04a157edad (diff)
feat(library): add experimental string CHR function
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
-rw-r--r--pseudocode-lib/README.md2
-rw-r--r--pseudocode-lib/_exp_string.cc5
-rw-r--r--pseudocode-lib/_exp_string.cps6
-rw-r--r--tests/library/exp-string/expected.txt4
-rw-r--r--tests/library/exp-string/notes.md2
-rw-r--r--tests/library/exp-string/program.cps8
6 files changed, 27 insertions, 0 deletions
diff --git a/pseudocode-lib/README.md b/pseudocode-lib/README.md
index 665e2ed..90fab0e 100644
--- a/pseudocode-lib/README.md
+++ b/pseudocode-lib/README.md
@@ -30,3 +30,5 @@ The library provides `&`, `_EXP_STRING_NUM_TO_STR` (INTEGER/REAL), `_EXP_STRING_
`_EXP_STRING_UCASE(S : _EXP_STRING)` returns a new `_EXP_STRING` with letters converted to upper case. Its `(Ch : CHAR)` overload returns CHAR, following guide section 5.5. Nonletters and embedded NUL bytes are preserved; the source is unchanged. Use ASCII for predictable case conversion.
`_EXP_STRING_ASC(S : _EXP_STRING)` returns the unsigned code of the first byte as INTEGER, or 0 for an empty string. Its `(Ch : CHAR)` overload returns the unsigned byte code of Ch. For ASCII characters this is the ASCII code.
+
+`_EXP_STRING_CHR(Code : INTEGER)` returns a new `_EXP_STRING` containing the low eight bits of Code as one byte. Unlike built-in STRING, `_EXP_STRING` preserves NUL: `_EXP_STRING_CHR(0)` has length 1.
diff --git a/pseudocode-lib/_exp_string.cc b/pseudocode-lib/_exp_string.cc
index 6a17f74..ae53ac1 100644
--- a/pseudocode-lib/_exp_string.cc
+++ b/pseudocode-lib/_exp_string.cc
@@ -122,6 +122,11 @@ long long _exp_string_impl_asc(void *Handle) {
long long _exp_string_impl_asc_char(char Ch) {
return checked([&]() -> long long { return static_cast<unsigned char>(Ch); });
}
+void _exp_string_impl_chr(void *Target, long long Code) {
+ checked([&] {
+ text(Target).assign(1, static_cast<char>(static_cast<unsigned char>(Code)));
+ });
+}
void _exp_string_impl_output(void *Handle) {
checked([&] {
const auto &Text = text(Handle);
diff --git a/pseudocode-lib/_exp_string.cps b/pseudocode-lib/_exp_string.cps
index a4d95d6..965a251 100644
--- a/pseudocode-lib/_exp_string.cps
+++ b/pseudocode-lib/_exp_string.cps
@@ -23,6 +23,7 @@ EXTERN "C" EXTERN_PROCEDURE _exp_string_impl_ucase(_EXP_STRING_HANDLE, _EXP_STRI
EXTERN "C" EXTERN_FUNCTION _exp_string_impl_ucase_char(C_CHAR) RETURNS C_CHAR
EXTERN "C" EXTERN_FUNCTION _exp_string_impl_asc(_EXP_STRING_HANDLE) RETURNS _EXP_STRING_C_INTEGER
EXTERN "C" EXTERN_FUNCTION _exp_string_impl_asc_char(C_CHAR) RETURNS _EXP_STRING_C_INTEGER
+EXTERN "C" EXTERN_PROCEDURE _exp_string_impl_chr(_EXP_STRING_HANDLE, _EXP_STRING_C_INTEGER)
EXTERN "C" EXTERN_PROCEDURE _exp_string_impl_output(_EXP_STRING_HANDLE)
CLASS _EXP_STRING
@@ -111,3 +112,8 @@ ENDFUNCTION
OVERLOAD FUNCTION _EXP_STRING_ASC(Ch : CHAR) RETURNS INTEGER
RETURN (INTEGER)_exp_string_impl_asc_char((C_CHAR)Ch)
ENDFUNCTION
+FUNCTION _EXP_STRING_CHR(Code : INTEGER) RETURNS _EXP_STRING
+ DECLARE Result : _EXP_STRING
+ CALL _exp_string_impl_chr(Result._HANDLE, (_EXP_STRING_C_INTEGER)Code)
+ RETURN Result
+ENDFUNCTION
diff --git a/tests/library/exp-string/expected.txt b/tests/library/exp-string/expected.txt
index 88c9a2f..0680cf2 100644
--- a/tests/library/exp-string/expected.txt
+++ b/tests/library/exp-string/expected.txt
@@ -35,3 +35,7 @@ ABC
65:65
0:0:0
255
+AZ
+65
+1:0
+65:255
diff --git a/tests/library/exp-string/notes.md b/tests/library/exp-string/notes.md
index f360410..7eddda1 100644
--- a/tests/library/exp-string/notes.md
+++ b/tests/library/exp-string/notes.md
@@ -17,3 +17,5 @@ LCASE converts AbC@19 to abc@19 without modifying the source. Its CHAR overload
UCASE converts AbC@19 to ABC@19 without modifying the source. Its CHAR overload returns CHAR H for h or H and preserves @, as in guide section 5.5. Empty length stays zero; two leading NULs survive conversion and letters after them still become ABC.
ASC returns 65 for CHAR A and the first byte of ABCDEFGH, zero for empty strings or NUL, and 255 for a byte with all eight bits set. These byte-code functions are compiler/library extensions.
+
+CHR codes 65 and 90 concatenate to AZ; converting A to a code and back preserves 65. Code zero produces one retained NUL byte. Taking the low eight bits gives 65 for 321 and 255 for -1.
diff --git a/tests/library/exp-string/program.cps b/tests/library/exp-string/program.cps
index dfa5b47..e789e54 100644
--- a/tests/library/exp-string/program.cps
+++ b/tests/library/exp-string/program.cps
@@ -45,6 +45,7 @@ CALL CheckMid()
CALL CheckLCase()
CALL CheckUCase()
CALL CheckAsc()
+CALL CheckChr()
PROCEDURE CheckLeft()
DECLARE Letters : _EXP_STRING((C_STRING)"ABCDEFGH")
@@ -115,3 +116,10 @@ PROCEDURE CheckAsc()
OUTPUT _EXP_STRING_ASC(Empty), ":", _EXP_STRING_ASC(Nuls), ":", _EXP_STRING_ASC('\0')
OUTPUT _EXP_STRING_ASC(High)
ENDPROCEDURE
+
+PROCEDURE CheckChr()
+ CALL _EXP_STRING_OUTPUT(_EXP_STRING_CHR(65) & _EXP_STRING_CHR(90))
+ OUTPUT _EXP_STRING_ASC(_EXP_STRING_CHR(_EXP_STRING_ASC('A')))
+ OUTPUT _EXP_STRING_LENGTH(_EXP_STRING_CHR(0)), ":", _EXP_STRING_ASC(_EXP_STRING_CHR(0))
+ OUTPUT _EXP_STRING_ASC(_EXP_STRING_CHR(321)), ":", _EXP_STRING_ASC(_EXP_STRING_CHR(-1))
+ENDPROCEDURE