| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include <string.h> | ||
| 2 | #include "gwion_util.h" | ||
| 3 | #include "gwion_ast.h" | ||
| 4 | #include "gwion_env.h" | ||
| 5 | #include "vm.h" | ||
| 6 | #include "gwion.h" | ||
| 7 | #include "instr.h" | ||
| 8 | #include "emit.h" | ||
| 9 | #include "object.h" | ||
| 10 | #include "operator.h" | ||
| 11 | #include "import.h" | ||
| 12 | #include "traverse.h" | ||
| 13 | #include "parse.h" | ||
| 14 | |||
| 15 | ✗ | ANN static Type fork_type(const Env env, const Exp_Unary *unary) { | |
| 16 | ✗ | const Type t = unary->exp->type; | |
| 17 | ✗ | if (t == env->gwion->type[et_void]) return env->gwion->type[et_fork]; | |
| 18 | ✗ | char c[21 + strlen(t->name)]; | |
| 19 | ✗ | sprintf(c, "TypedFork:[%s]", t->name); | |
| 20 | ✗ | const Type fork = env->gwion->type[et_fork]; | |
| 21 | ✗ | UNSET_FLAG(fork, final); | |
| 22 | ✗ | const Type typed = str2type(env->gwion, "TypedFork", exp_self(unary)->pos); | |
| 23 | ✗ | if (typed->nspc->offset == fork->nspc->offset) | |
| 24 | ✗ | typed->nspc->offset += t->size; | |
| 25 | ✗ | UNSET_FLAG(typed, final); | |
| 26 | ✗ | const Type ret = str2type(env->gwion, c, exp_self(unary)->pos); | |
| 27 | ✗ | SET_FLAG(typed, final); | |
| 28 | ✗ | SET_FLAG(fork, final); | |
| 29 | ✗ | return ret; | |
| 30 | } | ||
| 31 | |||
| 32 | ANN Type upvalue_type(const Env env, Capture *cap); | ||
| 33 | 51 | static OP_CHECK(opck_spork) { | |
| 34 | 51 | const Exp_Unary *unary = (Exp_Unary *)data; | |
| 35 |
4/4✓ Branch 0 taken 25 times.
✓ Branch 1 taken 26 times.
✓ Branch 2 taken 24 times.
✓ Branch 3 taken 1 times.
|
51 | if (unary->unary_type == unary_exp && unary->exp->exp_type == ae_exp_call) { |
| 36 | 24 | const m_bool is_spork = unary->op == insert_symbol("spork"); | |
| 37 |
1/2✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
|
24 | return is_spork ? env->gwion->type[et_shred] : fork_type(env, unary); |
| 38 | } | ||
| 39 |
2/2✓ Branch 0 taken 26 times.
✓ Branch 1 taken 1 times.
|
27 | if (unary->unary_type == unary_code) { |
| 40 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 24 times.
|
26 | if(unary->captures) { |
| 41 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | uint32_t offset = !env->class_def ? 0 : SZ_INT; |
| 42 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | for(uint32_t i = 0; i < unary->captures->len; i++) { |
| 43 | 2 | Capture *const cap = mp_vector_at(unary->captures, Capture, i); | |
| 44 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
|
2 | DECL_OO(const Type, t, = upvalue_type(env, cap)); |
| 45 | 1 | cap->temp = new_value(env, t, s_name(cap->xid), cap->pos); | |
| 46 | 1 | cap->temp->from->offset = offset; | |
| 47 | 1 | offset += cap->temp->type->size; | |
| 48 | } | ||
| 49 | } | ||
| 50 | 25 | Upvalues upvalues = { .values = env->curr->info->value }; | |
| 51 |
4/4✓ Branch 0 taken 7 times.
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 6 times.
|
25 | if(env->func && env->func->def->base->values) |
| 52 | 1 | upvalues.parent = env->func->def->base->values; | |
| 53 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 20 times.
|
25 | if(env->class_def) env->class_def->info->values = env->curr->info->value; |
| 54 | 25 | env->curr->info->value = new_scope(env->gwion->mp); | |
| 55 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 24 times.
|
25 | if(unary->captures) { |
| 56 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | for(uint32_t i = 0; i < unary->captures->len; i++) { |
| 57 | 1 | Capture *const cap = mp_vector_at(unary->captures, Capture, i); | |
| 58 | 1 | valid_value(env, cap->xid, cap->temp); | |
| 59 | } | ||
| 60 | } | ||
| 61 | 25 | const Func f = env->func; | |
| 62 | 25 | struct Value_ value = { .type = env->gwion->type[et_function]}; | |
| 63 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 20 times.
|
25 | if(env->class_def) |
| 64 | 5 | set_vflag(&value, vflag_member); | |
| 65 | 25 | struct Func_Base_ fbase = { .xid=insert_symbol("in spork"), .values = &upvalues, .fbflag = fbflag_lambda, .pos = exp_self(unary)->pos}; | |
| 66 | 25 | struct Func_Def_ fdef = { .base = &fbase}; | |
| 67 | 25 | struct Func_ func = { .name = "in spork", .def = &fdef, .value_ref = &value}; | |
| 68 | 25 | env->func = &func; | |
| 69 | // scope depth? | ||
| 70 | 25 | const m_bool ret = check_stmt_list(env, unary->code); | |
| 71 | 25 | env->func = f; | |
| 72 | 25 | free_scope(env->gwion->mp, env->curr->info->value); | |
| 73 | 25 | env->curr->info->value = upvalues.values; | |
| 74 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
|
25 | CHECK_BN(ret); |
| 75 | 25 | return env->gwion | |
| 76 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 7 times.
|
25 | ->type[unary->op == insert_symbol("spork") ? et_shred : et_fork]; |
| 77 | } | ||
| 78 | 1 | ERR_O(exp_self(unary)->pos, _("only function calls can be sporked...")) | |
| 79 | } | ||
| 80 | |||
| 81 | 50 | static OP_EMIT(opem_spork) { | |
| 82 | 50 | const Exp_Unary *unary = (Exp_Unary *)data; | |
| 83 | 50 | return emit_exp_spork(emit, unary); | |
| 84 | } | ||
| 85 | |||
| 86 | 45 | static FREEARG(freearg_xork) { vmcode_remref((VM_Code)instr->m_val, gwion); } | |
| 87 | |||
| 88 | 19 | static FREEARG(clean_fast_except) { | |
| 89 | 19 | struct FastExceptInfo *info = (struct FastExceptInfo *)instr->m_val2; | |
| 90 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 14 times.
|
19 | if(info) mp_free2(((Gwion)gwion)->mp, sizeof(struct FastExceptInfo), info); |
| 91 | 19 | } | |
| 92 | |||
| 93 | 638 | GWION_IMPORT(xork) { | |
| 94 | 638 | GWI_BB(gwi_oper_ini(gwi, NULL, (m_str)OP_ANY_TYPE, NULL)) | |
| 95 | 638 | GWI_BB(gwi_oper_add(gwi, opck_spork)) | |
| 96 | 638 | GWI_BB(gwi_oper_emi(gwi, opem_spork)) | |
| 97 | 638 | GWI_BB(gwi_oper_end(gwi, "spork", NULL)) | |
| 98 | 638 | GWI_BB(gwi_oper_add(gwi, opck_spork)) | |
| 99 | 638 | GWI_BB(gwi_oper_emi(gwi, opem_spork)) | |
| 100 | 638 | GWI_BB(gwi_oper_end(gwi, "fork", NULL)) | |
| 101 | 638 | gwi_register_freearg(gwi, SporkIni, freearg_xork); | |
| 102 | 638 | gwi_register_freearg(gwi, fast_except, clean_fast_except); | |
| 103 | 638 | return GW_OK; | |
| 104 | } | ||
| 105 |