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