diff options
Diffstat (limited to 'pez.c')
| -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 }, |