From 3c7b79003683aebe7b91c62633ca918a4b11135e Mon Sep 17 00:00:00 2001 From: fennecdjay Date: Fri, 17 May 2019 13:21:53 +0200 Subject: [PATCH] :art: Unify class_body parsing --- include/parse.h | 6 ++++++ src/emit/emit.c | 13 ++----------- src/parse/check.c | 15 +-------------- src/parse/scan0.c | 13 ++----------- src/parse/scan1.c | 15 +-------------- src/parse/scan2.c | 15 +-------------- src/parse/scanx.c | 37 +++++++++++++++++++++++++++++++++++++ 7 files changed, 50 insertions(+), 64 deletions(-) create mode 100644 src/parse/scanx.c diff --git a/include/parse.h b/include/parse.h index b88f34ac..c3ef0eb6 100644 --- a/include/parse.h +++ b/include/parse.h @@ -63,4 +63,10 @@ ANN static m_bool prefix##_stmt_##name(const Env env, const type stmt) { GWDEBUG ANN m_uint union_push(const Env, const Stmt_Union); ANN void union_pop(const Env, const Stmt_Union, const m_uint); ANN m_bool check_stmt(const Env env, const Stmt stmt); + +ANN m_bool scanx_body(const Env env, const Class_Def cdef, const _exp_func f, void* data); +static inline ANN m_bool env_body(const Env env, const Class_Def cdef, const _exp_func f) { + return scanx_body(env, cdef, f, env); +} +#define env_body(a,b,c) env_body(a,b,(_exp_func)c) #endif diff --git a/src/emit/emit.c b/src/emit/emit.c index 527693c9..b33b1f09 100644 --- a/src/emit/emit.c +++ b/src/emit/emit.c @@ -1765,17 +1765,8 @@ ANN static m_bool emit_class_def(const Emitter emit, const Class_Def cdef) { emit_class_code(emit, type->name); if(cdef->base.ext && cdef->base.ext->array) CHECK_BB(emit_array_extend(emit, type->e->parent, cdef->base.ext->array->exp)) - if(cdef->body) { - const m_uint scope = env_push_type(emit->env, cdef->base.type); - if(cdef->tmpl) - template_push_types(emit->env, cdef->tmpl->list.list, cdef->tmpl->base); - Class_Body body = cdef->body; - do CHECK_BB(emit_section(emit, body->section)) - while((body = body->next)); - if(cdef->tmpl) - nspc_pop_type(emit->env->gwion->mp, emit->env->curr); - env_pop(emit->env, scope); - } + if(cdef->body) + CHECK_BB(scanx_body(emit->env, cdef, (_exp_func)emit_section, emit)) emit_class_finish(emit, nspc); SET_FLAG(cdef->base.type->nspc->pre_ctor, ctor); emit_class_pop(emit); diff --git a/src/parse/check.c b/src/parse/check.c index a0f886d3..3e8f8c4a 100644 --- a/src/parse/check.c +++ b/src/parse/check.c @@ -1172,19 +1172,6 @@ ANN static m_bool check_class_parent(const Env env, const Class_Def cdef) { return GW_OK; } -ANN static m_bool check_class_body(const Env env, const Class_Def cdef) { - const m_uint scope = env_push_type(env, cdef->base.type); - Class_Body body = cdef->body; - if(cdef->tmpl) - template_push_types(env, cdef->tmpl->list.list, cdef->tmpl->base); - do CHECK_BB(check_section(env, body->section)) - while((body = body->next)); - if(cdef->tmpl) - nspc_pop_type(env->gwion->mp, env->curr); - env_pop(env, scope); - return GW_OK; -} - ANN static inline void inherit(const Type t) { const Nspc nspc = t->nspc, parent = t->e->parent->nspc; nspc->info->offset = parent->info->offset; @@ -1206,7 +1193,7 @@ ANN m_bool check_class_def(const Env env, const Class_Def cdef) { type->e->parent = t_object; inherit(type); if(cdef->body) - CHECK_BB(check_class_body(env, cdef)) + CHECK_BB(env_body(env, cdef, check_section)) SET_FLAG(type, checked | ae_flag_check); return GW_OK; } diff --git a/src/parse/scan0.c b/src/parse/scan0.c index 0061bf37..95728044 100644 --- a/src/parse/scan0.c +++ b/src/parse/scan0.c @@ -226,17 +226,8 @@ ANN static m_bool scan0_section(const Env env, const Section* section) { ANN m_bool scan0_class_def(const Env env, const Class_Def cdef) { CHECK_BB(scan0_class_def_pre(env, cdef)) CHECK_OB((cdef->base.type = scan0_class_def_init(env, cdef))) - if(cdef->body) { - Class_Body body = cdef->body; - const m_uint scope = env_push_type(env, cdef->base.type); - if(cdef->tmpl) - template_push_types(env, cdef->tmpl->list.list, cdef->tmpl->base); - do CHECK_BB(scan0_section(env, body->section)) - while((body = body->next)); - if(cdef->tmpl) - nspc_pop_type(env->gwion->mp, env->curr); - env_pop(env, scope); - } + if(cdef->body) + CHECK_BB(env_body(env, cdef, scan0_section)) (void)mk_class(env, cdef->base.type); if(GET_FLAG(cdef, global)) env->curr = (Nspc)vector_pop(&env->scope->nspc_stack); diff --git a/src/parse/scan1.c b/src/parse/scan1.c index 63b7f30f..ee1069e3 100644 --- a/src/parse/scan1.c +++ b/src/parse/scan1.c @@ -335,19 +335,6 @@ ANN static m_bool scan1_class_parent(const Env env, const Class_Def cdef) { return GW_OK; } -ANN static m_bool scan1_class_body(const Env env, const Class_Def cdef) { - const m_uint scope = env_push_type(env, cdef->base.type); - Class_Body body = cdef->body; - if(cdef->tmpl) - template_push_types(env, cdef->tmpl->list.list, cdef->tmpl->base); - do CHECK_BB(scan1_section(env, body->section)) - while((body = body->next)); - if(cdef->tmpl) - nspc_pop_type(env->gwion->mp, env->curr); - env_pop(env, scope); - return GW_OK; -} - ANN m_bool scan1_class_def(const Env env, const Class_Def cdef) { if(!cdef->base.type) CHECK_BB(scan0_class_def(env, cdef)) @@ -356,7 +343,7 @@ ANN m_bool scan1_class_def(const Env env, const Class_Def cdef) { if(cdef->base.ext) CHECK_BB(scan1_class_parent(env, cdef)) if(cdef->body) - CHECK_BB(scan1_class_body(env, cdef)) + CHECK_BB(env_body(env, cdef, scan1_section)) SET_FLAG(cdef->base.type, scan1); return GW_OK; } diff --git a/src/parse/scan2.c b/src/parse/scan2.c index 6d9230e0..bc4981a0 100644 --- a/src/parse/scan2.c +++ b/src/parse/scan2.c @@ -548,26 +548,13 @@ ANN static m_bool scan2_class_parent(const Env env, const Class_Def cdef) { return GW_OK; } -ANN static m_bool scan2_class_body(const Env env, const Class_Def cdef) { - const m_uint scope = env_push_type(env, cdef->base.type); - Class_Body body = cdef->body; - if(cdef->tmpl) - template_push_types(env, cdef->tmpl->list.list, cdef->tmpl->base); - do CHECK_BB(scan2_section(env, body->section)) - while((body = body->next)); - if(cdef->tmpl) - nspc_pop_type(env->gwion->mp, env->curr); - env_pop(env, scope); - return GW_OK; -} - ANN m_bool scan2_class_def(const Env env, const Class_Def cdef) { if(tmpl_class_base(cdef->tmpl)) return GW_OK; if(cdef->base.ext) CHECK_BB(scan2_class_parent(env, cdef)) if(cdef->body) - CHECK_BB(scan2_class_body(env, cdef)) + CHECK_BB(env_body(env, cdef, scan2_section)) SET_FLAG(cdef->base.type, scan2); return GW_OK; } diff --git a/src/parse/scanx.c b/src/parse/scanx.c new file mode 100644 index 00000000..5762760b --- /dev/null +++ b/src/parse/scanx.c @@ -0,0 +1,37 @@ +#include +#include "gwion_util.h" +#include "gwion_ast.h" +#include "oo.h" +#include "env.h" +#include "type.h" +#include "nspc.h" +#include "vm.h" +#include "parse.h" +#include "template.h" + +ANN static inline m_bool _body(const Env e, Class_Body b, const _exp_func f) { + do CHECK_BB(f(e, b->section)) + while((b = b->next)); + return GW_OK; +} + +ANN static inline m_uint _push(const Env e, const Class_Def c) { + const m_uint scope = env_push_type(e, c->base.type); + if(c->tmpl) + template_push_types(e, c->tmpl->list.list, c->tmpl->base); + return scope; +} + +ANN static inline void _pop(const Env e, const Class_Def c, const m_uint s) { + if(c->tmpl) + nspc_pop_type(e->gwion->mp, e->curr); + env_pop(e, s); +} + +ANN m_bool +scanx_body(const Env e, const Class_Def c, const _exp_func f, void* d) { + const m_uint scope = _push(e, c); + const m_bool ret = _body(d, c->body, f); + _pop(e, c, scope); + return ret; +} -- 2.43.0