unify TaggedValue and IdentValue (no more separate valtype etc)

master
Daniel Kolesa 2016-08-18 00:53:14 +01:00
parent 479c245190
commit 3f406de1de
4 changed files with 92 additions and 174 deletions

View File

@ -464,7 +464,9 @@ static inline void cs_call_alias(
};
cs.stack = &aliaslink;
if (!a->code) {
a->code = reinterpret_cast<Bytecode *>(compilecode(cs, a->get_str()));
a->code = reinterpret_cast<Bytecode *>(
compilecode(cs, a->val_v.get_str())
);
}
ostd::Uint32 *codep = reinterpret_cast<ostd::Uint32 *>(a->code);
bcode_incr(codep);
@ -972,7 +974,7 @@ static ostd::Uint32 const *runcode(
switch (cs_get_lookupu_type(cs, arg, id, op)) {
case ID_ALIAS:
arg.set_str(ostd::move(
static_cast<Alias *>(id)->get_str()
static_cast<Alias *>(id)->val_v.get_str()
));
continue;
case ID_SVAR:
@ -997,7 +999,7 @@ static ostd::Uint32 const *runcode(
}
case CODE_LOOKUP | RET_STR:
args[numargs++].set_str(
ostd::move(cs_get_lookup_id(cs, op)->get_str())
ostd::move(cs_get_lookup_id(cs, op)->val_v.get_str())
);
continue;
case CODE_LOOKUPARG | RET_STR: {
@ -1005,7 +1007,7 @@ static ostd::Uint32 const *runcode(
if (!a) {
args[numargs++].set_str("");
} else {
args[numargs++].set_str(ostd::move(a->get_str()));
args[numargs++].set_str(ostd::move(a->val_v.get_str()));
}
continue;
}
@ -1014,7 +1016,7 @@ static ostd::Uint32 const *runcode(
TaggedValue &arg = args[numargs - 1];
switch (cs_get_lookupu_type(cs, arg, id, op)) {
case ID_ALIAS:
arg.set_int(static_cast<Alias *>(id)->get_int());
arg.set_int(static_cast<Alias *>(id)->val_v.get_int());
continue;
case ID_SVAR:
arg.set_int(cs_parse_int(
@ -1036,7 +1038,7 @@ static ostd::Uint32 const *runcode(
}
case CODE_LOOKUP | RET_INT:
args[numargs++].set_int(
cs_get_lookup_id(cs, op)->get_int()
cs_get_lookup_id(cs, op)->val_v.get_int()
);
continue;
case CODE_LOOKUPARG | RET_INT: {
@ -1044,7 +1046,7 @@ static ostd::Uint32 const *runcode(
if (!a) {
args[numargs++].set_int(0);
} else {
args[numargs++].set_int(a->get_int());
args[numargs++].set_int(a->val_v.get_int());
}
continue;
}
@ -1053,7 +1055,9 @@ static ostd::Uint32 const *runcode(
TaggedValue &arg = args[numargs - 1];
switch (cs_get_lookupu_type(cs, arg, id, op)) {
case ID_ALIAS:
arg.set_float(static_cast<Alias *>(id)->get_float());
arg.set_float(
static_cast<Alias *>(id)->val_v.get_float()
);
continue;
case ID_SVAR:
arg.set_float(cs_parse_float(
@ -1077,7 +1081,7 @@ static ostd::Uint32 const *runcode(
}
case CODE_LOOKUP | RET_FLOAT:
args[numargs++].set_float(
cs_get_lookup_id(cs, op)->get_float()
cs_get_lookup_id(cs, op)->val_v.get_float()
);
continue;
case CODE_LOOKUPARG | RET_FLOAT: {
@ -1085,7 +1089,7 @@ static ostd::Uint32 const *runcode(
if (!a) {
args[numargs++].set_float(CsFloat(0));
} else {
args[numargs++].set_float(a->get_float());
args[numargs++].set_float(a->val_v.get_float());
}
continue;
}
@ -1094,7 +1098,7 @@ static ostd::Uint32 const *runcode(
TaggedValue &arg = args[numargs - 1];
switch (cs_get_lookupu_type(cs, arg, id, op)) {
case ID_ALIAS:
static_cast<Alias *>(id)->get_val(arg);
static_cast<Alias *>(id)->val_v.get_val(arg);
continue;
case ID_SVAR:
arg.set_str(*static_cast<Svar *>(id)->storage);
@ -1113,14 +1117,14 @@ static ostd::Uint32 const *runcode(
}
}
case CODE_LOOKUP | RET_NULL:
cs_get_lookup_id(cs, op)->get_val(args[numargs++]);
cs_get_lookup_id(cs, op)->val_v.get_val(args[numargs++]);
continue;
case CODE_LOOKUPARG | RET_NULL: {
Alias *a = cs_get_lookuparg_id(cs, op);
if (!a) {
args[numargs++].set_null();
} else {
a->get_val(args[numargs++]);
a->val_v.get_val(args[numargs++]);
}
continue;
}
@ -1534,7 +1538,7 @@ noid:
force_arg(result, op & CODE_RET_MASK);
continue;
}
if (a->get_valtype() == VAL_NULL) {
if (a->val_v.get_type() == VAL_NULL) {
goto noid;
}
idarg.cleanup();
@ -1630,7 +1634,7 @@ void CsState::run_ret(Ident *id, TvalRange args, TaggedValue &ret) {
break;
}
}
if (a->get_valtype() == VAL_NULL) {
if (a->val_v.get_type() == VAL_NULL) {
break;
}
cs_call_alias(

View File

@ -72,39 +72,39 @@ Svar::Svar(ostd::ConstCharRange n, char **s, VarCb f, int flagsv):
/* ID_ALIAS */
Alias::Alias(ostd::ConstCharRange n, char *a, int flagsv):
valtype(VAL_STR), code(nullptr), stack(nullptr)
code(nullptr), stack(nullptr)
{
type = ID_ALIAS;
flags = flagsv;
name = n;
val.s = a;
val.len = strlen(a);
val_v.set_mstr(a);
}
Alias::Alias(ostd::ConstCharRange n, CsInt a, int flagsv):
valtype(VAL_INT), code(nullptr), stack(nullptr)
code(nullptr), stack(nullptr)
{
type = ID_ALIAS;
flags = flagsv;
name = n;
val.i = a;
val_v.set_int(a);
}
Alias::Alias(ostd::ConstCharRange n, CsFloat a, int flagsv):
valtype(VAL_FLOAT), code(nullptr), stack(nullptr)
code(nullptr), stack(nullptr)
{
type = ID_ALIAS;
flags = flagsv;
name = n;
val.f = a;
val_v.set_float(a);
}
Alias::Alias(ostd::ConstCharRange n, int flagsv):
valtype(VAL_NULL), code(nullptr), stack(nullptr)
code(nullptr), stack(nullptr)
{
type = ID_ALIAS;
flags = flagsv;
name = n;
val_v.set_null();
}
Alias::Alias(ostd::ConstCharRange n, TaggedValue const &v, int flagsv):
valtype(v.p_type), code(nullptr), stack(nullptr), val(v)
code(nullptr), stack(nullptr), val_v(v)
{
type = ID_ALIAS;
flags = flagsv;
@ -272,13 +272,9 @@ void CsState::clear_override(Ident &id) {
switch (id.type) {
case ID_ALIAS: {
Alias &a = static_cast<Alias &>(id);
if (a.get_valtype() == VAL_STR) {
delete[] a.val.s;
}
a.val_v.cleanup();
a.clean_code();
a.valtype = VAL_STR;
a.val.s = cs_dup_ostr("");
a.val.len = 0;
a.val_v.set_str("");
break;
}
case ID_IVAR: {
@ -539,50 +535,34 @@ ostd::ConstCharRange TaggedValue::force_str() {
return s;
}
static inline CsInt cs_get_int(IdentValue const &v, int type) {
switch (type) {
CsInt TaggedValue::get_int() const {
switch (get_type()) {
case VAL_FLOAT:
return CsInt(v.f);
return CsInt(f);
case VAL_INT:
return v.i;
return i;
case VAL_STR:
case VAL_MACRO:
case VAL_CSTR:
return cs_parse_int(v.s);
return cs_parse_int(s);
}
return 0;
}
CsInt TaggedValue::get_int() const {
return cs_get_int(*this, get_type());
}
CsInt Alias::get_int() const {
return cs_get_int(val, get_valtype());
}
static inline CsFloat cs_get_float(IdentValue const &v, int type) {
switch (type) {
CsFloat TaggedValue::get_float() const {
switch (get_type()) {
case VAL_FLOAT:
return v.f;
return f;
case VAL_INT:
return CsFloat(v.i);
return CsFloat(i);
case VAL_STR:
case VAL_MACRO:
case VAL_CSTR:
return cs_parse_float(v.s);
return cs_parse_float(s);
}
return 0.0f;
}
CsFloat TaggedValue::get_float() const {
return cs_get_float(*this, get_type());
}
CsFloat Alias::get_float() const {
return cs_get_float(val, get_valtype());
}
Bytecode *TaggedValue::get_code() const {
if (get_type() != VAL_CODE) {
return nullptr;
@ -597,61 +577,45 @@ Ident *TaggedValue::get_ident() const {
return id;
}
static inline ostd::String cs_get_str(IdentValue const &v, int type) {
switch (type) {
ostd::String TaggedValue::get_str() const {
switch (get_type()) {
case VAL_STR:
case VAL_MACRO:
case VAL_CSTR:
return ostd::ConstCharRange(v.s, v.len);
return ostd::ConstCharRange(s, len);
case VAL_INT:
return intstr(v.i);
return intstr(i);
case VAL_FLOAT:
return floatstr(v.f);
return floatstr(f);
}
return ostd::String("");
}
ostd::String TaggedValue::get_str() const {
return cs_get_str(*this, get_type());
}
ostd::String Alias::get_str() const {
return cs_get_str(val, get_valtype());
}
static inline ostd::ConstCharRange cs_get_strr(IdentValue const &v, int type) {
switch (type) {
ostd::ConstCharRange TaggedValue::get_strr() const {
switch (get_type()) {
case VAL_STR:
case VAL_MACRO:
case VAL_CSTR:
return ostd::ConstCharRange(v.s, v.len);
return ostd::ConstCharRange(s, len);
default:
break;
}
return ostd::ConstCharRange();
}
ostd::ConstCharRange TaggedValue::get_strr() const {
return cs_get_strr(*this, get_type());
}
ostd::ConstCharRange Alias::get_strr() const {
return cs_get_strr(val, get_valtype());
}
static inline void cs_get_val(IdentValue const &v, int type, TaggedValue &r) {
switch (type) {
void TaggedValue::get_val(TaggedValue &r) const {
switch (get_type()) {
case VAL_STR:
case VAL_MACRO:
case VAL_CSTR: {
r.set_str(ostd::ConstCharRange(v.s, v.len));
r.set_str(ostd::ConstCharRange(s, len));
break;
}
case VAL_INT:
r.set_int(v.i);
r.set_int(i);
break;
case VAL_FLOAT:
r.set_float(v.f);
r.set_float(f);
break;
default:
r.set_null();
@ -659,14 +623,6 @@ static inline void cs_get_val(IdentValue const &v, int type, TaggedValue &r) {
}
}
void TaggedValue::get_val(TaggedValue &r) const {
cs_get_val(*this, get_type(), r);
}
void Alias::get_val(TaggedValue &r) const {
cs_get_val(val, get_valtype(), r);
}
OSTD_EXPORT bool code_is_empty(Bytecode const *code) {
if (!code) {
return true;
@ -716,19 +672,19 @@ bool TaggedValue::get_bool() const {
}
void Alias::get_cstr(TaggedValue &v) const {
switch (get_valtype()) {
switch (val_v.get_type()) {
case VAL_MACRO:
v.set_macro(val.code, val.len);
v.set_macro(val_v.code, val_v.len);
break;
case VAL_STR:
case VAL_CSTR:
v.set_cstr(ostd::ConstCharRange(val.s, val.len));
v.set_cstr(ostd::ConstCharRange(val_v.s, val_v.len));
break;
case VAL_INT:
v.set_str(ostd::move(intstr(val.i)));
v.set_str(ostd::move(intstr(val_v.i)));
break;
case VAL_FLOAT:
v.set_str(ostd::move(floatstr(val.f)));
v.set_str(ostd::move(floatstr(val_v.f)));
break;
default:
v.set_cstr("");
@ -737,19 +693,19 @@ void Alias::get_cstr(TaggedValue &v) const {
}
void Alias::get_cval(TaggedValue &v) const {
switch (get_valtype()) {
switch (val_v.get_type()) {
case VAL_MACRO:
v.set_macro(val.code, val.len);
v.set_macro(val_v.code, val_v.len);
break;
case VAL_STR:
case VAL_CSTR:
v.set_cstr(ostd::ConstCharRange(val.s, val.len));
v.set_cstr(ostd::ConstCharRange(val_v.s, val_v.len));
break;
case VAL_INT:
v.set_int(val.i);
v.set_int(val_v.i);
break;
case VAL_FLOAT:
v.set_float(val.f);
v.set_float(val_v.f);
break;
default:
v.set_null();
@ -766,8 +722,7 @@ void Alias::clean_code() {
}
void Alias::push_arg(TaggedValue const &v, IdentStack &st, bool um) {
st.val = val;
st.valtype = valtype;
st.val_s = val_v;
st.next = stack;
stack = &st;
set_value(v);
@ -782,9 +737,7 @@ void Alias::pop_arg() {
return;
}
IdentStack *st = stack;
if (get_valtype() == VAL_STR) {
delete[] val.s;
}
val_v.cleanup();
set_value(*stack);
clean_code();
stack = st->next;
@ -792,8 +745,7 @@ void Alias::pop_arg() {
void Alias::undo_arg(IdentStack &st) {
IdentStack *prev = stack;
st.val = val;
st.valtype = valtype;
st.val_s = val_v;
st.next = prev;
stack = prev->next;
set_value(*prev);
@ -802,8 +754,7 @@ void Alias::undo_arg(IdentStack &st) {
void Alias::redo_arg(IdentStack const &st) {
IdentStack *prev = st.next;
prev->val = val;
prev->valtype = valtype;
prev->val_s = val_v;
stack = prev;
set_value(st);
clean_code();
@ -811,9 +762,7 @@ void Alias::redo_arg(IdentStack const &st) {
void Alias::set_arg(CsState &cs, TaggedValue &v) {
if (cs.stack->usedargs & (1 << index)) {
if (get_valtype() == VAL_STR) {
delete[] val.s;
}
val_v.cleanup();
set_value(v);
clean_code();
} else {
@ -823,9 +772,7 @@ void Alias::set_arg(CsState &cs, TaggedValue &v) {
}
void Alias::set_alias(CsState &cs, TaggedValue &v) {
if (get_valtype() == VAL_STR) {
delete[] val.s;
}
val_v.cleanup();
set_value(v);
clean_code();
flags = (flags & cs.identflags) | cs.identflags;
@ -1003,7 +950,7 @@ CsState::get_alias_val(ostd::ConstCharRange name) {
if ((a->index < MaxArguments) && !(stack->usedargs & (1 << a->index))) {
return ostd::nothing;
}
return ostd::move(a->get_str());
return ostd::move(a->val_v.get_str());
}
CsInt cs_clamp_var(CsState &cs, Ivar *iv, CsInt v) {
@ -1193,16 +1140,8 @@ void cs_init_lib_io(CsState &cs) {
static inline void cs_set_iter(Alias &a, CsInt i, IdentStack &stack) {
if (a.stack == &stack) {
if (a.get_valtype() != VAL_INT) {
if (a.get_valtype() == VAL_STR) {
delete[] a.val.s;
a.val.s = nullptr;
a.val.len = 0;
}
a.clean_code();
a.valtype = VAL_INT;
}
a.val.i = i;
a.val_v.cleanup();
a.val_v.set_int(i);
return;
}
TaggedValue v;

View File

@ -67,7 +67,9 @@ OSTD_EXPORT bool code_is_empty(Bytecode const *code);
struct Ident;
struct Alias;
struct IdentValue {
struct OSTD_EXPORT TaggedValue {
friend struct Alias;
union {
CsInt i; /* ID_IVAR, VAL_INT */
CsFloat f; /* ID_FVAR, VAL_FLOAT */
@ -77,10 +79,6 @@ struct IdentValue {
char const *cstr; /* VAL_CSTR */
};
ostd::Size len;
};
struct OSTD_EXPORT TaggedValue: IdentValue {
friend struct Alias;
int get_type() const {
return p_type;
@ -95,6 +93,13 @@ struct OSTD_EXPORT TaggedValue: IdentValue {
f = val;
}
void set_str(ostd::String val) {
if (val.size() == 0) {
/* ostd zero length strings cannot be disowned */
char *buf = new char[1];
buf[0] = '\0';
set_mstr(buf);
return;
}
ostd::CharRange cr = val.iter();
val.disown();
set_mstr(cr);
@ -159,8 +164,7 @@ private:
using TvalRange = ostd::PointerRange<TaggedValue>;
struct IdentStack {
IdentValue val;
int valtype;
TaggedValue val_s;
IdentStack *next;
};
@ -259,10 +263,9 @@ struct OSTD_EXPORT Svar: Var {
};
struct OSTD_EXPORT Alias: Ident {
int valtype;
Bytecode *code;
IdentStack *stack;
IdentValue val;
TaggedValue val_v;
Alias(ostd::ConstCharRange n, char *a, int flags);
Alias(ostd::ConstCharRange n, CsInt a, int flags);
@ -271,27 +274,16 @@ struct OSTD_EXPORT Alias: Ident {
Alias(ostd::ConstCharRange n, TaggedValue const &v, int flags);
void set_value(TaggedValue const &v) {
valtype = v.get_type();
val = v;
val_v = v;
}
void set_value(IdentStack const &v) {
valtype = v.valtype;
val = v.val;
val_v = v.val_s;
}
CsFloat get_float() const;
CsInt get_int() const;
ostd::String get_str() const;
ostd::ConstCharRange get_strr() const;
void get_val(TaggedValue &r) const;
void get_cstr(TaggedValue &v) const;
void get_cval(TaggedValue &v) const;
int get_valtype() const {
return valtype;
}
void push_arg(TaggedValue const &v, IdentStack &st, bool um = true);
void pop_arg();
void undo_arg(IdentStack &st);
@ -303,12 +295,8 @@ struct OSTD_EXPORT Alias: Ident {
void clean_code();
void force_null() {
if (valtype == VAL_STR) {
delete[] val.s;
val.s = nullptr;
val.len = 0;
}
valtype = VAL_NULL;
val_v.cleanup();
val_v.set_null();
}
};

View File

@ -70,14 +70,9 @@ static inline void cs_list_assoc(TvalRange args, TaggedValue &res, F cmp) {
static inline void cs_set_iter(Alias &a, char *val, IdentStack &stack) {
if (a.stack == &stack) {
if (a.get_valtype() == VAL_STR) {
delete[] a.val.s;
} else {
a.valtype = VAL_STR;
}
a.val_v.cleanup();
a.clean_code();
a.val.s = val;
a.val.len = strlen(val);
a.val_v.set_mstr(val);
return;
}
TaggedValue v;
@ -554,17 +549,9 @@ struct ListSortFun {
bool operator()(ListSortItem const &xval, ListSortItem const &yval) {
x->clean_code();
if (x->get_valtype() != VAL_CSTR) {
x->valtype = VAL_CSTR;
}
x->val.cstr = xval.str;
x->val.len = strlen(xval.str);
x->val_v.set_cstr(xval.str);
y->clean_code();
if (y->get_valtype() != VAL_CSTR) {
y->valtype = VAL_CSTR;
}
y->val.cstr = yval.str;
y->val.len = strlen(yval.str);
y->val_v.set_cstr(yval.str);
return cs.run_bool(body);
}
};