From a8519434f058d0ab60bf7f90acc61997cb982cfa Mon Sep 17 00:00:00 2001 From: ubq323 Date: Wed, 26 Jun 2024 14:44:19 +0100 Subject: add cfunc type and rudimentary stdlib --- vm.c | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) (limited to 'vm.c') diff --git a/vm.c b/vm.c index a5ffbc6..34660de 100644 --- a/vm.c +++ b/vm.c @@ -184,21 +184,25 @@ int runvm(State *S) { uint8_t len = RBYTE(); Val callee = PEEKN(len); - if (!IS_FUNC(callee)) { - fprintf(stderr,"can only call functions"); - exit(1); - } - ObjFunc *func = AS_FUNC(callee); - - StackFrame *sf = &th->rstack[th->rsp++]; - sf->ip = th->ip; - sf->ch = th->ch; - sf->fp = th->fp; - - th->ip = 0; - th->ch = &func->ch; - th->fp = th->sp - len; + if (IS_FUNC(callee)) { + ObjFunc *func = AS_FUNC(callee); + + StackFrame *sf = &th->rstack[th->rsp++]; + sf->ip = th->ip; + sf->ch = th->ch; + sf->fp = th->fp; + + th->ip = 0; + th->ch = &func->ch; + th->fp = th->sp - len; + } else if (IS_CFUNC(callee)) { + int nargs = len - 1; + Val *firstarg = &th->stack[th->sp - nargs]; + Val res = AS_CFUNC(callee)(S, len - 1, firstarg); + th->sp -= len; + PUSH(res); + } break; } -- cgit v1.2.3