From e5b7eea0cfc8d1f2743a74b4e26cdda537b71401 Mon Sep 17 00:00:00 2001 From: bog Date: Wed, 27 Mar 2024 17:19:40 +0100 Subject: [PATCH] :art: moved call logic from exec.c into moka state. --- lib/exec.c | 20 +------------------- lib/moka.c | 30 ++++++++++++++++++++++++++++++ lib/moka.h | 1 + 3 files changed, 32 insertions(+), 19 deletions(-) diff --git a/lib/exec.c b/lib/exec.c index b2a5f56..e7f9b9f 100644 --- a/lib/exec.c +++ b/lib/exec.c @@ -38,25 +38,7 @@ void exec_instr(struct exec* self, switch (instr->opcode) { case OP_CALL: { - MOKA fun = moka_pop(moka); - struct vec args; - vec_init(&args); - - for (ssize_t i=0; iglobal_values.data[ - moka_get_ref(moka, fun) - ]; - - assert(val->type == TY_NATIVE); - struct native* native = val->data.native; - (native->fun)(moka, &args); - - vec_free(&args); + moka_call(moka, param); self->pc++; } break; diff --git a/lib/moka.c b/lib/moka.c index 764831e..e959b35 100644 --- a/lib/moka.c +++ b/lib/moka.c @@ -149,6 +149,30 @@ void moka_dump(struct moka* self, MOKA value) abort(); } +MOKA moka_call(struct moka* self, int arg_count) +{ + MOKA fun = moka_pop(self); + struct vec args; + vec_init(&args); + + for (ssize_t i=0; iglobal_values.data[ + moka_get_ref(self, fun) + ]; + + assert(val->type == TY_NATIVE); + struct native* native = val->data.native; + (native->fun)(self, &args); + + vec_free(&args); + return moka_top(self); +} + MOKA moka_push_int(struct moka* self, int value, int line) { assert(self); @@ -305,6 +329,12 @@ native_fun_t moka_get_native(struct moka* self, MOKA value) MOKA moka_eval_lazy(struct moka* self, MOKA lazy_value) { assert(self); + + if (!moka_is(self, lazy_value, TY_LAZY)) + { + return lazy_value; + } + struct node* node = moka_get_lazy(self, lazy_value); struct compiler compiler; diff --git a/lib/moka.h b/lib/moka.h index 2b6baf6..67223a0 100644 --- a/lib/moka.h +++ b/lib/moka.h @@ -39,6 +39,7 @@ TypeKind moka_type_of(struct moka* self, MOKA value); void moka_dump(struct moka* self, MOKA value); +MOKA moka_call(struct moka* self, int arg_count); MOKA moka_push_int(struct moka* self, int value, int line); int moka_get_int(struct moka* self, MOKA value);