diff options
| author | 2025-04-11 19:59:33 +0200 | |
|---|---|---|
| committer | 2025-04-11 19:59:33 +0200 | |
| commit | ef06c98e90ef97110594f2f1a457a7b68438774c (patch) | |
| tree | c3699adfe49b62d8f0f98929229418093b413fa3 /pez.c | |
| parent | ddb5bf5f8b221781c059365b81ded6fdbf8c947f (diff) | |
assert[]
Diffstat (limited to 'pez.c')
| -rw-r--r-- | pez.c | 28 |
1 files changed, 26 insertions, 2 deletions
@@ -2220,6 +2220,22 @@ f_arraypush(PezContext *cx, int argc) return push(cx, box_obj(arr)); } +static bool +f_assert(PezContext *cx, int argc) +{ + TRY(pez_checksig(cx, argc, "assert", "any, ?string")); + if (!pez_truthy(cx, -argc)) { + char buf[8]; + if (argc == 2) + pez_error(cx, "assert", "assertion failed: %s", pez_getstring(cx, buf, -1)); + else + pez_error(cx, "assert", "assertion failed!"); + return 0; + + } + return pez_pushvoid(cx); +} + static const struct coredef { const char *n; PezCFn *f; } core[] = { { "printf", f_printf }, { "sprintf", f_sprintf }, @@ -2227,6 +2243,7 @@ static const struct coredef { const char *n; PezCFn *f; } core[] = { { "typeof", f_typeof }, { "array#new", f_arraynew }, { "array#push", f_arraypush }, + { "assert", f_assert }, }; static bool @@ -4307,6 +4324,12 @@ pez_isarray(PezContext *cx, int idx) return isobj_of1(cx, idx, PEZ_TArray); } +bool +pez_truthy(PezContext *cx, int idx) +{ + return truthy(*iget(cx, idx)); +} + int pez_typeof(PezContext *cx, int idx) { @@ -4382,7 +4405,7 @@ pez_checksig(PezContext *cx, int argc, const char *fn, const char *sig) else assert(0 && "bad sig type"); if (arg >= argc) { if (opt) { - break; + goto next; } else { return pez_error(cx, fn, "too few args for [%s] (got %d)", osig, argc), 0; } @@ -4394,9 +4417,10 @@ pez_checksig(PezContext *cx, int argc, const char *fn, const char *sig) } while (*sig && *sig != ','); if ((typ & mask) == 0) { pez_error(cx, fn, "arg #%d mismatch: expected %.*s, got %s", - arg, thisn, this, pez_typename(cx, -argc + arg)); + arg, thisn, this + !aisalpha(*this), pez_typename(cx, -argc + arg)); return 0; } + next: sig += *sig == ','; if (!*sig) { ++arg; |