From 7f1cb3207b0e16c1b20311c3564769c2d2499165 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Astor?= Date: Sat, 9 May 2020 13:00:08 +0200 Subject: [PATCH] :art: Improve type naming --- src/env/type.c | 49 ++++++++++++++++++++++---------------------- src/lib/object_op.c | 9 ++++---- src/main.c | 7 +++---- src/parse/operator.c | 9 ++------ 4 files changed, 34 insertions(+), 40 deletions(-) diff --git a/src/env/type.c b/src/env/type.c index 829cff69..391bad44 100644 --- a/src/env/type.c +++ b/src/env/type.c @@ -136,35 +136,34 @@ ANN m_bool type_ref(Type t) { } ANN m_str get_type_name(const Env env, const Type t, const m_uint index) { - const m_str s = t->name; - m_str name = strstr(s, "<"); - m_uint i = 0; + if(!index || t->name[0] != '<') + return NULL; + m_str name = t->name + 2; m_uint lvl = 0; m_uint n = 1; - const size_t len = name ? strlen(name) : 0; - const size_t slen = strlen(s); - const size_t tlen = slen - len + 1; - char c[slen + 1]; - if(!name) - return index ? NULL : s_name(insert_symbol(s)); - if(index == 0) { - snprintf(c, tlen, "%s", s); - return s_name(insert_symbol(c)); - } - ++name; - while(*name++) { - if(*name == '<') { - if(n == index) - c[i++] = *name; - lvl++; - name++; - } else if(*name == '~' && !lvl--) - break; + const size_t slen = strlen(name); + char c, buf[slen + 1], *tmp = buf; + while((c = *name)) { + if(c == '<') + ++lvl; + else if(c == '>') { + if(!lvl-- && n == index) { + --tmp; + break; + } + } else if(c == ',') { + if(!lvl && n++ == index) + break; + if(!lvl) + ++name; + } if(n == index) - c[i++] = *name; + *tmp++ = *name; + ++name; + } - c[i] = '\0'; - return strlen(c) ? s_name(insert_symbol(c)) : NULL; + *tmp = '\0'; + return strlen(buf) ? s_name(insert_symbol(buf)) : NULL; } ANN m_uint get_depth(const Type type) { diff --git a/src/lib/object_op.c b/src/lib/object_op.c index 3b6f05c7..5736314c 100644 --- a/src/lib/object_op.c +++ b/src/lib/object_op.c @@ -311,11 +311,12 @@ ANN static inline size_t tmpl_set(struct tmpl_info* info, const Type t) { ANN static ssize_t template_size(const Env env, struct tmpl_info* info) { ID_List base = info->cdef->base.tmpl->list; Type_List call = info->call; - size_t size = tmpl_set(info, info->cdef->base.type); + size_t size = 0; do { DECL_OB(const Type, t, = known_type(env, call->td)) size += tmpl_set(info, t); } while((call = call->next) && (base = base->next) && ++size); + size += tmpl_set(info, info->cdef->base.type); return size + 16 + 3; } @@ -327,11 +328,10 @@ ANN static inline m_str tmpl_get(struct tmpl_info* info, m_str str) { ANN static void template_name(struct tmpl_info* info, m_str s) { m_str str = s; - str = tmpl_get(info, str); *str++ = '<'; *str++ = '~'; - const m_uint size = vector_size(&info->type); - for(info->index = 1; info->index < size; ++info->index) { + const m_uint size = vector_size(&info->type) -1; + for(info->index = 0; info->index < size; ++info->index) { str = tmpl_get(info, str); if(info->index < size - 1) *str++ = ','; @@ -340,6 +340,7 @@ ANN static void template_name(struct tmpl_info* info, m_str s) { *str++ = '>'; } } + str = tmpl_get(info, str); *str = '\0'; } diff --git a/src/main.c b/src/main.c index ede3938d..3229e3b5 100644 --- a/src/main.c +++ b/src/main.c @@ -17,7 +17,7 @@ static void sig(int unused NUSED) { #ifdef __AFL_HAVE_MANUAL_CONTROL -#define BUFSIZE 128 +#define BUFSIZE 4 static void afl_run(const Gwion gwion) { __AFL_INIT(); @@ -25,9 +25,8 @@ static void afl_run(const Gwion gwion) { struct GwText_ text = { .mp=gwion->mp }; while (__AFL_LOOP(256)) { ssize_t sz; - memset(buf, 0, BUFSIZE); - while((sz = read(0, buf, BUFSIZE)) > 0) { - buf[sz] = '\0'; + while((sz = read(0, buf, BUFSIZE-1)) > 0) { +buf[sz] = '\0'; text_add(&text, buf); } if(compile_string(gwion, "afl", text.str)) { diff --git a/src/parse/operator.c b/src/parse/operator.c index e49b0283..15c2bdd7 100644 --- a/src/parse/operator.c +++ b/src/parse/operator.c @@ -37,13 +37,8 @@ ANN void free_op_map(Map map, struct Gwion_ *gwion) { ANN static Type op_parent(const Env env, const Type t) { if(GET_FLAG(t, template) && GET_FLAG(t, ref)) { const Type type = typedef_base(t); - const m_str post = strstr(type->name, "<"); - size_t len = strlen(type->name) - strlen(post); - char c[len + 1]; - for(size_t i = 0; i < len; i++) - c[i] = type->name[i]; - c[len] = 0; - return nspc_lookup_type1(env->curr, insert_symbol(env->gwion->st, c)); + const m_str post = strrchr(type->name, '>') + 1; + return nspc_lookup_type1(env->curr, insert_symbol(env->gwion->st, post)); } return t->e->parent; } -- 2.43.0