Line | Branch | Exec | Source |
---|---|---|---|
1 | //! \file enum.c | ||
2 | //! \brief enumerations utils | ||
3 | |||
4 | #include "gwion_util.h" | ||
5 | #include "gwion_ast.h" | ||
6 | #include "gwion_env.h" | ||
7 | #include "vm.h" | ||
8 | #include "traverse.h" | ||
9 | #include "instr.h" | ||
10 | #include "emit.h" | ||
11 | #include "gwion.h" | ||
12 | #include "object.h" | ||
13 | #include "operator.h" | ||
14 | #include "import.h" | ||
15 | #include "gwi.h" | ||
16 | |||
17 | //! start an enum definition | ||
18 | //! \arg the importer | ||
19 | //! \arg string defining a primitive type | ||
20 | //! why is return type m_int ? | ||
21 | |||
22 | __attribute__ ((visibility ("default"))) | ||
23 | 640 | ANN m_int gwi_enum_ini(const Gwi gwi, const m_str type) { | |
24 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 639 times.
|
640 | CHECK_BB(ck_ini(gwi, ck_edef)); |
25 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 639 times.
|
639 | CHECK_OB((gwi->ck->xid = gwi_str2sym(gwi, type))); |
26 | 639 | vector_init(&gwi->ck->v); | |
27 | 639 | gwi->ck->tmpl = new_mp_vector(gwi->gwion->mp, Symbol, 0); | |
28 | 639 | return GW_OK; | |
29 | } | ||
30 | /* | ||
31 | // adds the id_list to the enum | ||
32 | // change that algo? | ||
33 | ANN static void add2list(struct ImportCK *ck, const ID_List list) { | ||
34 | if (!ck->tmpl) { | ||
35 | ck->tmpl = new_mp_vector(list, ; | ||
36 | } else { | ||
37 | ck->curr->next = list; | ||
38 | } | ||
39 | ck->curr = list; | ||
40 | } | ||
41 | */ | ||
42 | //! add an enum entry | ||
43 | //! \arg the importer | ||
44 | //! \arg name of the entry | ||
45 | //! TODO: change return type to m_bool | ||
46 | 640 | ANN m_int gwi_enum_add(const Gwi gwi, const m_str name, const m_uint i) { | |
47 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 639 times.
|
640 | CHECK_BB(ck_ok(gwi, ck_edef)); |
48 | // DECL_OB(const ID_List, list, = gwi_str2symlist(gwi, name)); | ||
49 | |||
50 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 639 times.
|
639 | DECL_OB(const Symbol, xid, = gwi_str2sym(gwi, name)); |
51 |
1/2✓ Branch 0 taken 639 times.
✗ Branch 1 not taken.
|
639 | mp_vector_add(gwi->gwion->mp, &gwi->ck->tmpl, Symbol, xid); |
52 | // add2list(gwi->ck, list); | ||
53 | 639 | vector_add(&gwi->ck->v, (vtype)i); | |
54 | 639 | return GW_OK; | |
55 | } | ||
56 | |||
57 | //! set enum values | ||
58 | //! \arg the importer | ||
59 | //! \arg a vector of values | ||
60 | //! \note [internal] | ||
61 | 638 | ANN static void import_enum_end(const Gwi gwi, const Vector v) { | |
62 | 638 | ImportCK *ck = gwi->ck; | |
63 |
2/2✓ Branch 1 taken 638 times.
✓ Branch 2 taken 638 times.
|
1276 | for (m_uint i = 0; i < vector_size(v); i++) { |
64 | 638 | const Value value = (Value)vector_at(v, i); | |
65 | 638 | const m_uint addr = vector_at(&ck->v, i); | |
66 | 638 | set_vflag(value, vflag_builtin); | |
67 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 638 times.
|
638 | value->d.num = addr ?: i; |
68 | } | ||
69 | // better clean ? | ||
70 | 638 | } | |
71 | |||
72 | //! finish enum import | ||
73 | //! \arg the importer | ||
74 | //! TODO: check what happens in inside template class | ||
75 | 638 | ANN Type gwi_enum_end(const Gwi gwi) { | |
76 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 638 times.
|
638 | CHECK_BO(ck_ok(gwi, ck_edef)); |
77 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 638 times.
|
638 | if (!vector_size(&gwi->ck->v)) GWI_ERR_O("Enum is empty"); |
78 | 638 | const Gwion gwion = gwi->gwion; | |
79 | const Enum_Def edef = | ||
80 | 638 | new_enum_def(gwion->mp, gwi->ck->tmpl, gwi->ck->xid, gwi->loc); | |
81 | 638 | gwi->ck->tmpl = NULL; | |
82 | 638 | const m_bool ret = traverse_enum_def(gwion->env, edef); | |
83 |
1/2✓ Branch 0 taken 638 times.
✗ Branch 1 not taken.
|
638 | if (ret > 0) import_enum_end(gwi, &edef->values); |
84 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 638 times.
|
638 | if (gwi->gwion->data->cdoc) { |
85 | ✗ | gwfmt_indent(gwi->gwfmt); | |
86 | ✗ | gwfmt_enum_def(gwi->gwfmt, edef); | |
87 | } | ||
88 |
1/2✓ Branch 0 taken 638 times.
✗ Branch 1 not taken.
|
638 | const Type t = ret > 0 ? edef->type : NULL; |
89 |
1/2✓ Branch 0 taken 638 times.
✗ Branch 1 not taken.
|
638 | if (edef->values.ptr) vector_release(&edef->values); |
90 | 638 | free_enum_def(gwion->mp, edef); | |
91 | 638 | vector_release(&gwi->ck->v); | |
92 | 638 | gwi->ck->v.ptr = NULL; | |
93 | 638 | ck_end(gwi); | |
94 | 638 | return t; | |
95 | } | ||
96 |