master
Daniel Kolesa 2016-08-30 21:29:09 +01:00
parent b8d35ae0ee
commit 83c50ebf67
4 changed files with 71 additions and 73 deletions

View File

@ -148,15 +148,17 @@ CsBytecodeRef &CsBytecodeRef::operator=(CsBytecodeRef &&v) {
return *this;
}
static inline ostd::Uint32 const *forcecode(CsState &cs, CsValue &v) {
if (v.get_type() != VAL_CODE) {
static inline ostd::Uint32 *forcecode(CsState &cs, CsValue &v) {
ostd::Uint32 *code = reinterpret_cast<ostd::Uint32 *>(v.get_code());
if (!code) {
GenState gs(cs);
gs.code.reserve(64);
gs.gen_main(v.get_str());
v.cleanup();
v.set_code(reinterpret_cast<CsBytecode *>(gs.code.disown() + 1));
code = reinterpret_cast<ostd::Uint32 *>(v.get_code());
}
return reinterpret_cast<ostd::Uint32 const *>(v.code);
return code;
}
static inline void forcecond(CsState &cs, CsValue &v) {
@ -207,8 +209,8 @@ static inline void free_args(CsValue *args, int &oldnum, int newnum) {
oldnum = newnum;
}
static ostd::Uint32 const *skipcode(
ostd::Uint32 const *code, CsValue *result = nullptr
static ostd::Uint32 *skipcode(
ostd::Uint32 *code, CsValue *result = nullptr
) {
int depth = 0;
for (;;) {
@ -264,12 +266,12 @@ void CsValue::copy_arg(CsValue &r) const {
r.set_str(ostd::ConstCharRange(s, len));
break;
case VAL_CODE: {
ostd::Uint32 const *bcode = reinterpret_cast<ostd::Uint32 const *>(code);
ostd::Uint32 const *end = skipcode(bcode);
ostd::Uint32 *bcode = reinterpret_cast<ostd::Uint32 *>(r.get_code());
ostd::Uint32 *end = skipcode(bcode);
ostd::Uint32 *dst = new ostd::Uint32[end - bcode + 1];
*dst++ = CODE_START;
memcpy(dst, bcode, (end - bcode) * sizeof(ostd::Uint32));
r.set_code(reinterpret_cast<CsBytecode const *>(dst));
r.set_code(reinterpret_cast<CsBytecode *>(dst));
break;
}
default:
@ -444,9 +446,7 @@ cleanup:
}
}
static ostd::Uint32 const *runcode(
CsState &cs, ostd::Uint32 const *code, CsValue &result
);
static ostd::Uint32 *runcode(CsState &cs, ostd::Uint32 *code, CsValue &result);
static inline void cs_call_alias(
CsState &cs, CsAlias *a, CsValue *args, CsValue &result,
@ -559,9 +559,7 @@ static inline int cs_get_lookupu_type(
return ID_UNKNOWN;
}
static ostd::Uint32 const *runcode(
CsState &cs, ostd::Uint32 const *code, CsValue &result
) {
static ostd::Uint32 *runcode(CsState &cs, ostd::Uint32 *code, CsValue &result) {
result.set_null();
if (rundepth >= MaxRunDepth) {
cs_debug_code(cs, "exceeded recursion limit");
@ -677,11 +675,11 @@ static ostd::Uint32 const *runcode(
int numlocals = op >> 8, offset = numargs - numlocals;
CsIdentStack locals[MaxArguments];
for (int i = 0; i < numlocals; ++i) {
cs_push_alias(args[offset + i].id, locals[i]);
cs_push_alias(args[offset + i].get_ident(), locals[i]);
}
code = runcode(cs, code, result);
for (int i = offset; i < numargs; i++) {
cs_pop_alias(args[i].id);
cs_pop_alias(args[i].get_ident());
}
goto exit;
}
@ -693,7 +691,7 @@ static ostd::Uint32 const *runcode(
if (cs.p_stack != &cs.noalias) {
cs_do_args(cs, [&]() {
result.cleanup();
cs.run_ret(args[--numargs].code, result);
cs.run_ret(args[--numargs].get_code(), result);
args[numargs].cleanup();
force_arg(result, op & CODE_RET_MASK);
});
@ -705,7 +703,7 @@ static ostd::Uint32 const *runcode(
case CODE_DO | RET_INT:
case CODE_DO | RET_FLOAT:
result.cleanup();
cs.run_ret(args[--numargs].code, result);
cs.run_ret(args[--numargs].get_code(), result);
args[numargs].cleanup();
force_arg(result, op & CODE_RET_MASK);
continue;
@ -736,7 +734,7 @@ static ostd::Uint32 const *runcode(
result.cleanup();
--numargs;
if (args[numargs].get_type() == VAL_CODE) {
cs.run_ret(args[numargs].code, result);
cs.run_ret(args[numargs].get_code(), result);
args[numargs].cleanup();
} else {
result = args[numargs];
@ -751,7 +749,7 @@ static ostd::Uint32 const *runcode(
result.cleanup();
--numargs;
if (args[numargs].get_type() == VAL_CODE) {
cs.run_ret(args[numargs].code, result);
cs.run_ret(args[numargs].get_code(), result);
args[numargs].cleanup();
} else {
result = args[numargs];
@ -764,9 +762,9 @@ static ostd::Uint32 const *runcode(
case CODE_MACRO: {
ostd::Uint32 len = op >> 8;
args[numargs++].set_macro(
reinterpret_cast<CsBytecode const *>(code), len
);
args[numargs++].set_macro(ostd::ConstCharRange(
reinterpret_cast<char const *>(code), len
));
code += len / sizeof(ostd::Uint32) + 1;
continue;
}
@ -869,7 +867,7 @@ static ostd::Uint32 const *runcode(
case CODE_BLOCK: {
ostd::Uint32 len = op >> 8;
args[numargs++].set_code(
reinterpret_cast<CsBytecode const *>(code + 1)
reinterpret_cast<CsBytecode *>(code + 1)
);
code += len;
continue;
@ -908,7 +906,7 @@ static ostd::Uint32 const *runcode(
break;
}
arg.set_code(
reinterpret_cast<CsBytecode const *>(gs.code.disown() + 1)
reinterpret_cast<CsBytecode *>(gs.code.disown() + 1)
);
continue;
}
@ -923,7 +921,7 @@ static ostd::Uint32 const *runcode(
gs.code.reserve(64);
gs.gen_main(arg.s);
arg.cleanup();
arg.set_code(reinterpret_cast<CsBytecode const *>(
arg.set_code(reinterpret_cast<CsBytecode *>(
gs.code.disown() + 1
));
} else {
@ -1498,7 +1496,7 @@ noid:
}
code = runcode(cs, code, result);
for (ostd::Size j = 0; j < ostd::Size(callargs); ++j) {
cs_pop_alias(args[offset + j].id);
cs_pop_alias(args[offset + j].get_ident());
}
goto exit;
}
@ -1567,8 +1565,8 @@ exit:
return code;
}
void CsState::run_ret(CsBytecode const *code, CsValue &ret) {
runcode(*this, reinterpret_cast<ostd::Uint32 const *>(code), ret);
void CsState::run_ret(CsBytecode *code, CsValue &ret) {
runcode(*this, reinterpret_cast<ostd::Uint32 *>(code), ret);
}
void CsState::run_ret(ostd::ConstCharRange code, CsValue &ret) {
@ -1657,7 +1655,7 @@ void CsState::run_ret(CsIdent *id, CsValueRange args, CsValue &ret) {
--rundepth;
}
CsString CsState::run_str(CsBytecode const *code) {
CsString CsState::run_str(CsBytecode *code) {
CsValue ret;
run_ret(code, ret);
CsString s = ret.get_str();
@ -1681,7 +1679,7 @@ CsString CsState::run_str(CsIdent *id, CsValueRange args) {
return s;
}
CsInt CsState::run_int(CsBytecode const *code) {
CsInt CsState::run_int(CsBytecode *code) {
CsValue ret;
run_ret(code, ret);
CsInt i = ret.get_int();
@ -1705,7 +1703,7 @@ CsInt CsState::run_int(CsIdent *id, CsValueRange args) {
return i;
}
CsFloat CsState::run_float(CsBytecode const *code) {
CsFloat CsState::run_float(CsBytecode *code) {
CsValue ret;
run_ret(code, ret);
CsFloat f = ret.get_float();
@ -1729,7 +1727,7 @@ CsFloat CsState::run_float(CsIdent *id, CsValueRange args) {
return f;
}
bool CsState::run_bool(CsBytecode const *code) {
bool CsState::run_bool(CsBytecode *code) {
CsValue ret;
run_ret(code, ret);
bool b = ret.get_bool();
@ -1753,7 +1751,7 @@ bool CsState::run_bool(CsIdent *id, CsValueRange args) {
return b;
}
void CsState::run(CsBytecode const *code) {
void CsState::run(CsBytecode *code) {
CsValue ret;
run_ret(code, ret);
ret.cleanup();

View File

@ -352,7 +352,7 @@ CsIdent *CsState::new_ident(ostd::ConstCharRange name, int flags) {
CsIdent *CsState::force_ident(CsValue &v) {
switch (v.get_type()) {
case VAL_IDENT:
return v.id;
return v.get_ident();
case VAL_MACRO:
case VAL_CSTR: {
CsIdent *id = new_ident(v.s);
@ -488,9 +488,7 @@ void CsValue::cleanup() {
delete[] s;
break;
case VAL_CODE:
ostd::Uint32 *bcode = const_cast<ostd::Uint32 *>(
reinterpret_cast<ostd::Uint32 const *>(code)
);
ostd::Uint32 *bcode = reinterpret_cast<ostd::Uint32 *>(p_code);
if (bcode[-1] == CODE_START) {
delete[] bcode;
}
@ -530,9 +528,9 @@ void CsValue::set_null() {
i = 0;
}
void CsValue::set_code(CsBytecode const *val) {
void CsValue::set_code(CsBytecode *val) {
p_type = VAL_CODE;
code = val;
p_code = val;
}
void CsValue::set_cstr(ostd::ConstCharRange val) {
@ -549,13 +547,13 @@ void CsValue::set_mstr(ostd::CharRange val) {
void CsValue::set_ident(CsIdent *val) {
p_type = VAL_IDENT;
id = val;
p_id = val;
}
void CsValue::set_macro(CsBytecode const *val, ostd::Size ln) {
void CsValue::set_macro(ostd::ConstCharRange val) {
p_type = VAL_MACRO;
len = ln;
code = val;
len = val.size();
cstr = val.data();
}
void CsValue::set(CsValue &tv) {
@ -663,14 +661,14 @@ CsBytecode *CsValue::get_code() const {
if (get_type() != VAL_CODE) {
return nullptr;
}
return const_cast<CsBytecode *>(code);
return p_code;
}
CsIdent *CsValue::get_ident() const {
if (get_type() != VAL_IDENT) {
return nullptr;
}
return id;
return p_id;
}
CsString CsValue::get_str() const {
@ -719,12 +717,12 @@ void CsValue::get_val(CsValue &r) const {
}
}
OSTD_EXPORT bool cs_code_is_empty(CsBytecode const *code) {
OSTD_EXPORT bool cs_code_is_empty(CsBytecode *code) {
if (!code) {
return true;
}
return (
*reinterpret_cast<ostd::Uint32 const *>(code) & CODE_OP_MASK
*reinterpret_cast<ostd::Uint32 *>(code) & CODE_OP_MASK
) == CODE_EXIT;
}
@ -732,7 +730,7 @@ bool CsValue::code_is_empty() const {
if (get_type() != VAL_CODE) {
return true;
}
return cscript::cs_code_is_empty(code);
return cscript::cs_code_is_empty(p_code);
}
static inline bool cs_get_bool(ostd::ConstCharRange s) {
@ -770,7 +768,7 @@ bool CsValue::get_bool() const {
void CsAlias::get_cstr(CsValue &v) const {
switch (val_v.get_type()) {
case VAL_MACRO:
v.set_macro(val_v.code, val_v.len);
v.set_macro(ostd::ConstCharRange(val_v.cstr, val_v.len));
break;
case VAL_STR:
case VAL_CSTR:
@ -791,7 +789,7 @@ 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(val_v.code, val_v.len);
v.set_macro(ostd::ConstCharRange(val_v.cstr, val_v.len));
break;
case VAL_STR:
case VAL_CSTR:
@ -1340,8 +1338,9 @@ void cs_init_lib_base(CsState &cs) {
if (i) {
res.cleanup();
}
if (args[i].get_type() == VAL_CODE) {
cs.run_ret(args[i].code, res);
CsBytecode *code = args[i].get_code();
if (code) {
cs.run_ret(code, res);
} else {
res = args[i];
}
@ -1360,8 +1359,9 @@ void cs_init_lib_base(CsState &cs) {
if (i) {
res.cleanup();
}
if (args[i].get_type() == VAL_CODE) {
cs.run_ret(args[i].code, res);
CsBytecode *code = args[i].get_code();
if (code) {
cs.run_ret(code, res);
} else {
res = args[i];
}
@ -1379,12 +1379,12 @@ void cs_init_lib_base(CsState &cs) {
cs_add_command(cs, "cond", "ee2V", [&cs](CsValueRange args, CsValue &res) {
for (ostd::Size i = 0; i < args.size(); i += 2) {
if ((i + 1) < args.size()) {
if (cs.run_bool(args[i].code)) {
cs.run_ret(args[i + 1].code, res);
if (cs.run_bool(args[i].get_code())) {
cs.run_ret(args[i + 1].get_code(), res);
break;
}
} else {
cs.run_ret(args[i].code, res);
cs.run_ret(args[i].get_code(), res);
break;
}
}
@ -1394,7 +1394,7 @@ void cs_init_lib_base(CsState &cs) {
CsInt val = args[0].get_int();
for (ostd::Size i = 1; (i + 1) < args.size(); i += 2) {
if ((args[i].get_type() == VAL_NULL) || (args[i].get_int() == val)) {
cs.run_ret(args[i + 1].code, res);
cs.run_ret(args[i + 1].get_code(), res);
return;
}
}
@ -1404,7 +1404,7 @@ void cs_init_lib_base(CsState &cs) {
CsFloat val = args[0].get_float();
for (ostd::Size i = 1; (i + 1) < args.size(); i += 2) {
if ((args[i].get_type() == VAL_NULL) || (args[i].get_float() == val)) {
cs.run_ret(args[i + 1].code, res);
cs.run_ret(args[i + 1].get_code(), res);
return;
}
}
@ -1414,7 +1414,7 @@ void cs_init_lib_base(CsState &cs) {
CsString val = args[0].get_str();
for (ostd::Size i = 1; (i + 1) < args.size(); i += 2) {
if ((args[i].get_type() == VAL_NULL) || (args[i].get_str() == val)) {
cs.run_ret(args[i + 1].code, res);
cs.run_ret(args[i + 1].get_code(), res);
return;
}
}

View File

@ -62,7 +62,7 @@ private:
CsBytecode *p_code;
};
OSTD_EXPORT bool cs_code_is_empty(CsBytecode const *code);
OSTD_EXPORT bool cs_code_is_empty(CsBytecode *code);
struct CsIdent;
@ -70,8 +70,8 @@ struct OSTD_EXPORT CsValue {
union {
CsInt i; /* ID_IVAR, VAL_INT */
CsFloat f; /* ID_FVAR, VAL_FLOAT */
CsBytecode const *code; /* VAL_CODE */
CsIdent *id; /* VAL_IDENT */
CsBytecode *p_code; /* VAL_CODE */
CsIdent *p_id; /* VAL_IDENT */
char *s; /* ID_SVAR, VAL_STR */
char const *cstr; /* VAL_CSTR */
};
@ -83,11 +83,11 @@ struct OSTD_EXPORT CsValue {
void set_float(CsFloat val);
void set_str(CsString val);
void set_null();
void set_code(CsBytecode const *val);
void set_code(CsBytecode *val);
void set_cstr(ostd::ConstCharRange val);
void set_mstr(ostd::CharRange val);
void set_ident(CsIdent *val);
void set_macro(CsBytecode const *val, ostd::Size ln);
void set_macro(ostd::ConstCharRange val);
void set(CsValue &tv);
@ -392,27 +392,27 @@ struct OSTD_EXPORT CsState {
ostd::ConstCharRange name, ostd::ConstCharRange args, CmdFunc func
);
CsString run_str(CsBytecode const *code);
CsString run_str(CsBytecode *code);
CsString run_str(ostd::ConstCharRange code);
CsString run_str(CsIdent *id, CsValueRange args);
CsInt run_int(CsBytecode const *code);
CsInt run_int(CsBytecode *code);
CsInt run_int(ostd::ConstCharRange code);
CsInt run_int(CsIdent *id, CsValueRange args);
CsFloat run_float(CsBytecode const *code);
CsFloat run_float(CsBytecode *code);
CsFloat run_float(ostd::ConstCharRange code);
CsFloat run_float(CsIdent *id, CsValueRange args);
bool run_bool(CsBytecode const *code);
bool run_bool(CsBytecode *code);
bool run_bool(ostd::ConstCharRange code);
bool run_bool(CsIdent *id, CsValueRange args);
void run_ret(CsBytecode const *code, CsValue &ret);
void run_ret(CsBytecode *code, CsValue &ret);
void run_ret(ostd::ConstCharRange code, CsValue &ret);
void run_ret(CsIdent *id, CsValueRange args, CsValue &ret);
void run(CsBytecode const *code);
void run(CsBytecode *code);
void run(ostd::ConstCharRange code);
void run(CsIdent *id, CsValueRange args);

View File

@ -76,7 +76,7 @@ static inline void cs_set_iter(CsAlias &a, char *val, CsIdentStack &stack) {
static void cs_loop_list_conc(
CsState &cs, CsValue &res, CsIdent *id, ostd::ConstCharRange list,
CsBytecode const *body, bool space
CsBytecode *body, bool space
) {
if (!id->is_alias()) {
return;