1 |
|
|
#include "gwion_util.h" |
2 |
|
|
#include "gwion_ast.h" |
3 |
|
|
#include "gwion_env.h" |
4 |
|
|
#include "vm.h" |
5 |
|
|
#include "gwion.h" |
6 |
|
|
#include "operator.h" |
7 |
|
|
#include "traverse.h" |
8 |
|
|
#include "vm.h" |
9 |
|
|
#include "parse.h" |
10 |
|
|
|
11 |
|
716 |
ANN static struct Env_Scope_ *new_envscope(MemPool p) { |
12 |
|
716 |
struct Env_Scope_ *a = mp_calloc(p, Env_Scope); |
13 |
|
716 |
vector_init(&a->breaks); |
14 |
|
716 |
vector_init(&a->conts); |
15 |
|
716 |
vector_init(&a->class_stack); |
16 |
|
716 |
vector_init(&a->nspc_stack); |
17 |
|
716 |
vector_init(&a->known_ctx); |
18 |
|
716 |
return a; |
19 |
|
|
} |
20 |
|
|
|
21 |
|
716 |
Env new_env(MemPool p) { |
22 |
|
716 |
const Env env = (Env)xmalloc(sizeof(struct Env_)); |
23 |
|
716 |
env->global_nspc = new_nspc(p, "global_nspc"); |
24 |
|
716 |
env->context = NULL; |
25 |
|
716 |
env->scope = new_envscope(p); |
26 |
|
716 |
env_reset(env); |
27 |
|
716 |
return env; |
28 |
|
|
} |
29 |
|
|
|
30 |
|
1375 |
ANN void env_reset(const Env env) { |
31 |
|
1375 |
vector_clear(&env->scope->breaks); |
32 |
|
1375 |
vector_clear(&env->scope->conts); |
33 |
|
1375 |
vector_clear(&env->scope->nspc_stack); |
34 |
|
1375 |
vector_add(&env->scope->nspc_stack, (vtype)env->global_nspc); |
35 |
|
1375 |
vector_clear(&env->scope->class_stack); |
36 |
|
1375 |
vector_add(&env->scope->class_stack, (vtype)NULL); |
37 |
|
1375 |
env->curr = env->global_nspc; |
38 |
|
1375 |
env->class_def = NULL; |
39 |
|
1375 |
env->func = NULL; |
40 |
|
1375 |
env->scope->depth = 0; |
41 |
|
1375 |
} |
42 |
|
|
|
43 |
|
715 |
ANN void release_ctx(struct Env_Scope_ *a, struct Gwion_ *gwion) { |
44 |
|
715 |
const m_uint size = vector_size(&a->known_ctx); |
45 |
✓✓ |
1852 |
for(m_uint i = size + 1; --i;) |
46 |
|
422 |
REM_REF((Context)vector_at(&a->known_ctx, i - 1), gwion); |
47 |
|
715 |
} |
48 |
|
|
|
49 |
|
715 |
ANN static void free_env_scope(struct Env_Scope_ *a, Gwion gwion) { |
50 |
|
715 |
release_ctx(a, gwion); |
51 |
|
715 |
vector_release(&a->known_ctx); |
52 |
|
715 |
vector_release(&a->nspc_stack); |
53 |
|
715 |
vector_release(&a->class_stack); |
54 |
|
715 |
vector_release(&a->breaks); |
55 |
|
715 |
vector_release(&a->conts); |
56 |
|
715 |
mp_free(gwion->mp, Env_Scope, a); |
57 |
|
715 |
} |
58 |
|
|
|
59 |
|
715 |
ANN void free_env(const Env a) { |
60 |
|
715 |
free_env_scope(a->scope, a->gwion); |
61 |
✓✓ |
715 |
while(pop_global(a->gwion)); |
62 |
|
715 |
xfree(a); |
63 |
|
715 |
} |
64 |
|
|
|
65 |
|
14207 |
ANN2(1,3) m_uint env_push(const Env env, const Type type, const Nspc nspc) { |
66 |
|
14207 |
const m_uint scope = env->scope->depth; |
67 |
|
14207 |
vector_add(&env->scope->class_stack, (vtype)env->class_def); |
68 |
|
14207 |
env->class_def = type; |
69 |
|
14207 |
vector_add(&env->scope->nspc_stack, (vtype)env->curr); |
70 |
|
14207 |
env->curr = nspc; |
71 |
|
14207 |
env->scope->depth = 0; |
72 |
|
14207 |
return scope; |
73 |
|
|
} |
74 |
|
|
|
75 |
|
14200 |
ANN void env_pop(const Env env, const m_uint scope) { |
76 |
|
14200 |
env->class_def = (Type)vector_pop(&env->scope->class_stack); |
77 |
|
14200 |
env->curr = (Nspc)vector_pop(&env->scope->nspc_stack); |
78 |
|
14200 |
env->scope->depth = scope; |
79 |
|
14200 |
} |
80 |
|
|
|
81 |
|
27115 |
ANN void env_add_type(const Env env, const Type type) { |
82 |
|
27115 |
const Type v_type = type_copy(env->gwion->mp, env->gwion->type[et_class]); |
83 |
|
27115 |
ADD_REF(v_type); |
84 |
|
27115 |
v_type->e->d.base_type = type; |
85 |
|
27115 |
SET_FLAG(type, builtin); |
86 |
|
27115 |
const Symbol sym = insert_symbol(type->name); |
87 |
|
27115 |
nspc_add_type_front(env->curr, sym, type); |
88 |
|
27115 |
const Value v = new_value(env->gwion->mp, v_type, s_name(sym)); |
89 |
|
27115 |
SET_FLAG(v, valid | ae_flag_const | ae_flag_global | ae_flag_builtin); |
90 |
|
27115 |
nspc_add_value(env->curr, insert_symbol(type->name), v); |
91 |
|
27115 |
v->from->owner = type->e->owner = env->curr; |
92 |
|
27115 |
v->from->owner_class = type->e->owner_class = env->class_def; // t owner_class ? |
93 |
|
27115 |
type->xid = ++env->scope->type_xid; |
94 |
|
27115 |
} |
95 |
|
|
|
96 |
|
622 |
ANN m_bool type_engine_check_prog(const Env env, const Ast ast) { |
97 |
|
622 |
const Context ctx = new_context(env->gwion->mp, ast, env->name); |
98 |
|
622 |
env_reset(env); |
99 |
|
622 |
load_context(ctx, env); |
100 |
|
622 |
return traverse_ast(env, ast); |
101 |
|
|
} |
102 |
|
|
|
103 |
|
622 |
ANN m_bool type_engine_clean_prog(const Env env, m_bool *r) { |
104 |
|
622 |
const m_bool ret = (m_bool)*r; |
105 |
|
622 |
const Context ctx = env->context; |
106 |
✓✓ |
622 |
if(ret > 0) //{ |
107 |
|
418 |
nspc_commit(env->curr); |
108 |
✓✓✓✓
|
622 |
if(ret > 0 || env->context->global) |
109 |
|
422 |
vector_add(&env->scope->known_ctx, (vtype)ctx); |
110 |
|
|
else //nspc_rollback(env->global_nspc); |
111 |
|
200 |
REM_REF(ctx, env->gwion); |
112 |
|
622 |
unload_context(ctx, env); |
113 |
|
622 |
return ret; |
114 |
|
|
} |