From 28949caa313f277abeb3eb1fcd903ef78f30c967 Mon Sep 17 00:00:00 2001 From: fennecdjay Date: Mon, 15 Jul 2019 01:39:34 +0200 Subject: [PATCH] :champagne: Improve string litterals --- src/emit/emit.c | 15 +++++++-------- src/parse/check.c | 22 +++++++++++++++++++--- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/emit/emit.c b/src/emit/emit.c index a63e0081..5831fbdb 100644 --- a/src/emit/emit.c +++ b/src/emit/emit.c @@ -457,12 +457,7 @@ ANN static m_bool prim_id(const Emitter emit, const Exp_Primary* prim) { emit_add_instr(emit, RegPushNow); else if(prim->d.var == insert_symbol("maybe")) emit_add_instr(emit, RegPushMaybe); - else if(prim->d.var == insert_symbol("__func__")) { - const Instr instr = emit_add_instr(emit, RegPushStr); - instr->m_val = (m_uint)s_name(insert_symbol(emit->env->func ? - emit->env->func->name : emit->env->class_def ? - emit->env->class_def->name : emit->env->name)); - } else + else emit_symbol(emit, prim); return GW_OK; } @@ -490,8 +485,12 @@ ANN static m_bool prim_str(const Emitter emit, const Exp_Primary* prim) { strcpy(c, prim->d.str); CHECK_BB(escape_str(emit, c, exp_self(prim)->pos)); } else c[0] = '\0'; - const Instr instr = emit_add_instr(emit, RegPushStr); - instr->m_val = (m_uint)s_name(insert_symbol(c)); + const Value v = prim->value; + const Symbol sym = insert_symbol(c); + if(!v->d.ptr) + v->d.ptr = (m_uint*)new_string2(emit->gwion->mp, NULL, s_name(sym)); + regpushi(emit, (m_uint)v->d.ptr); + emit_add_instr(emit, RegAddRef); return GW_OK; } diff --git a/src/parse/check.c b/src/parse/check.c index 30fc2136..91ccdd9d 100644 --- a/src/parse/check.c +++ b/src/parse/check.c @@ -202,11 +202,28 @@ ANN static Type check_exp_prim_this(const Env env, const Exp_Primary* primary) { return env->class_def; } -ANN static Type prim_id(const Env env, const Exp_Primary* primary) { +ANN static Type prim_str(const Env env, Exp_Primary *const prim) { + if(!prim->value) { + const m_str str = prim->d.str; + const Value v = new_value(env->gwion->mp, t_string, str); + char c[strlen(str) + 8]; + sprintf(c, "%s:string", str); + nspc_add_value(env_nspc(env), insert_symbol(c), v); + prim->value = v; + } + return t_string; +} + +ANN static Type prim_id(const Env env, Exp_Primary* primary) { const m_str str = s_name(primary->d.var); if(!strcmp(str, "this")) return check_exp_prim_this(env, primary); - else + else if(!strcmp(str, "__func__")) { + primary->primary_type = ae_primary_str; + primary->d.str = env->func ? env->func->name : env->class_def ? + env->class_def->name : env->name; + return prim_str(env, primary); + } else return prim_id_non_res(env, primary); } @@ -273,7 +290,6 @@ ANN static Type prim_##name(const Env env NUSED, const Exp_Primary * primary NUS } describe_prim_xxx(num, t_int) describe_prim_xxx(float, t_float) -describe_prim_xxx(str, t_string) describe_prim_xxx(nil, t_void) typedef Type (*_type_func)(const Env, const void*); -- 2.43.0