summaryrefslogtreecommitdiff
path: root/pez.c
diff options
context:
space:
mode:
Diffstat (limited to 'pez.c')
-rw-r--r--pez.c28
1 files changed, 26 insertions, 2 deletions
diff --git a/pez.c b/pez.c
index 92f9a47..87cf518 100644
--- a/pez.c
+++ b/pez.c
@@ -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;