🎨 moved call logic from exec.c into moka state.
parent
b761c1849f
commit
e5b7eea0cf
20
lib/exec.c
20
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; 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);
|
||||
moka_call(moka, param);
|
||||
self->pc++;
|
||||
} break;
|
||||
|
||||
|
|
30
lib/moka.c
30
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; 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)
|
||||
{
|
||||
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;
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
Loading…
Reference in New Issue