From 5e1d4c3f39d4ec97064141ff1cdf94eb7b578698 Mon Sep 17 00:00:00 2001 From: q66 Date: Tue, 30 Aug 2016 21:55:35 +0100 Subject: [PATCH] raw members of CsValue are now private --- cs_vm.cc | 49 ++++++++++++++----------- cubescript.cc | 100 ++++++++++++++++++++++++-------------------------- cubescript.hh | 33 ++++++++++------- 3 files changed, 95 insertions(+), 87 deletions(-) diff --git a/cs_vm.cc b/cs_vm.cc index 33a274b..1904f34 100644 --- a/cs_vm.cc +++ b/cs_vm.cc @@ -166,7 +166,7 @@ static inline void forcecond(CsState &cs, CsValue &v) { case VAL_STR: case VAL_MACRO: case VAL_CSTR: - if (v.s[0]) { + if (!v.get_strr().empty()) { forcecode(cs, v); } else { v.set_int(0); @@ -263,7 +263,7 @@ void CsValue::copy_arg(CsValue &r) const { case VAL_STR: case VAL_CSTR: case VAL_MACRO: - r.set_str(ostd::ConstCharRange(s, len)); + r.set_str(ostd::ConstCharRange(p_s, p_len)); break; case VAL_CODE: { ostd::Uint32 *bcode = reinterpret_cast(r.get_code()); @@ -517,7 +517,7 @@ static inline int cs_get_lookupu_type( ) { return -2; /* default case */ } - id = cs.get_ident(arg.s); + id = cs.get_ident(arg.get_strr()); if (id) { switch(id->get_type()) { case CsIdentType::alias: @@ -554,7 +554,7 @@ static inline int cs_get_lookupu_type( return ID_UNKNOWN; } } - cs_debug_code(cs, "unknown alias lookup: %s", arg.s); + cs_debug_code(cs, "unknown alias lookup: %s", arg.get_strr()); arg.cleanup(); return ID_UNKNOWN; } @@ -879,14 +879,14 @@ static ostd::Uint32 *runcode(CsState &cs, ostd::Uint32 *code, CsValue &result) { case VAL_INT: gs.code.reserve(8); gs.code.push(CODE_START); - gs.gen_int(arg.i); + gs.gen_int(arg.get_int()); gs.code.push(CODE_RESULT); gs.code.push(CODE_EXIT); break; case VAL_FLOAT: gs.code.reserve(8); gs.code.push(CODE_START); - gs.gen_float(arg.f); + gs.gen_float(arg.get_float()); gs.code.push(CODE_RESULT); gs.code.push(CODE_EXIT); break; @@ -894,7 +894,7 @@ static ostd::Uint32 *runcode(CsState &cs, ostd::Uint32 *code, CsValue &result) { case VAL_MACRO: case VAL_CSTR: gs.code.reserve(64); - gs.gen_main(arg.s); + gs.gen_main(arg.get_strr()); arg.cleanup(); break; default: @@ -915,11 +915,12 @@ static ostd::Uint32 *runcode(CsState &cs, ostd::Uint32 *code, CsValue &result) { switch (arg.get_type()) { case VAL_STR: case VAL_MACRO: - case VAL_CSTR: - if (arg.s[0]) { + case VAL_CSTR: { + ostd::ConstCharRange s = arg.get_strr(); + if (!s.empty()) { GenState gs(cs); gs.code.reserve(64); - gs.gen_main(arg.s); + gs.gen_main(s); arg.cleanup(); arg.set_code(reinterpret_cast( gs.code.disown() + 1 @@ -928,6 +929,7 @@ static ostd::Uint32 *runcode(CsState &cs, ostd::Uint32 *code, CsValue &result) { arg.force_null(); } break; + } } continue; } @@ -954,7 +956,7 @@ static ostd::Uint32 *runcode(CsState &cs, ostd::Uint32 *code, CsValue &result) { arg.get_type() == VAL_MACRO || arg.get_type() == VAL_CSTR ) { - id = cs.new_ident(ostd::ConstCharRange(arg.cstr, arg.len)); + id = cs.new_ident(arg.get_strr()); } if ( id->get_index() < MaxArguments && @@ -1235,7 +1237,8 @@ static ostd::Uint32 *runcode(CsState &cs, ostd::Uint32 *code, CsValue &result) { continue; case CODE_SVAR1: cs.set_var_str_checked( - static_cast(cs.identmap[op >> 8]), args[--numargs].s + static_cast(cs.identmap[op >> 8]), + args[--numargs].get_strr() ); args[numargs].cleanup(); continue; @@ -1258,22 +1261,25 @@ static ostd::Uint32 *runcode(CsState &cs, ostd::Uint32 *code, CsValue &result) { continue; case CODE_IVAR1: cs.set_var_int_checked( - static_cast(cs.identmap[op >> 8]), args[--numargs].i + static_cast(cs.identmap[op >> 8]), + args[--numargs].get_int() ); continue; case CODE_IVAR2: numargs -= 2; cs.set_var_int_checked( static_cast(cs.identmap[op >> 8]), - (args[numargs].i << 16) | (args[numargs + 1].i << 8)); + (args[numargs].get_int() << 16) + | (args[numargs + 1].get_int() << 8) + ); continue; case CODE_IVAR3: numargs -= 3; cs.set_var_int_checked( static_cast(cs.identmap[op >> 8]), - (args[numargs].i << 16) - | (args[numargs + 1].i << 8) - | args[numargs + 2].i); + (args[numargs].get_int() << 16) + | (args[numargs + 1].get_int() << 8) + | (args[numargs + 2].get_int())); continue; case CODE_FVAR | RET_FLOAT: @@ -1294,7 +1300,8 @@ static ostd::Uint32 *runcode(CsState &cs, ostd::Uint32 *code, CsValue &result) { continue; case CODE_FVAR1: cs.set_var_float_checked( - static_cast(cs.identmap[op >> 8]), args[--numargs].f + static_cast(cs.identmap[op >> 8]), + args[--numargs].get_float() ); continue; @@ -1456,13 +1463,13 @@ litval: } continue; } - CsIdent *id = cs.get_ident(idarg.s); + CsIdent *id = cs.get_ident(idarg.get_strr()); if (!id) { noid: - if (cs_check_num(idarg.s)) { + if (cs_check_num(idarg.get_strr())) { goto litval; } - cs_debug_code(cs, "unknown command: %s", idarg.s); + cs_debug_code(cs, "unknown command: %s", idarg.get_strr()); result.force_null(); free_args(args, numargs, offset - 1); force_arg(result, op & CODE_RET_MASK); diff --git a/cubescript.cc b/cubescript.cc index 620f63c..d786853 100644 --- a/cubescript.cc +++ b/cubescript.cc @@ -354,14 +354,10 @@ CsIdent *CsState::force_ident(CsValue &v) { case VAL_IDENT: return v.get_ident(); case VAL_MACRO: - case VAL_CSTR: { - CsIdent *id = new_ident(v.s); - v.set_ident(id); - return id; - } + case VAL_CSTR: case VAL_STR: { - CsIdent *id = new_ident(v.s); - delete[] v.s; + CsIdent *id = new_ident(v.get_strr()); + v.cleanup(); v.set_ident(id); return id; } @@ -485,7 +481,7 @@ void CsState::print_var(CsVar *v) { void CsValue::cleanup() { switch (get_type()) { case VAL_STR: - delete[] s; + delete[] p_s; break; case VAL_CODE: ostd::Uint32 *bcode = reinterpret_cast(p_code); @@ -502,12 +498,12 @@ int CsValue::get_type() const { void CsValue::set_int(CsInt val) { p_type = VAL_INT; - i = val; + p_i = val; } void CsValue::set_float(CsFloat val) { p_type = VAL_FLOAT; - f = val; + p_f = val; } void CsValue::set_str(CsString val) { @@ -525,7 +521,7 @@ void CsValue::set_str(CsString val) { void CsValue::set_null() { p_type = VAL_NULL; - i = 0; + p_code = nullptr; } void CsValue::set_code(CsBytecode *val) { @@ -535,14 +531,14 @@ void CsValue::set_code(CsBytecode *val) { void CsValue::set_cstr(ostd::ConstCharRange val) { p_type = VAL_CSTR; - len = val.size(); - cstr = val.data(); + p_len = val.size(); + p_cstr = val.data(); } void CsValue::set_mstr(ostd::CharRange val) { p_type = VAL_STR; - len = val.size(); - s = val.data(); + p_len = val.size(); + p_s = val.data(); } void CsValue::set_ident(CsIdent *val) { @@ -552,8 +548,8 @@ void CsValue::set_ident(CsIdent *val) { void CsValue::set_macro(ostd::ConstCharRange val) { p_type = VAL_MACRO; - len = val.size(); - cstr = val.data(); + p_len = val.size(); + p_cstr = val.data(); } void CsValue::set(CsValue &tv) { @@ -574,15 +570,15 @@ CsFloat CsValue::force_float() { CsFloat rf = 0.0f; switch (get_type()) { case VAL_INT: - rf = i; + rf = p_i; break; case VAL_STR: case VAL_MACRO: case VAL_CSTR: - rf = cs_parse_float(s); + rf = cs_parse_float(ostd::ConstCharRange(p_s, p_len)); break; case VAL_FLOAT: - return f; + return p_f; } cleanup(); set_float(rf); @@ -593,15 +589,15 @@ CsInt CsValue::force_int() { CsInt ri = 0; switch (get_type()) { case VAL_FLOAT: - ri = f; + ri = p_f; break; case VAL_STR: case VAL_MACRO: case VAL_CSTR: - ri = cs_parse_int(s); + ri = cs_parse_int(ostd::ConstCharRange(p_s, p_len)); break; case VAL_INT: - return i; + return p_i; } cleanup(); set_int(ri); @@ -612,33 +608,33 @@ ostd::ConstCharRange CsValue::force_str() { CsString rs; switch (get_type()) { case VAL_FLOAT: - rs = ostd::move(floatstr(f)); + rs = ostd::move(floatstr(p_f)); break; case VAL_INT: - rs = ostd::move(intstr(i)); + rs = ostd::move(intstr(p_i)); break; case VAL_MACRO: case VAL_CSTR: - rs = ostd::ConstCharRange(s, len); + rs = ostd::ConstCharRange(p_s, p_len); break; case VAL_STR: - return s; + return ostd::ConstCharRange(p_s, p_len); } cleanup(); set_str(ostd::move(rs)); - return s; + return ostd::ConstCharRange(p_s, p_len); } CsInt CsValue::get_int() const { switch (get_type()) { case VAL_FLOAT: - return CsInt(f); + return CsInt(p_f); case VAL_INT: - return i; + return p_i; case VAL_STR: case VAL_MACRO: case VAL_CSTR: - return cs_parse_int(s); + return cs_parse_int(ostd::ConstCharRange(p_s, p_len)); } return 0; } @@ -646,13 +642,13 @@ CsInt CsValue::get_int() const { CsFloat CsValue::get_float() const { switch (get_type()) { case VAL_FLOAT: - return f; + return p_f; case VAL_INT: - return CsFloat(i); + return CsFloat(p_i); case VAL_STR: case VAL_MACRO: case VAL_CSTR: - return cs_parse_float(s); + return cs_parse_float(ostd::ConstCharRange(p_s, p_len)); } return 0.0f; } @@ -676,11 +672,11 @@ CsString CsValue::get_str() const { case VAL_STR: case VAL_MACRO: case VAL_CSTR: - return ostd::ConstCharRange(s, len); + return ostd::ConstCharRange(p_s, p_len); case VAL_INT: - return intstr(i); + return intstr(p_i); case VAL_FLOAT: - return floatstr(f); + return floatstr(p_f); } return CsString(""); } @@ -690,7 +686,7 @@ ostd::ConstCharRange CsValue::get_strr() const { case VAL_STR: case VAL_MACRO: case VAL_CSTR: - return ostd::ConstCharRange(s, len); + return ostd::ConstCharRange(p_s, p_len); default: break; } @@ -702,14 +698,14 @@ void CsValue::get_val(CsValue &r) const { case VAL_STR: case VAL_MACRO: case VAL_CSTR: { - r.set_str(ostd::ConstCharRange(s, len)); + r.set_str(ostd::ConstCharRange(p_s, p_len)); break; } case VAL_INT: - r.set_int(i); + r.set_int(p_i); break; case VAL_FLOAT: - r.set_float(f); + r.set_float(p_f); break; default: r.set_null(); @@ -753,13 +749,13 @@ static inline bool cs_get_bool(ostd::ConstCharRange s) { bool CsValue::get_bool() const { switch (get_type()) { case VAL_FLOAT: - return f != 0; + return p_f != 0; case VAL_INT: - return i != 0; + return p_i != 0; case VAL_STR: case VAL_MACRO: case VAL_CSTR: - return cs_get_bool(ostd::ConstCharRange(s, len)); + return cs_get_bool(ostd::ConstCharRange(p_s, p_len)); default: return false; } @@ -768,17 +764,17 @@ bool CsValue::get_bool() const { void CsAlias::get_cstr(CsValue &v) const { switch (val_v.get_type()) { case VAL_MACRO: - v.set_macro(ostd::ConstCharRange(val_v.cstr, val_v.len)); + v.set_macro(val_v.get_strr()); break; case VAL_STR: case VAL_CSTR: - v.set_cstr(ostd::ConstCharRange(val_v.s, val_v.len)); + v.set_cstr(val_v.get_strr()); break; case VAL_INT: - v.set_str(ostd::move(intstr(val_v.i))); + v.set_str(ostd::move(intstr(val_v.get_int()))); break; case VAL_FLOAT: - v.set_str(ostd::move(floatstr(val_v.f))); + v.set_str(ostd::move(floatstr(val_v.get_float()))); break; default: v.set_cstr(""); @@ -789,17 +785,17 @@ void CsAlias::get_cstr(CsValue &v) const { void CsAlias::get_cval(CsValue &v) const { switch (val_v.get_type()) { case VAL_MACRO: - v.set_macro(ostd::ConstCharRange(val_v.cstr, val_v.len)); + v.set_macro(val_v.get_strr()); break; case VAL_STR: case VAL_CSTR: - v.set_cstr(ostd::ConstCharRange(val_v.s, val_v.len)); + v.set_cstr(val_v.get_strr()); break; case VAL_INT: - v.set_int(val_v.i); + v.set_int(val_v.get_int()); break; case VAL_FLOAT: - v.set_float(val_v.f); + v.set_float(val_v.get_float()); break; default: v.set_null(); diff --git a/cubescript.hh b/cubescript.hh index 7a8a26c..015c23b 100644 --- a/cubescript.hh +++ b/cubescript.hh @@ -67,16 +67,6 @@ OSTD_EXPORT bool cs_code_is_empty(CsBytecode *code); struct CsIdent; struct OSTD_EXPORT CsValue { - union { - CsInt i; /* ID_IVAR, VAL_INT */ - CsFloat f; /* ID_FVAR, VAL_FLOAT */ - CsBytecode *p_code; /* VAL_CODE */ - CsIdent *p_id; /* VAL_IDENT */ - char *s; /* ID_SVAR, VAL_STR */ - char const *cstr; /* VAL_CSTR */ - }; - ostd::Size len; - int get_type() const; void set_int(CsInt val); @@ -112,6 +102,15 @@ struct OSTD_EXPORT CsValue { void copy_arg(CsValue &r) const; private: + union { + CsInt p_i; /* ID_IVAR, VAL_INT */ + CsFloat p_f; /* ID_FVAR, VAL_FLOAT */ + CsBytecode *p_code; /* VAL_CODE */ + CsIdent *p_id; /* VAL_IDENT */ + char *p_s; /* ID_SVAR, VAL_STR */ + char const *p_cstr; /* VAL_CSTR */ + }; + ostd::Size p_len; int p_type; }; @@ -626,14 +625,18 @@ namespace util { auto s = ostd::appender(); switch (vals[i].get_type()) { case VAL_INT: { - auto r = format_int(ostd::forward(writer), vals[i].i); + auto r = format_int( + ostd::forward(writer), vals[i].get_int() + ); if (r > 0) { ret += ostd::Size(r); } break; } case VAL_FLOAT: { - auto r = format_float(ostd::forward(writer), vals[i].f); + auto r = format_float( + ostd::forward(writer), vals[i].get_float() + ); if (r > 0) { ret += ostd::Size(r); } @@ -641,9 +644,11 @@ namespace util { } case VAL_STR: case VAL_CSTR: - case VAL_MACRO: - ret += writer.put_n(vals[i].s, vals[i].len); + case VAL_MACRO: { + auto sv = vals[i].get_strr(); + ret += writer.put_n(sv.data(), sv.size()); break; + } default: break; }