From 6213eeb675014fd668b780897dd35b320725fa17 Mon Sep 17 00:00:00 2001 From: fennecdjay Date: Fri, 11 Oct 2019 01:12:06 +0200 Subject: [PATCH] :art: Improve enum --- ast | 2 +- src/emit/emit.c | 2 +- src/lib/tuple.c | 4 ++-- src/oo/type.c | 12 +++++++++++- src/parse/cpy_ast.c | 2 ++ src/parse/scan0.c | 19 ++++++++++++------- tests/tree/cpy_ast.gw | 5 ++--- 7 files changed, 31 insertions(+), 15 deletions(-) diff --git a/ast b/ast index d5d3cdd5..601562fc 160000 --- a/ast +++ b/ast @@ -1 +1 @@ -Subproject commit d5d3cdd5dc857479181e9d977c32718ac743a82d +Subproject commit 601562fc8e90d0cc4a66e1db03e78af3afbab46a diff --git a/src/emit/emit.c b/src/emit/emit.c index 1fbe3375..f15dc145 100644 --- a/src/emit/emit.c +++ b/src/emit/emit.c @@ -1398,7 +1398,7 @@ ANN static m_bool emit_union_def(const Emitter emit, const Union_Def udef) { if(udef->xid) { union_allocdata(emit->gwion->mp, udef); Type_Decl *type_decl = new_type_decl(emit->gwion->mp, - new_id_list(emit->gwion->mp, udef->xid, udef->pos)); + new_id_list(emit->gwion->mp, udef->xid, loc_cpy(emit->gwion->mp, udef->pos))); type_decl->flag = udef->flag; const Var_Decl var_decl = new_var_decl(emit->gwion->mp, udef->xid, NULL, loc_cpy(emit->gwion->mp, udef->pos)); const Var_Decl_List var_decl_list = new_var_decl_list(emit->gwion->mp, var_decl, NULL); diff --git a/src/lib/tuple.c b/src/lib/tuple.c index 7cab9c00..c613091a 100644 --- a/src/lib/tuple.c +++ b/src/lib/tuple.c @@ -299,7 +299,7 @@ ANN Type tuple_type(const Env env, const Vector v, const loc_t pos) { base = slist; {// build Type_List const ID_List ilist = new_id_list(env->gwion->mp, tsym, - loc_cpy(env->gwion->mp, pos)); + loc_cpy(env->gwion->mp, pos))); Type_Decl *td = new_type_decl(env->gwion->mp, ilist); Type_List tmp_tl = new_type_list(env->gwion->mp, td, NULL); if(tl) @@ -313,7 +313,7 @@ ANN Type tuple_type(const Env env, const Vector v, const loc_t pos) { Section * section = new_section_stmt_list(env->gwion->mp, base); Class_Body body = new_class_body(env->gwion->mp, section, NULL); const ID_List ilist = new_id_list(env->gwion->mp, insert_symbol(env->gwion->type[et_tuple]->name), - loc_cpy(env->gwion->mp, pos)); + loc_cpy(env->gwion->mp, loc_cpy(env->gwion->mp, pos))); Type_Decl *td = new_type_decl(env->gwion->mp, ilist); Class_Def cdef = new_class_def(env->gwion->mp, ae_flag_template, sym, td, body, loc_cpy(env->gwion->mp, pos)); diff --git a/src/oo/type.c b/src/oo/type.c index 4c00d740..23453a52 100644 --- a/src/oo/type.c +++ b/src/oo/type.c @@ -10,6 +10,7 @@ #include "parse.h" #include "gwion.h" #include "tuple.h" +#include "context.h" ANN static void free_type(Type a, Gwion gwion) { if(GET_FLAG(a, template) || GET_FLAG(a, global)) { @@ -22,8 +23,17 @@ ANN static void free_type(Type a, Gwion gwion) { free_decl_list(gwion->mp, a->e->def->list); } a->e->def->union_def = NULL; - } else if(a->e->def) + } else if(a->e->def) { +// looks like errored template get be freed for some reason +// fix that + UNSET_FLAG(a->e->def, template); + if(a->e->ctx && a->e->ctx->error)//exit(12); + SET_FLAG(a->e->def, ref); + else + UNSET_FLAG(a->e->def, ref); + UNSET_FLAG(a->e->def, global); free_class_def(gwion->mp, a->e->def); + } } if(a->nspc) REM_REF(a->nspc, gwion); diff --git a/src/parse/cpy_ast.c b/src/parse/cpy_ast.c index 13a28484..ab75269e 100644 --- a/src/parse/cpy_ast.c +++ b/src/parse/cpy_ast.c @@ -338,6 +338,7 @@ ANN static Enum_Def cpy_enum_def(MemPool p, const Enum_Def src) { Enum_Def a = mp_calloc(p, Enum_Def); a->list = cpy_id_list(p, src->list); a->xid = src->xid; + a->pos = loc_cpy(p, src->pos); a->flag = src->flag; vector_init(&a->values); return a; @@ -385,6 +386,7 @@ ANN static Union_Def cpy_union_def(MemPool p, const Union_Def src) { a->type_xid = src->type_xid; // 1 if(src->tmpl) a->tmpl = cpy_tmpl(p, src->tmpl); // 1 + a->pos = loc_cpy(p, src->pos); a->flag = src->flag; // 1 return a; } diff --git a/src/parse/scan0.c b/src/parse/scan0.c index 85cdb991..fdeba373 100644 --- a/src/parse/scan0.c +++ b/src/parse/scan0.c @@ -200,19 +200,24 @@ ANN m_bool scan0_enum_def(const Env env, const Enum_Def edef) { s_name(edef->xid), v->type->name) CHECK_BB(scan0_defined(env, edef->xid, edef->pos)) } +// TODO: make a func const Type t = type_copy(env->gwion->mp, env->gwion->type[et_int]); t->xid = ++env->scope->type_xid; - t->name = edef->xid ? s_name(edef->xid) : "int"; + const size_t line_len = num_digit(edef->pos->first_line); + const size_t col_len = num_digit(edef->pos->first_column); +// TODO: make a func also for name, used in union too + char name[strlen(env->curr->name) + line_len + col_len + 10]; // add pos + sprintf(name, "@enum:%s:%u:%u", env->curr->name, edef->pos->first_line, edef->pos->first_column); + const Symbol sym = insert_symbol(name); + t->name = edef->xid ? s_name(edef->xid) : s_name(sym); t->e->parent = env->gwion->type[et_int]; const Nspc nspc = GET_FLAG(edef, global) ? env->global_nspc : env->curr; t->e->owner = nspc; if(GET_FLAG(edef, global)) context_global(env); edef->t = t; - if(edef->xid) { - add_type(env, nspc, t); - mk_class(env, t); - } + add_type(env, nspc, t); + mk_class(env, t); scan0_implicit_similar(env, t, env->gwion->type[et_int]); return GW_OK; } @@ -268,8 +273,8 @@ ANN m_bool scan0_union_def(const Env env, const Union_Def udef) { env->curr : env->global_nspc; const size_t line_len = num_digit(udef->pos->first_line); const size_t col_len = num_digit(udef->pos->first_column); - char name[strlen(env->name) + line_len + col_len + 10]; // add pos - sprintf(name, "@union:%s:%u:%u", env->name, udef->pos->first_line, udef->pos->first_column); + char name[strlen(env->curr->name) + line_len + col_len + 10]; // add pos + sprintf(name, "@union:%s:%u:%u", env->curr->name, udef->pos->first_line, udef->pos->first_column); const Symbol sym = insert_symbol(name); const Type t = union_type(env, nspc, sym, 1); udef->value = new_value(env->gwion->mp, t, s_name(sym)); diff --git a/tests/tree/cpy_ast.gw b/tests/tree/cpy_ast.gw index eba11ffc..6ab60241 100644 --- a/tests/tree/cpy_ast.gw +++ b/tests/tree/cpy_ast.gw @@ -1,4 +1,4 @@ -class C { +class <~A~>C { \a{}; [ 1 ]; int i,j; @@ -16,9 +16,8 @@ class C { int array[2]; for(auto a : array); repeat(1); -#! gotos ... match i { case _:; } enum { zero }; typedef int Int; } -C c; +<~int~>C c; -- 2.43.0