GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/lib/engine.c Lines: 128 130 98.5 %
Date: 2020-07-24 14:14:26 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
6
static GACK(gack_class) {
22
6
  const Type type = actual_type(shred->info->vm->gwion, t) ?: t;
23
6
  INTERP_PRINTF("class(%s)", type->name)
24
6
}
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
21
static GACK(gack_fptr) {
35
21
  const VM_Code code = *(VM_Code*)VALUE;
36
21
  if(code)
37
16
    INTERP_PRINTF("%s", code->name)
38
  else
39
5
    INTERP_PRINTF("%s", t->name)
40
21
}
41
42
28
static GACK(gack_void) {
43
28
 INTERP_PRINTF("void");
44
28
}
45
46
565
static GACK(gack_int) {
47
565
  INTERP_PRINTF("%"INT_F, *(m_uint*)VALUE);
48
565
}
49
50
3
static GACK(gack_char) {
51
3
  INTERP_PRINTF("%c", *(char*)VALUE);
52
3
}
53
54
170
static GACK(gack_float) {
55
170
  INTERP_PRINTF("%.4f", *(m_float*)VALUE);
56
170
}
57
58
192
static GACK(gack_compound) {
59
192
  INTERP_PRINTF("%p", VALUE);
60
192
}
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
730
ANN static m_bool import_core_libs(const Gwi gwi) {
76
730
  const Type t_class = gwi_mk_type(gwi, "@Class", SZ_INT, NULL);
77
730
  GWI_BB(gwi_set_global_type(gwi, t_class, et_class))
78
730
  GWI_BB(gwi_gack(gwi, t_class, gack_class)) // not working yet
79
730
  GWI_BB(gwi_add_type(gwi, t_class))
80
730
  GWI_BB(gwi_oper_ini(gwi, (m_str)OP_ANY_TYPE, (m_str)OP_ANY_TYPE, NULL))
81
730
  GWI_BB(gwi_oper_add(gwi, opck_object_dot))
82
730
  GWI_BB(gwi_oper_emi(gwi, opem_object_dot))
83
730
  GWI_BB(gwi_oper_end(gwi, "@dot", NULL))
84
730
  const Type t_undefined = gwi_mk_type(gwi, "@Undefined", SZ_INT, NULL);
85
730
  GWI_BB(gwi_set_global_type(gwi, t_undefined, et_undefined))
86
730
  const Type t_auto = gwi_mk_type(gwi, "auto", SZ_INT, NULL);
87
730
  SET_FLAG(t_auto, infer);
88
730
  GWI_BB(gwi_set_global_type(gwi, t_auto, et_auto))
89
730
  SET_FLAG(t_class, infer);
90
730
  const Type t_void  = gwi_mk_type(gwi, "void", 0, NULL);
91
730
  GWI_BB(gwi_gack(gwi, t_void, gack_void))
92
730
  GWI_BB(gwi_set_global_type(gwi, t_void, et_void))
93
730
  const Type t_gack = gwi_mk_type(gwi, "@Gack", SZ_INT, NULL);
94
730
  GWI_BB(gwi_gack(gwi, t_gack, gack_gack))
95
730
  GWI_BB(gwi_set_global_type(gwi, t_gack, et_gack))
96
730
  const Type t_int = gwi_mk_type(gwi, "int", SZ_INT, NULL);
97
730
  GWI_BB(gwi_gack(gwi, t_int, gack_int))
98
730
  GWI_BB(gwi_set_global_type(gwi, t_int, et_int))
99
730
  const Type t_char = gwi_mk_type(gwi, "char", SZ_INT, "int");
100
730
  GWI_BB(gwi_gack(gwi, t_char, gack_char))
101
730
  GWI_BB(gwi_set_global_type(gwi, t_char, et_char))
102
730
  const Type t_float = gwi_mk_type(gwi, "float", SZ_FLOAT, NULL);
103
730
  GWI_BB(gwi_gack(gwi, t_float, gack_float))
104
730
  GWI_BB(gwi_set_global_type(gwi, t_float, et_float))
105
730
  const Type t_dur = gwi_mk_type(gwi, "dur", SZ_FLOAT, NULL);
106
730
  GWI_BB(gwi_gack(gwi, t_dur, gack_float))
107
730
  GWI_BB(gwi_add_type(gwi, t_dur))
108
730
  const Type t_time = gwi_mk_type(gwi, "time", SZ_FLOAT, NULL);
109
730
  GWI_BB(gwi_gack(gwi, t_time, gack_float))
110
730
  GWI_BB(gwi_add_type(gwi, t_time))
111
730
  const Type t_now = gwi_mk_type(gwi, "@now", SZ_FLOAT, "time");
112
730
  GWI_BB(gwi_add_type(gwi, t_now))
113
730
  struct SpecialId_ spid = { .type=t_now, .exec=RegPushNow, .is_const=1 };
114
730
  gwi_specialid(gwi, "now", &spid);
115
116
730
  const Type t_compound = gwi_mk_type(gwi, "@Compound", 0, NULL);
117
730
  GWI_BB(gwi_gack(gwi, t_compound, gack_compound))
118
730
  GWI_BB(gwi_set_global_type(gwi, t_compound, et_compound))
119
730
  GWI_BB(import_object(gwi))
120
730
  GWI_BB(import_prim(gwi))
121
730
  const Type t_function = gwi_mk_type(gwi, "@function", SZ_INT, NULL);
122
730
  GWI_BB(gwi_gack(gwi, t_function, gack_function))
123
730
  GWI_BB(gwi_set_global_type(gwi, t_function, et_function))
124
730
  const Type t_fptr = gwi_mk_type(gwi, "@func_ptr", SZ_INT, "@function");
125
730
  GWI_BB(gwi_gack(gwi, t_fptr, gack_fptr))
126
730
  GWI_BB(gwi_set_global_type(gwi, t_fptr, et_fptr))
127
730
  const Type t_lambda = gwi_mk_type(gwi, "@lambda", SZ_INT, "@function");
128
730
  SET_FLAG(t_lambda, infer);
129
730
  GWI_BB(gwi_set_global_type(gwi, t_lambda, et_lambda))
130
131
730
  GWI_BB(gwi_typedef_ini(gwi, "int", "@internal"))
132
730
  GWI_BB(gwi_typedef_end(gwi, ae_flag_none))
133
134
730
  GWI_BB(import_object_op(gwi))
135
730
  GWI_BB(import_values(gwi))
136
137
// TODO: check me
138
730
  const Type t_union = gwi_class_ini(gwi, "@Union", NULL);
139
730
  gwi->gwion->type[et_union] = t_union;
140
730
  GWI_BB(gwi_class_end(gwi))
141
142
730
  GWI_BB(import_array(gwi))
143
730
  GWI_BB(import_event(gwi))
144
730
  GWI_BB(import_ugen(gwi))
145
730
  GWI_BB(import_ptr(gwi))
146
730
  GWI_BB(import_func(gwi))
147
730
  GWI_BB(gwi_oper_ini(gwi, NULL, (m_str)OP_ANY_TYPE, NULL))
148
730
  GWI_BB(gwi_oper_add(gwi, opck_new))
149
730
  GWI_BB(gwi_oper_emi(gwi, opem_new))
150
730
  GWI_BB(gwi_oper_end(gwi, "new", NULL))
151
//  GWI_BB(import_prim(gwi))
152
730
  GWI_BB(import_vararg(gwi))
153
730
  GWI_BB(import_string(gwi))
154
730
  GWI_BB(import_shred(gwi))
155
730
  GWI_BB(import_modules(gwi))
156
157
730
  GWI_BB(gwi_oper_ini(gwi, "@Class", "@Class", "int"))
158
730
  GWI_BB(gwi_oper_end(gwi, "==", int_eq))
159
730
  GWI_BB(gwi_oper_end(gwi, "!=", int_neq))
160
730
  GWI_BB(gwi_oper_end(gwi, ">=", instr_class_ge))
161
730
  GWI_BB(gwi_oper_end(gwi, ">",  instr_class_gt))
162
730
  GWI_BB(gwi_oper_end(gwi, "<=", instr_class_le))
163
730
  GWI_BB(gwi_oper_end(gwi, "<",  instr_class_lt))
164
730
  return GW_OK;
165
}
166
167
730
ANN m_bool type_engine_init(const Gwion gwion, const Vector plug_dirs) {
168
730
  gwion->env->name = "[builtin]";
169
730
  struct loc_t_ loc = {};
170
730
  OperCK oper = {};
171
730
  struct Gwi_ gwi = { .gwion=gwion, .loc=&loc, .oper=&oper };
172
730
  CHECK_BB(import_core_libs(&gwi))
173
730
  push_global(gwion, "[plugins]");
174
730
  gwion->env->name = "[imported]";
175
799
  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
730
  push_global(gwion, "[user]");
181
730
  return GW_OK;
182
}