GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/lib/engine.c Lines: 128 130 98.5 %
Date: 2020-09-14 00:22:58 Branches: 15 20 75.0 %

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 "instr.h"
7
#include "object.h"
8
#include "emit.h"
9
#include "vm.h"
10
#include "gwion.h"
11
#include "operator.h"
12
#include "import.h"
13
#include "gwi.h"
14
#include "engine.h"
15
#include "parser.h"
16
#include "lang_private.h"
17
#include "specialid.h"
18
#include "gack.h"
19
20
#undef insert_symbol
21
5
static GACK(gack_class) {
22
5
  const Type type = actual_type(shred->info->vm->gwion, t) ?: t;
23
5
  INTERP_PRINTF("class(%s)", type->name)
24
5
}
25
26
17
static GACK(gack_function) {
27
17
  INTERP_PRINTF("%s", t->name)
28
17
}
29
30
static GACK(gack_gack) {
31
  INTERP_PRINTF("%s", *(m_str*)VALUE)
32
}
33
34
23
static GACK(gack_fptr) {
35
23
  const VM_Code code = *(VM_Code*)VALUE;
36
23
  if(code)
37
18
    INTERP_PRINTF("%s", code->name)
38
  else
39
5
    INTERP_PRINTF("%s", t->name)
40
23
}
41
42
31
static GACK(gack_void) {
43
31
 INTERP_PRINTF("void");
44
31
}
45
46
548
static GACK(gack_int) {
47
548
  INTERP_PRINTF("%"INT_F, *(m_uint*)VALUE);
48
548
}
49
50
3
static GACK(gack_char) {
51
3
  INTERP_PRINTF("%c", *(char*)VALUE);
52
3
}
53
54
156
static GACK(gack_float) {
55
156
  INTERP_PRINTF("%.4f", *(m_float*)VALUE);
56
156
}
57
58
191
static GACK(gack_compound) {
59
191
  INTERP_PRINTF("%p", *(void**)VALUE);
60
191
}
61
#define mk_class_instr(op, arg0, arg1, ...)                          \
62
static INSTR(instr_class_##op) {                                     \
63
  POP_REG(shred, SZ_INT);                                            \
64
  const Type l = *(Type*)(shred->reg - SZ_INT);                      \
65
  const Type r = *(Type*)(shred->reg);                               \
66
  *(m_uint*)(shred->reg - SZ_INT) = isa(arg0, arg1) > 0 __VA_ARGS__; \
67
}
68
2
mk_class_instr(ge, l, r)
69

4
mk_class_instr(gt, l, r, && l != r)
70
1
mk_class_instr(le, r, l)
71

