From ddb5bf5f8b221781c059365b81ded6fdbf8c947f Mon Sep 17 00:00:00 2001 From: lemon Date: Fri, 11 Apr 2025 17:56:06 +0200 Subject: fix bug with newrec. add typeof --- pez.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'pez.c') diff --git a/pez.c b/pez.c index 98f7d08..92f9a47 100644 --- a/pez.c +++ b/pez.c @@ -1555,6 +1555,7 @@ exefn(PezContext *cx, Fn *fn, uint nargs) TRY(push(cx, VOID)); ++stktop; } + /* Val *start = stktop; */ assert(pr->nstack > 0); if (stktop + pr->nstack >= cx->stkend) { return cx->err = PEZ_EStack, 0; @@ -1576,7 +1577,7 @@ exefn(PezContext *cx, Fn *fn, uint nargs) #define BADOP break; default: #endif #define push(x) (*stktop++ = (x)) -#define pop() (*--stktop) +#define pop() (/*assert(start < stktop),*/ *--stktop) #define peek() (&stktop[-1]) VMBEGIN @@ -1924,7 +1925,7 @@ exefn(PezContext *cx, Fn *fn, uint nargs) uint8_t n = code[ip++]; Record *rc; TRY((rc = newrecord(cx, n, stktop - 2*n)) != NULL); - stktop -= n + 1; + stktop -= 2*n; push(box_obj(rc)); } CASE(Ob) { @@ -2202,6 +2203,13 @@ f_arraynew(PezContext *cx, int argc) return push(cx, box_obj(arr)); } +static bool +f_typeof(PezContext *cx, int argc) +{ + TRY(pez_checksig(cx, argc, "typeof", "any")); + return pez_pushstring(cx, pez_typename(cx, -1), -1); +} + static bool f_arraypush(PezContext *cx, int argc) { @@ -2216,6 +2224,7 @@ static const struct coredef { const char *n; PezCFn *f; } core[] = { { "printf", f_printf }, { "sprintf", f_sprintf }, { "dilambda", f_dilambda }, + { "typeof", f_typeof }, { "array#new", f_arraynew }, { "array#push", f_arraypush }, }; -- cgit v1.2.3