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

View File

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

View File

@ -62,7 +62,7 @@ private:
CsBytecode *p_code; CsBytecode *p_code;
}; };
OSTD_EXPORT bool cs_code_is_empty(CsBytecode const *code); OSTD_EXPORT bool cs_code_is_empty(CsBytecode *code);
struct CsIdent; struct CsIdent;
@ -70,8 +70,8 @@ struct OSTD_EXPORT CsValue {
union { union {
CsInt i; /* ID_IVAR, VAL_INT */ CsInt i; /* ID_IVAR, VAL_INT */
CsFloat f; /* ID_FVAR, VAL_FLOAT */ CsFloat f; /* ID_FVAR, VAL_FLOAT */
CsBytecode const *code; /* VAL_CODE */ CsBytecode *p_code; /* VAL_CODE */
CsIdent *id; /* VAL_IDENT */ CsIdent *p_id; /* VAL_IDENT */
char *s; /* ID_SVAR, VAL_STR */ char *s; /* ID_SVAR, VAL_STR */
char const *cstr; /* VAL_CSTR */ char const *cstr; /* VAL_CSTR */
}; };
@ -83,11 +83,11 @@ struct OSTD_EXPORT CsValue {
void set_float(CsFloat val); void set_float(CsFloat val);
void set_str(CsString val); void set_str(CsString val);
void set_null(); void set_null();
void set_code(CsBytecode const *val); void set_code(CsBytecode *val);
void set_cstr(ostd::ConstCharRange val); void set_cstr(ostd::ConstCharRange val);
void set_mstr(ostd::CharRange val); void set_mstr(ostd::CharRange val);
void set_ident(CsIdent *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); void set(CsValue &tv);
@ -392,27 +392,27 @@ struct OSTD_EXPORT CsState {
ostd::ConstCharRange name, ostd::ConstCharRange args, CmdFunc func 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(ostd::ConstCharRange code);
CsString run_str(CsIdent *id, CsValueRange args); 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(ostd::ConstCharRange code);
CsInt run_int(CsIdent *id, CsValueRange args); 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(ostd::ConstCharRange code);
CsFloat run_float(CsIdent *id, CsValueRange args); 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(ostd::ConstCharRange code);
bool run_bool(CsIdent *id, CsValueRange args); 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(ostd::ConstCharRange code, CsValue &ret);
void run_ret(CsIdent *id, CsValueRange args, 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(ostd::ConstCharRange code);
void run(CsIdent *id, CsValueRange args); 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( static void cs_loop_list_conc(
CsState &cs, CsValue &res, CsIdent *id, ostd::ConstCharRange list, CsState &cs, CsValue &res, CsIdent *id, ostd::ConstCharRange list,
CsBytecode const *body, bool space CsBytecode *body, bool space
) { ) {
if (!id->is_alias()) { if (!id->is_alias()) {
return; return;