Skip to content

Commit

Permalink
🎨 define fptr_def and type_def in section
Browse files Browse the repository at this point in the history
  • Loading branch information
fennecdjay committed Jul 23, 2019
1 parent d834002 commit 4c083b6
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 182 deletions.
2 changes: 1 addition & 1 deletion ast
Submodule ast updated from 195d19 to 0edd66
3 changes: 2 additions & 1 deletion include/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ static const _exp_func exp_func[] = {
#define DECL_SECTION_FUNC(prefix) \
static const _exp_func section_func[] = { \
(_exp_func)prefix##_stmt_list, (_exp_func)prefix##_func_def, (_exp_func)prefix##_class_def, \
(_exp_func)prefix##_enum_def, (_exp_func)prefix##_union_def \
(_exp_func)prefix##_enum_def, (_exp_func)prefix##_union_def, \
(_exp_func)prefix##_fptr_def, (_exp_func)prefix##_type_def \
}; \
ANN static inline m_bool prefix##_section(const void* a, /* const */ Section* section) { GWDEBUG_EXE \
void* d = &section->d.stmt_list; \
Expand Down
20 changes: 10 additions & 10 deletions include/traverse.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ ANN m_bool traverse_class_def(const Env, const Class_Def);
ANN m_bool traverse_func_def(const Env, const Func_Def);
ANN m_bool traverse_union_def(const Env, const Union_Def);
ANN m_bool traverse_enum_def(const Env, const Enum_Def);
ANN m_bool traverse_stmt_fptr(const Env, const Stmt_Fptr );
ANN m_bool traverse_stmt_type(const Env env, const Stmt_Type def);
ANN m_bool traverse_fptr_def(const Env, const Fptr_Def);
ANN m_bool traverse_type_def(const Env env, const Type_Def);
ANN m_bool traverse_decl(const Env, const Exp_Decl*);

ANN m_bool scan0_ast(const Env, Ast);
Expand All @@ -22,10 +22,10 @@ ANN m_bool scan1_func_def(const Env, const Func_Def);
ANN m_bool scan2_func_def(const Env, const Func_Def);
ANN m_bool check_func_def(const Env, const Func_Def);

ANN m_bool scan0_stmt_fptr(const Env, const Stmt_Fptr);
ANN m_bool scan1_stmt_fptr(const Env, const Stmt_Fptr);
ANN m_bool scan2_stmt_fptr(const Env, const Stmt_Fptr);
//ANN m_bool check_stmt_fptr(const Env, const Stmt_Fptr);
ANN m_bool scan0_fptr_def(const Env, const Fptr_Def);
ANN m_bool scan1_fptr_def(const Env, const Fptr_Def);
ANN m_bool scan2_fptr_def(const Env, const Fptr_Def);
//ANN m_bool check_fptr_def(const Env, const Fptr);

ANN m_bool scan0_union_def(const Env, const Union_Def);
ANN m_bool scan1_union_def(const Env, const Union_Def);
Expand All @@ -37,10 +37,10 @@ ANN m_bool scan1_enum_def(const Env, const Enum_Def);
//ANN m_bool scan2_enum_def(const Env, const Enum_Def);
ANN m_bool check_enum_def(const Env, const Enum_Def);

ANN m_bool scan0_stmt_type(const Env, const Stmt_Type);
ANN m_bool scan1_stmt_type(const Env, const Stmt_Type);
ANN m_bool scan2_stmt_type(const Env, const Stmt_Type);
ANN m_bool check_stmt_type(const Env, const Stmt_Type);
ANN m_bool scan0_type_def(const Env, const Type_Def);
ANN m_bool scan1_type_def(const Env, const Type_Def);
ANN m_bool scan2_type_def(const Env, const Type_Def);
ANN m_bool check_type_def(const Env, const Type_Def);

ANN m_bool scan0_class_def(const Env, const Class_Def);
ANN m_bool scan1_class_def(const Env, const Class_Def);
Expand Down
6 changes: 3 additions & 3 deletions src/emit/emit.c
Original file line number Diff line number Diff line change
Expand Up @@ -1338,8 +1338,8 @@ ANN static m_bool emit_stmt_case(const Emitter emit, const Stmt_Exp stmt) {
return GW_OK;
}

ANN static m_bool emit_stmt_type(const Emitter emit, const Stmt_Type stmt) {
return stmt->type->e->def ? emit_class_def(emit, stmt->type->e->def) : 1;
ANN static m_bool emit_type_def(const Emitter emit, const Type_Def tdef) {
return tdef->type->e->def ? emit_class_def(emit, tdef->type->e->def) : 1;
}

ANN static m_bool emit_enum_def(const Emitter emit, const Enum_Def edef) {
Expand Down Expand Up @@ -1430,7 +1430,6 @@ static const _exp_func stmt_func[] = {
(_exp_func)emit_stmt_if, (_exp_func)emit_stmt_code, (_exp_func)emit_stmt_switch,
(_exp_func)emit_stmt_break, (_exp_func)emit_stmt_continue, (_exp_func)emit_stmt_return,
(_exp_func)emit_stmt_case, (_exp_func)emit_stmt_jump,
(_exp_func)dummy_func, (_exp_func)emit_stmt_type
};

ANN static m_bool emit_stmt(const Emitter emit, const Stmt stmt, const m_bool pop) {
Expand Down Expand Up @@ -1715,6 +1714,7 @@ ANN static m_bool emit_func_def(const Emitter emit, const Func_Def fdef) {
return GW_OK;
}

#define emit_fptr_def dummy_func
DECL_SECTION_FUNC(emit)

ANN Code* emit_class_code(const Emitter emit, const m_str name) {
Expand Down
24 changes: 12 additions & 12 deletions src/lib/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ ANN m_int gwi_fptr_ini(const Gwi gwi, const restrict m_str type, const restrict
return GW_OK;
}

ANN static Stmt import_fptr(const Gwi gwi, DL_Func* dl_fun, ae_flag flag) {
ANN static Fptr_Def import_fptr(const Gwi gwi, DL_Func* dl_fun, ae_flag flag) {
const Env env = gwi->gwion->env;
m_uint array_depth;
ID_List type_path;
Expand All @@ -529,18 +529,18 @@ ANN static Stmt import_fptr(const Gwi gwi, DL_Func* dl_fun, ae_flag flag) {
!(type_decl = new_type_decl(env->gwion->mp, type_path)))
GWI_ERR_O(_(" ... during fptr import '%s' (type)."), dl_fun->name);
Func_Base *base = new_func_base(env->gwion->mp, type_decl, insert_symbol(env->gwion->st, dl_fun->name), args);
return new_stmt_fptr(env->gwion->mp, base, flag);
return new_fptr_def(env->gwion->mp, base, flag);
}

ANN Type gwi_fptr_end(const Gwi gwi, const ae_flag flag) {
const Stmt stmt = import_fptr(gwi, &gwi->func, flag);
CHECK_BO(traverse_stmt_fptr(gwi->gwion->env, &stmt->d.stmt_fptr))
const Fptr_Def fptr = import_fptr(gwi, &gwi->func, flag);
CHECK_BO(traverse_fptr_def(gwi->gwion->env, fptr))
if(gwi->gwion->env->class_def)
SET_FLAG(stmt->d.stmt_fptr.base->func->def, builtin);
SET_FLAG(fptr->base->func->def, builtin);
else
SET_FLAG(stmt->d.stmt_fptr.base->func, builtin);
const Type t = stmt->d.stmt_fptr.type;
free_stmt(gwi->gwion->mp, stmt);
SET_FLAG(fptr->base->func, builtin);
const Type t = fptr->type;
free_fptr_def(gwi->gwion->mp, fptr);
return t;
}

Expand All @@ -556,10 +556,10 @@ ANN Type gwi_typedef_end(const Gwi gwi, const ae_flag flag) {
GWI_ERR_O(_("'%s' : invalid type"), gwi->val.type);
td->flag |= flag;
const Symbol sym = insert_symbol(gwi->gwion->st, gwi->val.name);
const Stmt stmt = new_stmt_type(gwi->gwion->mp, td, sym);
traverse_stmt_type(gwi->gwion->env, &stmt->d.stmt_type);
const Type t = stmt->d.stmt_type.type;
free_stmt(gwi->gwion->mp, stmt);
const Type_Def tdef = new_type_def(gwi->gwion->mp, td, sym);
traverse_type_def(gwi->gwion->env, tdef);
const Type t = tdef->type;
free_type_def(gwi->gwion->mp, tdef);
return t;
}

Expand Down
16 changes: 8 additions & 8 deletions src/parse/check.c
Original file line number Diff line number Diff line change
Expand Up @@ -494,12 +494,12 @@ CHECK_BO(check_call(env, exp))
fbase->tmpl->base = 0;
fbase->tmpl->call = types;
if(template_push_types(env, fbase->tmpl) > 0) {
const Stmt stmt = new_stmt_fptr(env->gwion->mp, fbase, base->flag);
const Fptr_Def fptr = new_fptr_def(env->gwion->mp, fbase, base->flag);
if(value) {
stmt->d.stmt_fptr.type = actual_type(value->type);
stmt->d.stmt_fptr.value = value;
fptr->type = actual_type(value->type);
fptr->value = value;
}
if(traverse_stmt_fptr(env, &stmt->d.stmt_fptr) > 0 &&
if(traverse_fptr_def(env, fptr) > 0 &&
(base->base->ret_type = known_type(env, base->base->td)) &&
(!exp->args || !!check_exp(env, exp->args))) {
m_func = find_func_match(env, fbase->func, exp->args);
Expand All @@ -508,7 +508,7 @@ CHECK_BO(check_call(env, exp))
map_set(&v->owner->info->type->map, (vtype)sym, (vtype)actual_type(m_func->value_ref->type));
}
}
free_stmt(env->gwion->mp, stmt);
free_fptr_def(env->gwion->mp, fptr);
}
} else {
for(m_uint i = 0; i < v->offset + 1; ++i) {
Expand Down Expand Up @@ -835,8 +835,8 @@ ANN static Type check_exp_dot(const Env env, Exp_Dot* member) {
return value->type;
}

ANN m_bool check_stmt_type(const Env env, const Stmt_Type stmt) {
return stmt->type->e->def ? check_class_def(env, stmt->type->e->def) : GW_OK;
ANN m_bool check_type_def(const Env env, const Type_Def tdef) {
return tdef->type->e->def ? check_class_def(env, tdef->type->e->def) : GW_OK;
}
ANN static Type check_exp_lambda(const Env env NUSED,
const Exp_If* exp_if NUSED) { return t_lambda; }
Expand Down Expand Up @@ -1088,7 +1088,6 @@ static const _exp_func stmt_func[] = {
(_exp_func)check_stmt_if, (_exp_func)check_stmt_code, (_exp_func)check_stmt_switch,
(_exp_func)check_stmt_break, (_exp_func)check_stmt_continue, (_exp_func)check_stmt_return,
(_exp_func)check_stmt_case, (_exp_func)check_stmt_jump,
(_exp_func)dummy_func, (_exp_func)check_stmt_type
};

ANN m_bool check_stmt(const Env env, const Stmt stmt) {
Expand Down Expand Up @@ -1244,6 +1243,7 @@ ANN m_bool check_func_def(const Env env, const Func_Def fdef) {
return ret;
}

#define check_fptr_def dummy_func
DECL_SECTION_FUNC(check)

ANN static m_bool check_class_parent(const Env env, const Class_Def cdef) {
Expand Down
36 changes: 19 additions & 17 deletions src/parse/cpy_ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ ANN static void cpy_stmt_if(MemPool p, Stmt_If a, const Stmt_If src) {
a->else_body = cpy_stmt(p, src->else_body);
}

ANN static void cpy_stmt_jump(MemPool p, const Stmt_Jump a,const Stmt_Jump src) {
ANN static void cpy_stmt_jump(MemPool p NUSED, const Stmt_Jump a,const Stmt_Jump src) {
a->name = src->name;
}

Expand Down Expand Up @@ -333,18 +333,26 @@ ANN Func_Base* cpy_func_base(MemPool p, const Func_Base* src) {
return a;
}

ANN static void cpy_stmt_fptr(MemPool p, Stmt_Fptr a, const Stmt_Fptr src) {
ANN static Fptr_Def cpy_fptr_def(MemPool p, const Fptr_Def src) {
Fptr_Def a = mp_calloc(p, Fptr_Def);
a->base = cpy_func_base(p, src->base);
return a;
}

ANN static void cpy_stmt_type(MemPool p, Stmt_Type a, const Stmt_Type src) {
ANN static void cpy_type_def2(MemPool p, Type_Def a, const Type_Def src) {
if(src->ext)
a->ext = cpy_type_decl(p, src->ext);
a->xid = src->xid;
if(src->tmpl)
a->tmpl = cpy_tmpl(p, src->tmpl);
}

ANN static Type_Def cpy_type_def(MemPool p, const Type_Def src) {
Type_Def a = mp_calloc(p, Type_Def);
cpy_type_def2(p, a, src);
return a;
}

ANN static Union_Def cpy_union_def(MemPool p, const Union_Def src) {
Union_Def a = mp_calloc(p, Union_Def);
a->l = cpy_decl_list(p, src->l); // 1
Expand All @@ -363,14 +371,8 @@ ANN static Stmt cpy_stmt(MemPool p, const Stmt src) {
switch(src->stmt_type) {
case ae_stmt_case:
case ae_stmt_exp:
if(src->d.stmt_exp.val)
a->d.stmt_exp.val = cpy_exp(p, src->d.stmt_exp.val);

// cpy_stmt_exp(p, &a->d.stmt_exp, &src->d.stmt_exp);
break;
case ae_stmt_return:
if(&src->d.stmt_exp)
cpy_stmt_exp(p, &a->d.stmt_exp, &src->d.stmt_exp);
cpy_stmt_exp(p, &a->d.stmt_exp, &src->d.stmt_exp);
break;
case ae_stmt_code:
cpy_stmt_code(p, &a->d.stmt_code, &src->d.stmt_code);
Expand All @@ -397,12 +399,6 @@ ANN static Stmt cpy_stmt(MemPool p, const Stmt src) {
case ae_stmt_switch:
cpy_stmt_switch(p, &a->d.stmt_switch, &src->d.stmt_switch);
break;
case ae_stmt_fptr:
cpy_stmt_fptr(p, &a->d.stmt_fptr, &src->d.stmt_fptr);
break;
case ae_stmt_type:
cpy_stmt_type(p, &a->d.stmt_type, &src->d.stmt_type);
break;
case ae_stmt_break:
case ae_stmt_continue:
break;
Expand Down Expand Up @@ -451,6 +447,12 @@ ANN static Section* cpy_section(MemPool p, const Section *src) {
case ae_section_union:
a->d.union_def = cpy_union_def(p, src->d.union_def);
break;
case ae_section_fptr:
a->d.fptr_def = cpy_fptr_def(p, src->d.fptr_def);
break;
case ae_section_type:
a->d.type_def = cpy_type_def(p, src->d.type_def);
break;
}
a->section_type = src->section_type;
return a;
Expand All @@ -466,7 +468,7 @@ ANN static Class_Body cpy_class_body(MemPool p, const Class_Body src) {

ANN Class_Def cpy_class_def(MemPool p, const Class_Def src) {
Class_Def a = mp_calloc(p, Class_Def);
cpy_stmt_type(p, &a->base, &src->base);
cpy_type_def2(p, &a->base, &src->base);
if(src->body) {
if(!GET_FLAG(src, union))
a->body = cpy_class_body(p, src->body);
Expand Down

0 comments on commit 4c083b6

Please sign in to comment.