From 62ae5e8bc6124d09cadf43c594957a1eb95f6d5e Mon Sep 17 00:00:00 2001 From: fennecdjay Date: Thu, 24 Oct 2019 17:43:49 +0200 Subject: [PATCH] :art: Begin import splitting --- include/gwi.h | 3 ++ include/import.h | 1 + include/import/internals.h | 13 +++++ src/import/internals.c | 54 ++++++++++++++++++++ src/import/item.c | 80 +++++++++++++++++++++++++++++ src/lib/import.c | 101 ------------------------------------- tests/import/coverage.c | 3 ++ tests/import/coverage.gw | 2 + 8 files changed, 156 insertions(+), 101 deletions(-) create mode 100644 include/import/internals.h create mode 100644 src/import/internals.c create mode 100644 src/import/item.c diff --git a/include/gwi.h b/include/gwi.h index d638a806..af16b219 100644 --- a/include/gwi.h +++ b/include/gwi.h @@ -58,4 +58,7 @@ struct Gwi_ { Templater templater; loc_t loc; }; + +#include "import/internals.h" + #endif diff --git a/include/import.h b/include/import.h index 8a30e93c..863b0e10 100644 --- a/include/import.h +++ b/include/import.h @@ -1,5 +1,6 @@ #ifndef __IMPORT #define __IMPORT + #define DLARG_MAX 6 typedef void (*f_xtor)(const M_Object, const m_bit*, const VM_Shred); diff --git a/include/import/internals.h b/include/import/internals.h new file mode 100644 index 00000000..2b603377 --- /dev/null +++ b/include/import/internals.h @@ -0,0 +1,13 @@ +#ifndef __IMPORT_INTERNALS +#define __IMPORT_INTERNALS + +#define GWI_ERR_B(a,...) { env_err(gwi->gwion->env, gwi->loc, (a), ## __VA_ARGS__); return GW_ERROR; } +#define GWI_ERR_O(a,...) { env_err(gwi->gwion->env, gwi->loc, (a), ## __VA_ARGS__); return NULL; } +#define ENV_ERR_B(pos, a,...) { env_err(env, pos, (a), ## __VA_ARGS__); return GW_ERROR; } +#define ENV_ERR_O(pos, a,...) { env_err(env, pos, (a), ## __VA_ARGS__); return NULL; } + +ANN void gwi_body(const Gwi, const Class_Body); + +ANN m_bool name_valid(const Gwi, const m_str); + +#endif diff --git a/src/import/internals.c b/src/import/internals.c new file mode 100644 index 00000000..a8381f1b --- /dev/null +++ b/src/import/internals.c @@ -0,0 +1,54 @@ +#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 "gwion.h" +#include "operator.h" +#include "import.h" +#include "gwi.h" + +void gwi_body(const Gwi gwi, const Class_Body body) { + const Class_Def cdef = gwi->gwion->env->class_def->e->def; + if(!cdef->body) + cdef->body = body; + else + gwi->body->next = body; + gwi->body = body; +} + +ANN m_bool _name_valid(const Gwi gwi, const m_str a) { + const m_uint len = strlen(a); + m_uint lvl = 0; + for(m_uint i = 0; i < len; i++) { + char c = a[i]; + if((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') + || (c == '_') || (c >= '0' && c <= '9')) + continue; + if(c == '<') { + if(a[++i] != '~') + GWI_ERR_B(_("illegal templating in name '%s'."), a) + ++lvl; + ++i; + continue; + } else if(c == ',') { + if(!lvl) + GWI_ERR_B(_("illegal use of ',' outside of templating in name '%s'."), a) + } else if(c == '~') { + if(!lvl || a[++i] != '>') + GWI_ERR_B(_("illegal templating in name '%s'."), a) + --lvl; + } else + GWI_ERR_B(_("illegal character '%c' in name '%s'."), c, a) + } + return !lvl ? 1 : -1; +} + +ANN m_bool name_valid(const Gwi gwi, const m_str a) { + return _name_valid(gwi, a[0] != '@' ? a : a + 1); +} diff --git a/src/import/item.c b/src/import/item.c new file mode 100644 index 00000000..2b33688e --- /dev/null +++ b/src/import/item.c @@ -0,0 +1,80 @@ +#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 "gwion.h" +#include "operator.h" +#include "import.h" +#include "gwi.h" +#include "cpy_ast.h" + +#define GWI_ERR_B(a,...) { env_err(gwi->gwion->env, gwi->loc, (a), ## __VA_ARGS__); return GW_ERROR; } +#define GWI_ERR_O(a,...) { env_err(gwi->gwion->env, gwi->loc, (a), ## __VA_ARGS__); return NULL; } +#define ENV_ERR_B(pos, a,...) { env_err(env, pos, (a), ## __VA_ARGS__); return GW_ERROR; } +#define ENV_ERR_O(pos, a,...) { env_err(env, pos, (a), ## __VA_ARGS__); return NULL; } + +ANN static void dl_var_new_exp_array(MemPool p, DL_Var* v) { + v->td->array = new_array_sub(p, NULL); + v->td->array->depth = v->array_depth; +} + +ANN static void dl_var_set(MemPool p, DL_Var* v, const ae_flag flag) { + v->list.self = &v->var; + v->td->flag = flag; + v->exp.exp_type = ae_exp_decl; + v->exp.d.exp_decl.td = v->td; + v->exp.d.exp_decl.list = &v->list; + if(v->array_depth) + dl_var_new_exp_array(p, v); +} + +ANN static void dl_var_release(MemPool p, const DL_Var* v) { + free_type_decl(p, v->td); +} + +ANN m_int gwi_item_ini(const Gwi gwi, const restrict m_str type, const restrict m_str name) { + DL_Var* v = &gwi->var; + memset(v, 0, sizeof(DL_Var)); + CHECK_BB(name_valid(gwi, name)) + v->var.xid = insert_symbol(gwi->gwion->st, name); + if((v->td = str2decl(gwi->gwion->env, type, &v->array_depth, gwi->loc))) + return GW_OK; + GWI_ERR_B(_(" ... during var import '%s.%s'."), gwi->gwion->env->name, name) +} + +ANN2(1) m_int gwi_item_tmpl(const Gwi gwi) { + const MemPool mp = gwi->gwion->mp; + DL_Var* v = &gwi->var; + Type_Decl *td = cpy_type_decl(mp, v->td); + const Var_Decl var = new_var_decl(mp, v->var.xid, v->var.array, + loc_cpy(mp, gwi->loc)); + const Var_Decl_List vlist = new_var_decl_list(mp, var, NULL); + const Exp exp = new_exp_decl(mp, td, vlist); + const Stmt stmt = new_stmt_exp(mp, ae_stmt_exp, exp); + const Stmt_List slist = new_stmt_list(mp, stmt, NULL); + Section* section = new_section_stmt_list(mp, slist); + const Class_Body body = new_class_body(mp, section, NULL); + gwi_body(gwi, body); + return GW_OK; +} + +#undef gwi_item_end +ANN2(1) m_int gwi_item_end(const Gwi gwi, const ae_flag flag, const m_uint* addr) { + const Env env = gwi->gwion->env; + DL_Var* v = &gwi->var; + dl_var_set(gwi->gwion->mp, v, flag | ae_flag_builtin); + v->var.addr = (void*)addr; + if(env->class_def && GET_FLAG(env->class_def, template)) + return gwi_item_tmpl(gwi); + v->exp.pos = gwi->loc; + CHECK_BB(traverse_decl(env, &v->exp.d.exp_decl)) + SET_FLAG(v->var.value, builtin); + dl_var_release(gwi->gwion->mp, v); + return (m_int)v->var.value->from->offset; +} diff --git a/src/lib/import.c b/src/lib/import.c index 8b32f517..89a89fc4 100644 --- a/src/lib/import.c +++ b/src/lib/import.c @@ -85,37 +85,6 @@ ANN m_int gwi_func_arg(const Gwi gwi, const restrict m_str t, const restrict m_s return GW_OK; } -ANN static m_bool _name_valid(const Gwi gwi, const m_str a) { - const m_uint len = strlen(a); - m_uint lvl = 0; - for(m_uint i = 0; i < len; i++) { - char c = a[i]; - if((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') - || (c == '_') || (c >= '0' && c <= '9')) - continue; - if(c == '<') { - if(a[++i] != '~') - GWI_ERR_B(_("illegal templating in name '%s'."), a) - ++lvl; - ++i; - continue; - } else if(c == ',') { - if(!lvl) - GWI_ERR_B(_("illegal use of ',' outside of templating in name '%s'."), a) - } else if(c == '~') { - if(!lvl || a[++i] != '>') - GWI_ERR_B(_("illegal templating in name '%s'."), a) - --lvl; - } else - GWI_ERR_B(_("illegal character '%c' in name '%s'."), c, a) - } - return !lvl ? 1 : -1; -} - -ANN static inline m_bool name_valid(const Gwi gwi, const m_str a) { - return _name_valid(gwi, a[0] != '@' ? a : a + 1); -} - ANN static m_bool check_illegal(const char c, const m_uint i) { return isalnum(c) || c == '_' || (!i && c == '@'); } @@ -343,76 +312,6 @@ ANN m_int gwi_class_end(const Gwi gwi) { return GW_OK; } -ANN static void dl_var_new_exp_array(MemPool p, DL_Var* v) { - v->td->array = new_array_sub(p, NULL); - v->td->array->depth = v->array_depth; - v->var.array = new_array_sub(p, NULL); - v->var.array->depth = v->array_depth; -} - -ANN static void dl_var_set(MemPool p, DL_Var* v, const ae_flag flag) { - v->list.self = &v->var; - v->td->flag = flag; - v->exp.exp_type = ae_exp_decl; - v->exp.d.exp_decl.td = v->td; - v->exp.d.exp_decl.list = &v->list; - if(v->array_depth) - dl_var_new_exp_array(p, v); -} - -ANN static void dl_var_release(MemPool p, const DL_Var* v) { - if(v->array_depth) - free_array_sub(p, v->var.array); - free_type_decl(p, v->td); -} - -ANN m_int gwi_item_ini(const Gwi gwi, const restrict m_str type, const restrict m_str name) { - DL_Var* v = &gwi->var; - memset(v, 0, sizeof(DL_Var)); - if(!(v->td = str2decl(gwi->gwion->env, type, &v->array_depth, gwi->loc))) - GWI_ERR_B(_(" ... during var import '%s.%s'."), gwi->gwion->env->name, name) - CHECK_BB(name_valid(gwi, name)) - v->var.xid = insert_symbol(gwi->gwion->st, name); - return GW_OK; -} - -#undef gwi_item_end - -static void gwi_body(const Gwi gwi, const Class_Body body) { - if(!gwi->gwion->env->class_def->e->def->body) - gwi->gwion->env->class_def->e->def->body = body; - else { - assert(gwi->body); - gwi->body->next = body; - } - gwi->body = body; -} - -ANN2(1) m_int gwi_item_end(const Gwi gwi, const ae_flag flag, const m_uint* addr) { - DL_Var* v = &gwi->var; - dl_var_set(gwi->gwion->mp, v, flag | ae_flag_builtin); - v->var.addr = (void*)addr; - if(gwi->gwion->env->class_def && GET_FLAG(gwi->gwion->env->class_def, template)) { - Type_Decl *type_decl = new_type_decl(gwi->gwion->mp, v->td->xid); - type_decl->flag = flag; - const Var_Decl var_decl = new_var_decl(gwi->gwion->mp, v->var.xid, v->var.array, loc_cpy(gwi->gwion->mp, gwi->loc)); - const Var_Decl_List var_decl_list = new_var_decl_list(gwi->gwion->mp, var_decl, NULL); - const Exp exp = new_exp_decl(gwi->gwion->mp, type_decl, var_decl_list); - const Stmt stmt = new_stmt_exp(gwi->gwion->mp, ae_stmt_exp, exp); - const Stmt_List list = new_stmt_list(gwi->gwion->mp, stmt, NULL); - Section* section = new_section_stmt_list(gwi->gwion->mp, list); - const Class_Body body = new_class_body(gwi->gwion->mp, section, NULL); - type_decl->array = v->td->array; - gwi_body(gwi, body); - return GW_OK; - } - v->exp.pos = gwi->loc; - CHECK_BB(traverse_decl(gwi->gwion->env, &v->exp.d.exp_decl)) - SET_FLAG(v->var.value, builtin); - dl_var_release(gwi->gwion->mp, v); - return (m_int)v->var.value->from->offset; -} - static Array_Sub make_dll_arg_list_array(MemPool p, Array_Sub array_sub, m_uint* array_depth, const m_uint array_depth2) { if(array_depth2) diff --git a/tests/import/coverage.c b/tests/import/coverage.c index a2e78356..aa842fba 100644 --- a/tests/import/coverage.c +++ b/tests/import/coverage.c @@ -80,6 +80,9 @@ GWION_IMPORT(coverage) { GWI_BB(gwi_item_ini(gwi,"Vec4", "sc_w")) GWI_BB(gwi_item_end(gwi, ae_flag_static | ae_flag_const, (void*)cw)) + GWI_BB(gwi_item_ini(gwi,"int[][]", "test_array")) + GWI_BB(gwi_item_end(gwi, ae_flag_none, NULL)) + GWI_BB(gwi_class_end(gwi)) return GW_OK; } diff --git a/tests/import/coverage.gw b/tests/import/coverage.gw index c0773f94..1cc4d722 100644 --- a/tests/import/coverage.gw +++ b/tests/import/coverage.gw @@ -24,3 +24,5 @@ c.s_i; <<< #(1.2, 0.1) => Coverage.s_c >>>; <<< @(1.2, 0.1, 2.6) => Coverage.s_v >>>; <<< @(1.2, 0.1, 2.6, 4.6) => Coverage.s_w >>>; + +<<< c.test_array[0][0] >>>; -- 2.43.0