From c7881246cef8432c54f0e739bab9d8356150773b Mon Sep 17 00:00:00 2001 From: fennecdjay Date: Thu, 24 Oct 2019 18:09:25 +0200 Subject: [PATCH] :art: split import => enum --- include/import.h | 4 +-- include/import/enum.h | 8 +++++ src/import/enum.c | 71 +++++++++++++++++++++++++++++++++++++++++++ src/lib/import.c | 48 ----------------------------- 4 files changed, 80 insertions(+), 51 deletions(-) create mode 100644 include/import/enum.h create mode 100644 src/import/enum.c diff --git a/include/import.h b/include/import.h index 6a648a95..78fae8f3 100644 --- a/include/import.h +++ b/include/import.h @@ -57,9 +57,7 @@ ANN2(1) m_int gwi_union_ini(const Gwi gwi, const m_str type, const m_str name); ANN m_int gwi_union_add(const Gwi gwi, const __restrict__ m_str type, const __restrict__ m_str name); ANN Type gwi_union_end(const Gwi gwi, const ae_flag flag); -ANN2(1) m_int gwi_enum_ini(const Gwi gwi, const m_str type); -ANN m_int gwi_enum_add(const Gwi gwi, const m_str name, const m_uint value); -ANN Type gwi_enum_end(const Gwi gwi); +#include "import/enum.h" ANN m_int gwi_func_ini(const Gwi gwi, const __restrict__ m_str type, const __restrict__ m_str name, const f_xfun addr); ANN m_int gwi_func_arg(const Gwi gwi, const __restrict__ m_str t, const __restrict__ m_str n); diff --git a/include/import/enum.h b/include/import/enum.h new file mode 100644 index 00000000..8eb4480a --- /dev/null +++ b/include/import/enum.h @@ -0,0 +1,8 @@ +#ifndef __IMPORT_ENUM +#define __IMPORT_ENUM + +ANN2(1) m_int gwi_enum_ini(const Gwi gwi, const m_str type); +ANN m_int gwi_enum_add(const Gwi gwi, const m_str name, const m_uint value); +ANN Type gwi_enum_end(const Gwi gwi); + +#endif diff --git a/src/import/enum.c b/src/import/enum.c new file mode 100644 index 00000000..55327ae1 --- /dev/null +++ b/src/import/enum.c @@ -0,0 +1,71 @@ +#include +#include +#include +#include "gwion_util.h" +#include "gwion_ast.h" +#include "oo.h" +#include "vm.h" +#include "env.h" +#include "type.h" +#include "value.h" +#include "traverse.h" +#include "instr.h" +#include "object.h" +#include "emit.h" +#include "func.h" +#include "nspc.h" +#include "gwion.h" +#include "operator.h" +#include "import.h" +#include "gwi.h" + +ANN2(1) m_int gwi_enum_ini(const Gwi gwi, const m_str type) { + gwi->enum_data.t = type; + vector_init(&gwi->enum_data.addr); + return GW_OK; +} + +ANN m_int gwi_enum_add(const Gwi gwi, const m_str name, const m_uint i) { + CHECK_BB(name_valid(gwi, name)) + const Gwion gwion = gwi->gwion; + const ID_List list = new_id_list(gwion->mp, insert_symbol(gwion->st, name), + loc_cpy(gwion->mp, gwi->loc)); + DL_Enum* d = &gwi->enum_data; + ALLOC_PTR(gwion->mp, addr, m_int, i); + vector_add(&gwi->enum_data.addr, (vtype)addr); + if(!d->base) + d->base = list; + else + d->curr->next = list; + d->curr = list; + return list ? 1 : -1; +} + +ANN static void import_enum_end(const Gwi gwi, const Vector v) { + DL_Enum* d = &gwi->enum_data; + for(m_uint i = 0; i < vector_size(v); i++) { + Value value = (Value)vector_at(v, i); + const m_uint addr = vector_at(&d->addr, i); + SET_FLAG(value, builtin); + ADD_REF(value->type); + if(!gwi->gwion->env->class_def) + value->d.ptr = (m_uint*)(addr ? addr : i); + else + value->d.ptr = (m_uint*)(addr ? *(m_uint*)addr : i); + } + d->t = NULL; + d->base = NULL; + vector_release(&d->addr); +} + +ANN Type gwi_enum_end(const Gwi gwi) { + const Gwion gwion = gwi->gwion; + DL_Enum* d = &gwi->enum_data; + const Enum_Def edef = new_enum_def(gwion->mp, d->base, + d->t ? insert_symbol(gwion->st, d->t) : NULL, loc_cpy(gwion->mp, gwi->loc)); + const m_bool ret = traverse_enum_def(gwion->env, edef); + import_enum_end(gwi, &edef->values); + const Type t = ret > 0 ? edef->t : NULL; + free_enum_def(gwion->mp, edef); + return t; +} diff --git a/src/lib/import.c b/src/lib/import.c index f0b07352..fcb311bc 100644 --- a/src/lib/import.c +++ b/src/lib/import.c @@ -703,54 +703,6 @@ ANN Type gwi_union_end(const Gwi gwi, const ae_flag flag) { return t; } -ANN2(1) m_int gwi_enum_ini(const Gwi gwi, const m_str type) { - gwi->enum_data.t = type; - vector_init(&gwi->enum_data.addr); - return GW_OK; -} - -ANN m_int gwi_enum_add(const Gwi gwi, const m_str name, const m_uint i) { - CHECK_BB(name_valid(gwi, name)) - const ID_List list = new_id_list(gwi->gwion->mp, insert_symbol(gwi->gwion->st, name), loc_cpy(gwi->gwion->mp, gwi->loc)); - DL_Enum* d = &gwi->enum_data; - ALLOC_PTR(gwi->gwion->mp, addr, m_int, i); - vector_add(&gwi->enum_data.addr, (vtype)addr); - if(!d->base) - d->base = list; - else - d->curr->next = list; - d->curr = list; - return list ? 1 : -1; -} - -ANN static void import_enum_end(const Gwi gwi, const Vector v) { - DL_Enum* d = &gwi->enum_data; - for(m_uint i = 0; i < vector_size(v); i++) { - Value value = (Value)vector_at(v, i); - const m_uint addr = vector_at(&d->addr, i); - SET_FLAG(value, builtin); - ADD_REF(value->type); - if(!gwi->gwion->env->class_def) - value->d.ptr = (m_uint*)(addr ? addr : i); - else - value->d.ptr = (m_uint*)(addr ? *(m_uint*)addr : i); - } - d->t = NULL; - d->base = NULL; - vector_release(&d->addr); -} - -ANN Type gwi_enum_end(const Gwi gwi) { - DL_Enum* d = &gwi->enum_data; - const Enum_Def edef = new_enum_def(gwi->gwion->mp, d->base, d->t ? insert_symbol(gwi->gwion->st, d->t) : NULL, - loc_cpy(gwi->gwion->mp, gwi->loc)); - const m_bool ret = traverse_enum_def(gwi->gwion->env, edef); - import_enum_end(gwi, &edef->values); - const Type t = ret > 0 ? edef->t : NULL; - free_enum_def(gwi->gwion->mp, edef); - return t; -} - ANN void register_freearg(const Gwi gwi, const f_instr _exec, const f_freearg _free) { map_set(&gwi->gwion->data->freearg, (vtype)_exec, (vtype)_free); } -- 2.43.0