store length information in type/valtype

master
Daniel Kolesa 2015-08-13 22:23:46 +01:00
parent c684239bf0
commit 538a7b73e1
2 changed files with 37 additions and 31 deletions

View File

@ -99,7 +99,7 @@ Ident::Ident(int t, ostd::ConstCharRange n, char **s, IdentFunc f, int flags)
/* ID_ALIAS */ /* ID_ALIAS */
Ident::Ident(int t, ostd::ConstCharRange n, char *a, int flags) Ident::Ident(int t, ostd::ConstCharRange n, char *a, int flags)
: type(t), valtype(VAL_STR), flags(flags), name(n), code(nullptr), : type(t), valtype(VAL_STR | (n.size() << 4)), flags(flags), name(n), code(nullptr),
stack(nullptr) { stack(nullptr) {
val.s = a; val.s = a;
} }
@ -118,7 +118,7 @@ Ident::Ident(int t, ostd::ConstCharRange n, int flags)
stack(nullptr) { stack(nullptr) {
} }
Ident::Ident(int t, ostd::ConstCharRange n, const TaggedValue &v, int flags) Ident::Ident(int t, ostd::ConstCharRange n, const TaggedValue &v, int flags)
: type(t), valtype(v.get_type()), flags(flags), name(n), code(nullptr), : type(t), valtype(v.p_type), flags(flags), name(n), code(nullptr),
stack(nullptr) { stack(nullptr) {
val = v; val = v;
} }
@ -226,7 +226,7 @@ void CsState::clear_override(Ident &id) {
if (!(id.flags & IDF_OVERRIDDEN)) return; if (!(id.flags & IDF_OVERRIDDEN)) return;
switch (id.type) { switch (id.type) {
case ID_ALIAS: case ID_ALIAS:
if (id.valtype == VAL_STR) { if (id.get_valtype() == VAL_STR) {
if (!id.val.s[0]) break; if (!id.val.s[0]) break;
delete[] id.val.s; delete[] id.val.s;
} }
@ -496,7 +496,7 @@ inline int TaggedValue::get_int() const {
} }
inline int Ident::get_int() const { inline int Ident::get_int() const {
return cs_get_int(val, valtype); return cs_get_int(val, get_valtype());
} }
static inline float cs_get_float(const IdentValue &v, int type) { static inline float cs_get_float(const IdentValue &v, int type) {
@ -518,15 +518,15 @@ inline float TaggedValue::get_float() const {
} }
inline float Ident::get_float() const { inline float Ident::get_float() const {
return cs_get_float(val, valtype); return cs_get_float(val, get_valtype());
} }
static inline ostd::ConstCharRange cs_get_str(const IdentValue &v, int type) { static inline ostd::ConstCharRange cs_get_str(const IdentValue &v, int type, int len) {
switch (type) { switch (type) {
case VAL_STR: case VAL_STR:
case VAL_MACRO: case VAL_MACRO:
case VAL_CSTR: case VAL_CSTR:
return v.s; return ostd::ConstCharRange(v.s, len);
case VAL_INT: case VAL_INT:
return intstr(v.i); return intstr(v.i);
case VAL_FLOAT: case VAL_FLOAT:
@ -536,11 +536,11 @@ static inline ostd::ConstCharRange cs_get_str(const IdentValue &v, int type) {
} }
inline ostd::ConstCharRange TaggedValue::get_str() const { inline ostd::ConstCharRange TaggedValue::get_str() const {
return cs_get_str(*this, get_type()); return cs_get_str(*this, get_type(), p_type >> 4);
} }
inline ostd::ConstCharRange Ident::get_str() const { inline ostd::ConstCharRange Ident::get_str() const {
return cs_get_str(val, valtype); return cs_get_str(val, get_valtype(), valtype >> 4);
} }
static inline void cs_get_val(const IdentValue &v, int type, TaggedValue &r) { static inline void cs_get_val(const IdentValue &v, int type, TaggedValue &r) {
@ -567,11 +567,11 @@ inline void TaggedValue::get_val(TaggedValue &r) const {
} }
inline void Ident::get_val(TaggedValue &r) const { inline void Ident::get_val(TaggedValue &r) const {
cs_get_val(val, valtype, r); cs_get_val(val, get_valtype(), r);
} }
inline void Ident::get_cstr(TaggedValue &v) const { inline void Ident::get_cstr(TaggedValue &v) const {
switch (valtype) { switch (get_valtype()) {
case VAL_MACRO: case VAL_MACRO:
v.set_macro(val.code); v.set_macro(val.code);
break; break;
@ -592,7 +592,7 @@ inline void Ident::get_cstr(TaggedValue &v) const {
} }
inline void Ident::get_cval(TaggedValue &v) const { inline void Ident::get_cval(TaggedValue &v) const {
switch (valtype) { switch (get_valtype()) {
case VAL_MACRO: case VAL_MACRO:
v.set_macro(val.code); v.set_macro(val.code);
break; break;
@ -639,7 +639,7 @@ void Ident::push_arg(const TaggedValue &v, IdentStack &st, bool um) {
void Ident::pop_arg() { void Ident::pop_arg() {
if (!stack) return; if (!stack) return;
IdentStack *st = stack; IdentStack *st = stack;
if (valtype == VAL_STR) delete[] val.s; if (get_valtype() == VAL_STR) delete[] val.s;
set_value(*stack); set_value(*stack);
clean_code(); clean_code();
stack = st->next; stack = st->next;
@ -675,7 +675,7 @@ void Ident::pop_alias() {
void Ident::set_arg(CsState &cs, TaggedValue &v) { void Ident::set_arg(CsState &cs, TaggedValue &v) {
if (cs.stack->usedargs & (1 << index)) { if (cs.stack->usedargs & (1 << index)) {
if (valtype == VAL_STR) delete[] val.s; if (get_valtype() == VAL_STR) delete[] val.s;
set_value(v); set_value(v);
clean_code(); clean_code();
} else { } else {
@ -685,7 +685,7 @@ void Ident::set_arg(CsState &cs, TaggedValue &v) {
} }
void Ident::set_alias(CsState &cs, TaggedValue &v) { void Ident::set_alias(CsState &cs, TaggedValue &v) {
if (valtype == VAL_STR) delete[] val.s; if (get_valtype() == VAL_STR) delete[] val.s;
set_value(v); set_value(v);
clean_code(); clean_code();
flags = (flags & cs.identflags) | cs.identflags; flags = (flags & cs.identflags) | cs.identflags;
@ -3303,7 +3303,7 @@ noid:
FORCERESULT; FORCERESULT;
case ID_ALIAS: case ID_ALIAS:
if (id->index < MAX_ARGUMENTS && !(cs.stack->usedargs & (1 << id->index))) FORCERESULT; if (id->index < MAX_ARGUMENTS && !(cs.stack->usedargs & (1 << id->index))) FORCERESULT;
if (id->valtype == VAL_NULL) goto noid; if (id->get_valtype() == VAL_NULL) goto noid;
idarg.cleanup(); idarg.cleanup();
CALLALIAS(cs, result); CALLALIAS(cs, result);
continue; continue;
@ -3367,7 +3367,7 @@ void CsState::run_ret(Ident *id, ostd::PointerRange<TaggedValue> args,
break; break;
case ID_ALIAS: case ID_ALIAS:
if (id->index < MAX_ARGUMENTS && !(stack->usedargs & (1 << id->index))) break; if (id->index < MAX_ARGUMENTS && !(stack->usedargs & (1 << id->index))) break;
if (id->valtype == VAL_NULL) break; if (id->get_valtype() == VAL_NULL) break;
#define callargs numargs #define callargs numargs
#define offset 0 #define offset 0
#define op RET_NULL #define op RET_NULL
@ -3652,8 +3652,8 @@ void init_lib_base(CsState &cs) {
static inline void cs_set_iter(Ident &id, int i, IdentStack &stack) { static inline void cs_set_iter(Ident &id, int i, IdentStack &stack) {
if (id.stack == &stack) { if (id.stack == &stack) {
if (id.valtype != VAL_INT) { if (id.get_valtype() != VAL_INT) {
if (id.valtype == VAL_STR) delete[] id.val.s; if (id.get_valtype() == VAL_STR) delete[] id.val.s;
id.clean_code(); id.clean_code();
id.valtype = VAL_INT; id.valtype = VAL_INT;
} }
@ -3943,10 +3943,10 @@ namespace util {
static inline void cs_set_iter(Ident &id, char *val, IdentStack &stack) { static inline void cs_set_iter(Ident &id, char *val, IdentStack &stack) {
if (id.stack == &stack) { if (id.stack == &stack) {
if (id.valtype == VAL_STR) if (id.get_valtype() == VAL_STR)
delete[] id.val.s; delete[] id.val.s;
else else
id.valtype = VAL_STR; id.valtype = VAL_STR | (strlen(val) << 4);
id.clean_code(); id.clean_code();
id.val.s = val; id.val.s = val;
return; return;
@ -4348,14 +4348,14 @@ struct ListSortFun {
ostd::Uint32 *body; ostd::Uint32 *body;
bool operator()(const ListSortItem &xval, const ListSortItem &yval) { bool operator()(const ListSortItem &xval, const ListSortItem &yval) {
if (x->valtype != VAL_CSTR)
x->valtype = VAL_CSTR;
x->clean_code(); x->clean_code();
x->val.code = (const ostd::Uint32 *)xval.str; if (x->get_valtype() != VAL_CSTR)
if (y->valtype != VAL_CSTR) x->valtype = VAL_CSTR | (strlen(xval.str) << 4);
y->valtype = VAL_CSTR; x->val.cstr = xval.str;
y->clean_code(); y->clean_code();
y->val.code = (const ostd::Uint32 *)yval.str; if (y->get_valtype() != VAL_CSTR)
y->valtype = VAL_CSTR | (strlen(xval.str) << 4);
y->val.cstr = yval.str;
return cs.run_bool(body); return cs.run_bool(body);
} }
}; };

View File

@ -94,8 +94,10 @@ struct IdentValue {
}; };
struct TaggedValue: IdentValue { struct TaggedValue: IdentValue {
friend struct Ident;
int get_type() const { int get_type() const {
return p_type; return p_type & 0xF;
} }
void set_int(int val) { void set_int(int val) {
@ -107,7 +109,7 @@ struct TaggedValue: IdentValue {
f = val; f = val;
} }
void set_str(char *val) { void set_str(char *val) {
p_type = VAL_STR; p_type = VAL_STR | (strlen(val) << 4);
s = val; s = val;
} }
void set_null() { void set_null() {
@ -119,11 +121,11 @@ struct TaggedValue: IdentValue {
code = val; code = val;
} }
void set_macro(const ostd::Uint32 *val) { void set_macro(const ostd::Uint32 *val) {
p_type = VAL_MACRO; p_type = VAL_MACRO | (strlen((const char *)val) << 4);
code = val; code = val;
} }
void set_cstr(const char *val) { void set_cstr(const char *val) {
p_type = VAL_CSTR; p_type = VAL_CSTR | (strlen(val) << 4);
cstr = val; cstr = val;
} }
void set_ident(Ident *val) { void set_ident(Ident *val) {
@ -272,6 +274,10 @@ struct Ident {
void set_arg(CsState &cs, TaggedValue &v); void set_arg(CsState &cs, TaggedValue &v);
void set_alias(CsState &cs, TaggedValue &v); void set_alias(CsState &cs, TaggedValue &v);
int get_valtype() const {
return valtype & 0xF;
}
}; };
struct IdentLink { struct IdentLink {