Line | Branch | Exec | Source |
---|---|---|---|
1 | #include <stdlib.h> | ||
2 | #include <string.h> | ||
3 | #include <ctype.h> | ||
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 | #include "mpool.h" | ||
17 | #include "specialid.h" | ||
18 | |||
19 | 374506 | ANN static Type _get_type(const Gwi gwi, const m_str s) { | |
20 |
2/2✓ Branch 0 taken 12122 times.
✓ Branch 1 taken 362384 times.
|
374506 | if (s == (m_str)OP_ANY_TYPE) return OP_ANY_TYPE; |
21 | // str2type | ||
22 | 362384 | Type_Decl *td = gwi_str2td(gwi, s); | |
23 | 362384 | const Type t = known_type(gwi->gwion->env, td); | |
24 | 362384 | free_type_decl(gwi->gwion->mp, td); | |
25 | 362384 | return t; | |
26 | } | ||
27 | |||
28 | 424908 | ANN2(1) static inline Type gwi_get_type(const Gwi gwi, const m_str str) { | |
29 |
2/2✓ Branch 0 taken 374506 times.
✓ Branch 1 taken 50402 times.
|
424908 | return str ? _get_type(gwi, str) : NULL; |
30 | } | ||
31 | |||
32 | ANN2(1, 2) | ||
33 | 141636 | static int import_op(const Gwi gwi, struct OperCK *const op, const f_instr f) { | |
34 | 141636 | const Type lhs = gwi_get_type(gwi, op->lhs), rhs = gwi_get_type(gwi, op->rhs), | |
35 | 141636 | ret = gwi_get_type(gwi, op->ret); | |
36 | 141636 | const struct Op_Func opfunc = { | |
37 | 141636 | .ck = op->ck, .em = op->em, .effect = {.ptr = op->effect.ptr}}; | |
38 | 141636 | const struct Op_Import opi = {.lhs = lhs, | |
39 | .rhs = rhs, | ||
40 | .ret = ret, | ||
41 | .func = &opfunc, | ||
42 | 141636 | .data = (uintptr_t)f, | |
43 | .pos = gwi->loc, | ||
44 | 141636 | .op = op->sym}; | |
45 | 141636 | const m_bool b = add_op(gwi->gwion, &opi); | |
46 | 141636 | op->effect.ptr = NULL; | |
47 | 141636 | return b; | |
48 | } | ||
49 | |||
50 | ANN2(1) | ||
51 | 59334 | m_int gwi_oper_ini(const Gwi gwi, const restrict m_str l, | |
52 | const restrict m_str r, const restrict m_str t) { | ||
53 | 59334 | gwi->oper->ret = t; | |
54 | 59334 | gwi->oper->rhs = r; | |
55 | 59334 | gwi->oper->lhs = l; | |
56 | 59334 | return GW_OK; | |
57 | } | ||
58 | |||
59 | 110374 | ANN m_int gwi_oper_add(const Gwi gwi, Type (*ck)(Env, void *)) { | |
60 | 110374 | gwi->oper->ck = ck; | |
61 | 110374 | return GW_OK; | |
62 | } | ||
63 | |||
64 | 29986 | ANN m_int gwi_oper_emi(const Gwi gwi, const opem em) { | |
65 | 29986 | gwi->oper->em = em; | |
66 | 29986 | return GW_OK; | |
67 | } | ||
68 | |||
69 | 1914 | ANN void gwi_oper_eff(const Gwi gwi, const m_str effect) { | |
70 |
1/2✓ Branch 0 taken 1914 times.
✗ Branch 1 not taken.
|
1914 | if (!gwi->oper->effect.ptr) vector_init(&gwi->oper->effect); |
71 | 1914 | vector_add(&gwi->oper->effect, (m_uint)insert_symbol(gwi->gwion->st, effect)); | |
72 | 1914 | } | |
73 | |||
74 | 141636 | ANN m_int gwi_oper_end(const Gwi gwi, const m_str op, const f_instr f) { | |
75 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 141636 times.
|
141636 | if (gwi->gwion->data->cdoc) { |
76 | ✗ | gwfmt_indent(gwi->gwfmt); | |
77 | ✗ | gwfmt_util(gwi->gwfmt, "{+C}operator{0} "); | |
78 | ✗ | if (gwi->oper->lhs && !gwi->oper->rhs) { | |
79 | ✗ | gwfmt_util(gwi->gwfmt, "{+}%s{0}", | |
80 | ✗ | gwi->oper->lhs != (m_str)1 ? gwi->oper->lhs : "@Any"); | |
81 | ✗ | gwfmt_space(gwi->gwfmt); | |
82 | } | ||
83 | ✗ | if (gwi->oper->ret) { | |
84 | ✗ | gwfmt_util(gwi->gwfmt, "{+}%s{0}", | |
85 | ✗ | gwi->oper->ret != (m_str)1 ? gwi->oper->ret : "@Any"); | |
86 | ✗ | gwfmt_space(gwi->gwfmt); | |
87 | } | ||
88 | ✗ | gwfmt_util(gwi->gwfmt, "{/}%s{0}", op); | |
89 | ✗ | gwfmt_space(gwi->gwfmt); | |
90 | ✗ | gwfmt_lparen(gwi->gwfmt); | |
91 | ✗ | if (gwi->oper->lhs && gwi->oper->rhs) { | |
92 | ✗ | gwfmt_util(gwi->gwfmt, "{+}%s{0}", | |
93 | ✗ | gwi->oper->lhs != (m_str)1 ? gwi->oper->lhs : "@Any"); | |
94 | ✗ | gwfmt_comma(gwi->gwfmt); | |
95 | ✗ | gwfmt_space(gwi->gwfmt); | |
96 | } | ||
97 | ✗ | if (gwi->oper->rhs) | |
98 | ✗ | gwfmt_util(gwi->gwfmt, "{+}%s{0}", | |
99 | ✗ | gwi->oper->rhs != (m_str)1 ? gwi->oper->rhs : "@Any"); | |
100 | ✗ | gwfmt_rparen(gwi->gwfmt); | |
101 | ✗ | gwfmt_sc(gwi->gwfmt); | |
102 | ✗ | gwfmt_nl(gwi->gwfmt); | |
103 | } | ||
104 | 141636 | gwi->oper->sym = insert_symbol(gwi->gwion->st, op); | |
105 | 141636 | const m_bool ret = import_op(gwi, gwi->oper, f); | |
106 | 141636 | gwi->oper->ck = NULL; | |
107 | 141636 | gwi->oper->em = NULL; | |
108 | 141636 | return ret; | |
109 | } | ||
110 | |||
111 | 2552 | ANN m_int gwi_oper_cond(const Gwi gwi, const m_str type, const f_instr f1, | |
112 | const f_instr f2) { | ||
113 | 2552 | GWI_BB(gwi_oper_ini(gwi, NULL, type, "bool")) | |
114 | 2552 | GWI_BB(gwi_oper_end(gwi, "@conditional", f1)) | |
115 | 2552 | GWI_BB(gwi_oper_end(gwi, "@unconditional", f2)) | |
116 | 2552 | return GW_OK; | |
117 | } | ||
118 |