diff options
author | ubq323 <ubq323@ubq323.website> | 2024-06-26 14:44:19 +0100 |
---|---|---|
committer | ubq323 <ubq323@ubq323.website> | 2024-06-26 14:44:19 +0100 |
commit | a8519434f058d0ab60bf7f90acc61997cb982cfa (patch) | |
tree | e1858d42805e61afbd5c6c0b9d2f3b47bc8411ee /vm.c | |
parent | 20584e520dfd44393aa66350b442e8a23e46c5a3 (diff) |
add cfunc type and rudimentary stdlib
Diffstat (limited to 'vm.c')
-rw-r--r-- | vm.c | 32 |
1 files changed, 18 insertions, 14 deletions
@@ -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; } |