3
mk_class_instr(lt, r, l, && l != r)
72
73
OP_CHECK(opck_object_dot);
74
OP_EMIT(opem_object_dot);
75
711
ANN static m_bool import_core_libs(const Gwi gwi) {
76
711
  const Type t_class = gwi_mk_type(gwi, "@Class", SZ_INT, NULL);
77
711
  GWI_BB(gwi_set_global_type(gwi, t_class, et_class))
78
711
  GWI_BB(gwi_gack(gwi, t_class, gack_class)) // not working yet
79
711
  GWI_BB(gwi_add_type(gwi, t_class))
80
711
  GWI_BB(gwi_oper_ini(gwi, (m_str)OP_ANY_TYPE, (m_str)OP_ANY_TYPE, NULL))
81
711
  GWI_BB(gwi_oper_add(gwi, opck_object_dot))
82
711
  GWI_BB(gwi_oper_emi(gwi, opem_object_dot))
83
711
  GWI_BB(gwi_oper_end(gwi, "@dot", NULL))
84
711
  const Type t_undefined = gwi_mk_type(gwi, "@Undefined", SZ_INT, NULL);
85
711
  GWI_BB(gwi_set_global_type(gwi, t_undefined, et_undefined))
86
711
  const Type t_auto = gwi_mk_type(gwi, "auto", SZ_INT, NULL);
87
711
  SET_FLAG(t_auto, infer);
88
711
  GWI_BB(gwi_set_global_type(gwi, t_auto, et_auto))
89
711
  SET_FLAG(t_class, infer);
90
711
  const Type t_void  = gwi_mk_type(gwi, "void", 0, NULL);
91
711
  GWI_BB(gwi_gack(gwi, t_void, gack_void))
92
711
  GWI_BB(gwi_set_global_type(gwi, t_void, et_void))
93
711
  const Type t_gack = gwi_mk_type(gwi, "@Gack", SZ_INT, NULL);
94
711
  GWI_BB(gwi_gack(gwi, t_gack, gack_gack))
95
711
  GWI_BB(gwi_set_global_type(gwi, t_gack, et_gack))
96
711
  const Type t_int = gwi_mk_type(gwi, "int", SZ_INT, NULL);
97
711
  GWI_BB(gwi_gack(gwi, t_int, gack_int))
98
711
  GWI_BB(gwi_set_global_type(gwi, t_int, et_int))
99
711
  const Type t_char = gwi_mk_type(gwi, "char", SZ_INT, "int");
100
711
  GWI_BB(gwi_gack(gwi, t_char, gack_char))
101
711
  GWI_BB(gwi_set_global_type(gwi, t_char, et_char))
102
711
  const Type t_float = gwi_mk_type(gwi, "float", SZ_FLOAT, NULL);
103
711
  GWI_BB(gwi_gack(gwi, t_float, gack_float))
104
711
  GWI_BB(gwi_set_global_type(gwi, t_float, et_float))
105
711
  const Type t_dur = gwi_mk_type(gwi, "dur", SZ_FLOAT, NULL);
106
711
  GWI_BB(gwi_gack(gwi, t_dur, gack_float))
107
711
  GWI_BB(gwi_add_type(gwi, t_dur))
108
711
  const Type t_time = gwi_mk_type(gwi, "time", SZ_FLOAT, NULL);
109
711
  GWI_BB(gwi_gack(gwi, t_time, gack_float))
110
711
  GWI_BB(gwi_add_type(gwi, t_time))
111
711
  const Type t_now = gwi_mk_type(gwi, "@now", SZ_FLOAT, "time");
112
711
  GWI_BB(gwi_add_type(gwi, t_now))
113
711
  struct SpecialId_ spid = { .type=t_now, .exec=RegPushNow, .is_const=1 };
114
711
  gwi_specialid(gwi, "now", &spid);
115
116
711
  const Type t_compound = gwi_mk_type(gwi, "@Compound", 0, NULL);
117
711
  GWI_BB(gwi_gack(gwi, t_compound, gack_compound))
118
711
  GWI_BB(gwi_set_global_type(gwi, t_compound, et_compound))
119
711
  GWI_BB(import_object(gwi))
120
711
  GWI_BB(import_prim(gwi))
121
711
  const Type t_function = gwi_mk_type(gwi, "@function", SZ_INT, NULL);
122
711
  GWI_BB(gwi_gack(gwi, t_function, gack_function))
123
711
  GWI_BB(gwi_set_global_type(gwi, t_function, et_function))
124
711
  const Type t_fptr = gwi_mk_type(gwi, "@func_ptr", SZ_INT, "@function");
125
711
  GWI_BB(gwi_gack(gwi, t_fptr, gack_fptr))
126
711
  GWI_BB(gwi_set_global_type(gwi, t_fptr, et_fptr))
127
711
  const Type t_lambda = gwi_mk_type(gwi, "@lambda", SZ_INT, "@function");
128
711
  SET_FLAG(t_lambda, infer);
129
711
  GWI_BB(gwi_set_global_type(gwi, t_lambda, et_lambda))
130
131
711
  GWI_BB(gwi_typedef_ini(gwi, "int", "@internal"))
132
711
  GWI_BB(gwi_typedef_end(gwi, ae_flag_none))
133
134
711
  GWI_BB(import_object_op(gwi))
135
711
  GWI_BB(import_values(gwi))
136
137
// TODO: check me
138
711
  const Type t_union = gwi_class_ini(gwi, "@Union", NULL);
139
711
  gwi->gwion->type[et_union] = t_union;
140
711
  GWI_BB(gwi_class_end(gwi))
141
142
711
  GWI_BB(import_array(gwi))
143
711
  GWI_BB(import_event(gwi))
144
711
  GWI_BB(import_ugen(gwi))
145
711
  GWI_BB(import_ptr(gwi))
146
711
  GWI_BB(import_func(gwi))
147
711
  GWI_BB(gwi_oper_ini(gwi, NULL, (m_str)OP_ANY_TYPE, NULL))
148
711
  GWI_BB(gwi_oper_add(gwi, opck_new))
149
711
  GWI_BB(gwi_oper_emi(gwi, opem_new))
150
711
  GWI_BB(gwi_oper_end(gwi, "new", NULL))
151
//  GWI_BB(import_prim(gwi))
152
711
  GWI_BB(import_vararg(gwi))
153
711
  GWI_BB(import_string(gwi))
154
711
  GWI_BB(import_shred(gwi))
155
711
  GWI_BB(import_modules(gwi))
156
157
711
  GWI_BB(gwi_oper_ini(gwi, "@Class", "@Class", "int"))
158
711
  GWI_BB(gwi_oper_end(gwi, "==", int_eq))
159
711
  GWI_BB(gwi_oper_end(gwi, "!=", int_neq))
160
711
  GWI_BB(gwi_oper_end(gwi, ">=", instr_class_ge))
161
711
  GWI_BB(gwi_oper_end(gwi, ">",  instr_class_gt))
162
711
  GWI_BB(gwi_oper_end(gwi, "<=", instr_class_le))
163
711
  GWI_BB(gwi_oper_end(gwi, "<",  instr_class_lt))
164
711
  return GW_OK;
165
}
166
167
711
ANN m_bool type_engine_init(const Gwion gwion, const Vector plug_dirs) {
168
711
  gwion->env->name = "[builtin]";
169
711
  struct loc_t_ loc = {};
170
711
  OperCK oper = {};
171
711
  struct Gwi_ gwi = { .gwion=gwion, .loc=&loc, .oper=&oper };
172
711
  CHECK_BB(import_core_libs(&gwi))
173
711
  push_global(gwion, "[plugins]");
174
711
  gwion->env->name = "[imported]";
175
780
  for(m_uint i = 0; i < vector_size(plug_dirs); ++i) {
176
69
    m_bool (*import)(Gwi) = (m_bool(*)(Gwi))vector_at(plug_dirs, i);
177

69
    if(import && import(&gwi) < 0)
178
35
      gwi_reset(&gwi);
179
  }
180
711
  push_global(gwion, "[user]");
181
711
  return GW_OK;
182
}