diff options
| author | 2025-06-18 16:37:19 +0200 | |
|---|---|---|
| committer | 2025-06-18 16:37:19 +0200 | |
| commit | 5408207577574eb8952368bf2bf925acb149851d (patch) | |
| tree | 1cb1f00add9b6bac830a680f209cde6c76f23cc9 | |
| parent | 1f48ccf32a0a7690d042da25fffe5c64ed216991 (diff) | |
ascenc/decmaster
| -rw-r--r-- | pez.c | 30 |
1 files changed, 30 insertions, 0 deletions
@@ -2335,6 +2335,34 @@ f_assert(PezContext *cx, int argc) return pez_pushvoid(cx); } +static bool +f_ascenc(PezContext *cx, int argc) +{ + char buf[8]; + TRY(pez_checksig(cx, argc, "ascenc", "string")); + if (pez_length(cx, -1) < 1) { + pez_error(cx, "ascenc", "empty string"); + return 0; + } + TRY(pez_getstring(cx, buf, -1)); + return pez_pushnumber(cx, FX((unsigned)buf[0])); +} + +static bool +f_ascdec(PezContext *cx, int argc) +{ + unsigned char buf[2] = ""; + PezNumber n; + TRY(pez_checksig(cx, argc, "ascdec", "number")); + TRY(pez_getnumber(cx, &n, -1)); + buf[0] = fixtoint(n); + if (FX(buf[0]) != n) { + return pez_error(cx, "ascenc", "invalid ascii code (%g)", fixtof(n)), 0; + } + return pez_pushstring(cx, (char *)buf, 1); +} + + static void file_dtor(void *d) { @@ -2486,6 +2514,8 @@ static const struct coredef { const char *n; PezCFn *f; } core[] = { { "tostring", f_tostring }, { "dilambda", f_dilambda }, { "typeof", f_typeof }, + { "ascenc", f_ascenc }, + { "ascdec", f_ascdec }, { "array#new", f_arraynew }, { "array#push", f_arraypush }, { "assert", f_assert }, |