🎨 moved call logic from exec.c into moka state.

main
bog 2024-03-27 17:19:40 +01:00
parent b761c1849f
commit e5b7eea0cf
3 changed files with 32 additions and 19 deletions

View File

@ -38,25 +38,7 @@ void exec_instr(struct exec* self,
switch (instr->opcode) switch (instr->opcode)
{ {
case OP_CALL: { case OP_CALL: {
MOKA fun = moka_pop(moka); moka_call(moka, param);
struct vec args;
vec_init(&args);
for (ssize_t i=0; i<param; i++)
{
MOKA arg = moka_pop(moka);
vec_push(&args, (void*) arg);
}
struct value* val = moka->global_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);
self->pc++; self->pc++;
} break; } break;

View File

@ -149,6 +149,30 @@ void moka_dump(struct moka* self, MOKA value)
abort(); 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; i<arg_count; i++)
{
MOKA arg = moka_pop(self);
vec_push(&args, (void*) arg);
}
struct value* val = self->global_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) MOKA moka_push_int(struct moka* self, int value, int line)
{ {
assert(self); 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) MOKA moka_eval_lazy(struct moka* self, MOKA lazy_value)
{ {
assert(self); assert(self);
if (!moka_is(self, lazy_value, TY_LAZY))
{
return lazy_value;
}
struct node* node = moka_get_lazy(self, lazy_value); struct node* node = moka_get_lazy(self, lazy_value);
struct compiler compiler; struct compiler compiler;

View File

@ -39,6 +39,7 @@ TypeKind moka_type_of(struct moka* self, MOKA value);
void moka_dump(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); MOKA moka_push_int(struct moka* self, int value, int line);
int moka_get_int(struct moka* self, MOKA value); int moka_get_int(struct moka* self, MOKA value);