From 0bed6f7d31242de0d1ac81515fc15a5703a9af97 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Astor?= Date: Thu, 12 Mar 2020 19:57:52 +0100 Subject: [PATCH] :art: Simplify strings --- examples/string.gw | 31 +----------- src/lib/string.c | 123 +-------------------------------------------- 2 files changed, 3 insertions(+), 151 deletions(-) diff --git a/examples/string.gw b/examples/string.gw index 6473e1bb..79c9f6d1 100644 --- a/examples/string.gw +++ b/examples/string.gw @@ -1,31 +1,4 @@ -Object o; string s; "CamelCase" => s; -#!null @=> s; -<<< 1 => s >>>; -<<< 1.0 => s >>>; -<<< "test" => s >>>; #! new -<<< "test" => s >>>; #! new -<<< s >>>; -<<< null => s >>>; #!test me -<<< o => s >>>; -<<< o >>>; -#! look at me!!! -<<< o => s >>>; #! hang . grrr ... -<<< "test" + "another test" >>>; #! mem leak - -#!me.exit(); - -{ (1 + s) @=> string ref str; } -{ (2.0 +s) @=> string ref str; } - -<<< 11 +=> s >>>; -<<< 11.0 +=> s >>>; -<<< o +=> s >>>; -<<< "test" + s >>>; #! also leak -<<< null +=> s >>>; #! also hang -<<< "test" +=> s >>>; #! test me -<<< "test" == "test" >>>; -<<< "test" != "test" >>>; -<<< "test" == "test1" >>>; -<<< "test" != "test1" >>>; +<<< "test" => s >>>; +<<< null @=> s >>>; #!test me diff --git a/src/lib/string.c b/src/lib/string.c index 944d26b4..7f2f8070 100644 --- a/src/lib/string.c +++ b/src/lib/string.c @@ -21,11 +21,6 @@ ANN static void push_string(const VM_Shred shred, const M_Object obj, const m_st _release(obj, shred); } -ANN static void push_new_string(const VM_Shred shred, const m_str c) { - const M_Object obj = new_string(shred->info->mp, shred, c); - *(M_Object*)REG(-SZ_INT) = (M_Object)obj; -} - #define describe_string_logical(name, action) \ static INSTR(String_##name) {\ POP_REG(shred, SZ_INT); \ @@ -39,95 +34,14 @@ describe_string_logical(eq, (lhs && rhs && !strcmp(STRING(lhs), STRING(rhs))) || describe_string_logical(neq, (lhs && rhs && strcmp(STRING(lhs), STRING(rhs))) || (lhs && !rhs) || (!lhs && rhs)) -#define describe_string_assign(name, type, offset, opt, len, format, ...) \ -static INSTR(name##String_Assign) {\ - POP_REG(shred, offset); \ - const type lhs = *(type*)REG(-SZ_INT); \ - const M_Object rhs = *(M_Object*)REG(offset - SZ_INT); \ - opt; \ - if(!rhs) \ - Except(shred, "NullStringException."); \ - char str[(len)]; \ - sprintf(str, format, __VA_ARGS__); \ - push_string(shred, rhs, str); \ -} - static INSTR(String_Assign) { POP_REG(shred, SZ_INT); const M_Object lhs = *(M_Object*)REG(-SZ_INT); const M_Object rhs = *(M_Object*)REG(0); release(lhs, shred); - if(!rhs) - Except(shred, "NullStringException."); push_string(shred, rhs, lhs ? STRING(lhs) : ""); } -describe_string_assign(Int_, m_int, SZ_INT,, - num_digit((m_uint)labs(lhs)) + 1, - "%" INT_F "", lhs) -describe_string_assign(Float_, m_float, SZ_FLOAT,, - num_digit((m_uint)lhs) + 6, - "%.4f", lhs) -describe_string_assign(Object_, M_Object, SZ_INT, release(lhs, shred), - 16, - "%p", (void*)lhs) - -static INSTR(String_String) { - POP_REG(shred, SZ_INT); - const M_Object lhs = *(M_Object*)REG(-SZ_INT); - const M_Object rhs = *(M_Object*)REG(0); - char str[(lhs ? strlen(STRING(lhs)) : 0) + (rhs ? strlen(STRING(rhs)) : 0) + 1]; - sprintf(str, "%s%s", lhs ? STRING(lhs) : "", rhs ? STRING(rhs) : ""); - push_new_string(shred, str); - release(rhs, shred); - release(lhs, shred); -} - -#define describe_string(name, type, offset, len, opt, format, ...) \ -static INSTR(name##_String) {\ - POP_REG(shred, offset); \ - const type lhs = *(type*)REG(-SZ_INT);\ - const M_Object rhs = *(M_Object*)REG(offset-SZ_INT);\ - if(!rhs) \ - Except(shred, "NullPtrException"); \ - char str[(len)];\ - sprintf(str, format, __VA_ARGS__);\ - push_new_string(shred, str);\ - opt; \ - release(rhs, shred);\ -} -describe_string(Int, m_int, SZ_INT, - num_digit((m_uint)lhs) + (rhs ? strlen(STRING(rhs)) : 0) + 1,, - "%" INT_F "%s", lhs, rhs ? STRING(rhs) : "") -describe_string(Float, m_float, SZ_FLOAT, - (num_digit((m_uint)lhs) + 5 + (rhs ? strlen(STRING(rhs)) : 0) + 1),, - "%.4f%s", lhs, rhs ? STRING(rhs) : "") -describe_string(Object, M_Object, SZ_INT, - 17 + (rhs ? strlen(STRING(rhs)) : 0), /*release(lhs, shred)*/, - "%p%s", (void*)lhs, rhs ? STRING(rhs) : "") - - -#define describe_string_plus(_name, offset, type, opt, len, format, ...) \ -static INSTR(_name##String_Plus) { \ - POP_REG(shred, offset); \ - const type lhs = *(type*)REG(-SZ_INT); \ - const M_Object rhs = *(M_Object*)REG(offset - SZ_INT); \ - opt; \ - if(!rhs) \ - Except(shred, "NullPtrException"); \ - char c[strlen(STRING(rhs)) + (len) + 1]; \ - sprintf(c, "%s"format, STRING(rhs), __VA_ARGS__); \ - push_string(shred, rhs, c); \ -} -describe_string_plus(,SZ_INT, M_Object, release(lhs, shred), - lhs ? strlen(STRING(lhs)) : 0, "%s", lhs ? STRING(lhs) : "") -describe_string_plus(Int_, SZ_INT, m_int,, - num_digit((m_uint)lhs), "%"INT_F, lhs) -describe_string_plus(Float_, SZ_FLOAT, m_float,, - num_digit((m_uint)lhs) + 6, "%.4f", lhs) -describe_string_plus(Object_, SZ_INT, M_Object, release(lhs, shred), - 16, "%p", (void*)lhs) - static CTOR(string_ctor) { STRING(o) = ""; } @@ -180,52 +94,17 @@ GWION_IMPORT(string) { GWI_BB(gwi_item_end(gwi, ae_flag_const, NULL)) GWI_BB(gwi_class_end(gwi)) - GWI_BB(gwi_oper_ini(gwi, "string", "string", "string")) + GWI_BB(gwi_oper_ini(gwi, "string", "nonnull string", "string")) GWI_BB(gwi_oper_add(gwi, opck_const_rhs)) GWI_BB(gwi_oper_end(gwi, "=>", String_Assign)) - GWI_BB(gwi_oper_end(gwi, "+", String_String)) - GWI_BB(gwi_oper_add(gwi, opck_const_rhs)) - GWI_BB(gwi_oper_end(gwi, "+=>", String_Plus)) GWI_BB(gwi_oper_ini(gwi, "string", "string", "int")) GWI_BB(gwi_oper_end(gwi, "==", String_eq)) GWI_BB(gwi_oper_end(gwi, "!=", String_neq)) - GWI_BB(gwi_oper_ini(gwi, "int", "string", "string")) - GWI_BB(gwi_oper_add(gwi, opck_const_rhs)) - GWI_BB(gwi_oper_end(gwi, "=>", Int_String_Assign)) - GWI_BB(gwi_oper_end(gwi, "+", Int_String)) - GWI_BB(gwi_oper_add(gwi, opck_const_rhs)) - GWI_BB(gwi_oper_end(gwi, "+=>", Int_String_Plus)) - - GWI_BB(gwi_oper_ini(gwi, "float", "string", "string")) - GWI_BB(gwi_oper_add(gwi, opck_const_rhs)) - GWI_BB(gwi_oper_end(gwi, "=>", Float_String_Assign)) - GWI_BB(gwi_oper_end(gwi, "+", Float_String)) - GWI_BB(gwi_oper_add(gwi, opck_const_rhs)) - GWI_BB(gwi_oper_end(gwi, "+=>", Float_String_Plus)) - - GWI_BB(gwi_oper_ini(gwi, "Object", "string", "string")) - GWI_BB(gwi_oper_add(gwi, opck_const_rhs)) - GWI_BB(gwi_oper_end(gwi, "=>", Object_String_Assign)) - GWI_BB(gwi_oper_end(gwi, "+", Object_String)) - GWI_BB(gwi_oper_add(gwi, opck_const_rhs)) - GWI_BB(gwi_oper_end(gwi, "+=>", Object_String_Plus)) - - GWI_BB(gwi_oper_ini(gwi, "@null", "string", "string")) - GWI_BB(gwi_oper_add(gwi, opck_const_rhs)) - GWI_BB(gwi_oper_end(gwi, "=>", Object_String_Assign)) - GWI_BB(gwi_oper_end(gwi, "+", Object_String)) - GWI_BB(gwi_oper_add(gwi, opck_const_rhs)) - GWI_BB(gwi_oper_end(gwi, "+=>", Object_String_Plus)) - GWI_BB(gwi_oper_ini(gwi, "string", "int", "string")) GWI_BB(gwi_oper_end(gwi, "@slice", StringSlice)) -// gwi_item_ini(gwi, "string", "__func__"); -// gwi_item_end(gwi, ae_flag_const, NULL); -// gwi_reserve(gwi, "__func__"); - struct SpecialId_ spid = { .ck=check_funcpp, .exec=RegPushMe, .is_const=1 }; gwi_specialid(gwi, "__func__", &spid); return GW_OK; -- 2.43.0