master
Daniel Kolesa 2016-08-15 18:55:22 +01:00
parent 661aef0c0f
commit 93f20b1fae
6 changed files with 53 additions and 77 deletions

View File

@ -243,11 +243,11 @@ static void compilestatements(GenState &gs, int rettype, int brak = '\0', int pr
static inline char const *compileblock(GenState &gs, char const *p, int rettype = RET_NULL, int brak = '\0'); static inline char const *compileblock(GenState &gs, char const *p, int rettype = RET_NULL, int brak = '\0');
void GenState::gen_int(ostd::ConstCharRange word) { void GenState::gen_int(ostd::ConstCharRange word) {
gen_int(parser::parse_int(word)); gen_int(cs_parse_int(word));
} }
void GenState::gen_float(ostd::ConstCharRange word) { void GenState::gen_float(ostd::ConstCharRange word) {
gen_float(parser::parse_float(word)); gen_float(cs_parse_float(word));
} }
void GenState::gen_value(int wordtype, ostd::ConstCharRange word) { void GenState::gen_value(int wordtype, ostd::ConstCharRange word) {
@ -928,7 +928,7 @@ noid:
case VAL_ANY: case VAL_ANY:
case VAL_CANY: { case VAL_CANY: {
ostd::ConstCharRange end = idname.get(); ostd::ConstCharRange end = idname.get();
CsInt val = parser::parse_int(end, &end); CsInt val = cs_parse_int(end, &end);
if (!end.empty()) gs.gen_str(idname.get(), rettype == VAL_CANY); if (!end.empty()) gs.gen_str(idname.get(), rettype == VAL_CANY);
else gs.gen_int(val); else gs.gen_int(val);
break; break;

View File

@ -5,11 +5,10 @@
#include <math.h> #include <math.h>
namespace cscript { namespace cscript {
namespace parser {
static inline void p_skip_white(ostd::ConstCharRange &v) { static inline void p_skip_white(ostd::ConstCharRange &v) {
while (!v.empty() && isspace(v.front())) { while (!v.empty() && isspace(v.front())) {
v.pop_front(); ++v;
} }
} }
@ -35,35 +34,15 @@ static inline CsInt p_hexd_to_int(char c) {
return 0; return 0;
} }
static inline CsInt p_decd_to_int(char c) {
return c - '0';
}
static inline CsInt p_bind_to_int(char c) {
return c - '0';
}
static inline bool p_is_hexdigit(char c) {
return isxdigit(c);
}
static inline bool p_is_decdigit(char c) {
return isdigit(c);
}
static inline bool p_is_bindigit(char c) {
return (c == '0') || (c == '1');
}
static inline bool p_check_neg(ostd::ConstCharRange &input) { static inline bool p_check_neg(ostd::ConstCharRange &input) {
bool neg = (input.front() == '-'); bool neg = (*input == '-');
if (neg || (input.front() == '+')) { if (neg || (*input == '+')) {
input.pop_front(); ++input;
} }
return neg; return neg;
} }
CsInt parse_int(ostd::ConstCharRange input, ostd::ConstCharRange *end) { CsInt cs_parse_int(ostd::ConstCharRange input, ostd::ConstCharRange *end) {
ostd::ConstCharRange orig = input; ostd::ConstCharRange orig = input;
p_skip_white(input); p_skip_white(input);
if (input.empty()) { if (input.empty()) {
@ -76,26 +55,26 @@ CsInt parse_int(ostd::ConstCharRange input, ostd::ConstCharRange *end) {
if (input.size() >= 2) { if (input.size() >= 2) {
ostd::ConstCharRange pfx = input.slice(0, 2); ostd::ConstCharRange pfx = input.slice(0, 2);
if ((pfx == "0x") || (pfx == "0X")) { if ((pfx == "0x") || (pfx == "0X")) {
input.pop_front_n(2); input += 2;
past = input; past = input;
while (!past.empty() && p_is_hexdigit(past.front())) { while (!past.empty() && isxdigit(*past)) {
ret = ret * 16 + p_hexd_to_int(past.front()); ret = ret * 16 + p_hexd_to_int(*past);
past.pop_front(); ++past;
} }
goto done; goto done;
} else if ((pfx == "0b") || (pfx == "0B")) { } else if ((pfx == "0b") || (pfx == "0B")) {
input.pop_front_n(2); input += 2;
past = input; past = input;
while (!past.empty() && p_is_bindigit(past.front())) { while (!past.empty() && ((*past == '0') || (*past == '1'))) {
ret = ret * 2 + p_bind_to_int(past.front()); ret = ret * 2 + (*past - '0');
past.pop_front(); ++past;
} }
goto done; goto done;
} }
} }
while (!past.empty() && p_is_decdigit(past.front())) { while (!past.empty() && isdigit(*past)) {
ret = ret * 10 + p_decd_to_int(past.front()); ret = ret * 10 + (*past - '0');
past.pop_front(); ++past;
} }
done: done:
if (past.equals_front(input)) { if (past.equals_front(input)) {
@ -114,21 +93,21 @@ static inline bool p_read_exp(ostd::ConstCharRange &input, CsInt &fn) {
if (input.empty()) { if (input.empty()) {
return true; return true;
} }
if ((input.front() != e1) && (input.front() != e2)) { if ((*input != e1) && (*input != e2)) {
return true; return true;
} }
input.pop_front(); ++input;
if (input.empty()) { if (input.empty()) {
return false; return false;
} }
bool neg = p_check_neg(input); bool neg = p_check_neg(input);
if (input.empty() || !p_is_decdigit(input.front())) { if (input.empty() || !isdigit(*input)) {
return false; return false;
} }
CsInt exp = 0; CsInt exp = 0;
while (!input.empty() && p_is_decdigit(input.front())) { while (!input.empty() && isdigit(*input)) {
exp = exp * 10 + p_decd_to_int(input.front()); exp = exp * 10 + (*input - '0');
input.pop_front(); ++input;
} }
if (neg) { if (neg) {
exp = -exp; exp = -exp;
@ -141,17 +120,17 @@ static inline bool parse_hex_float(
ostd::ConstCharRange input, ostd::ConstCharRange *end, CsFloat &ret ostd::ConstCharRange input, ostd::ConstCharRange *end, CsFloat &ret
) { ) {
auto read_hd = [&input](double r, CsInt &n) { auto read_hd = [&input](double r, CsInt &n) {
while (!input.empty() && p_is_hexdigit(input.front())) { while (!input.empty() && isxdigit(*input)) {
r = r * 16.0 + double(p_hexd_to_int(input.front())); r = r * 16.0 + double(p_hexd_to_int(*input));
++n; ++n;
input.pop_front(); ++input;
} }
return r; return r;
}; };
CsInt wn = 0, fn = 0; CsInt wn = 0, fn = 0;
double r = read_hd(0.0, wn); double r = read_hd(0.0, wn);
if (!input.empty() && (input.front() == '.')) { if (!input.empty() && (*input == '.')) {
input.pop_front(); ++input;
r = read_hd(r, fn); r = read_hd(r, fn);
} }
if (!wn && !fn) { if (!wn && !fn) {
@ -170,17 +149,17 @@ static inline bool parse_dec_float(
ostd::ConstCharRange input, ostd::ConstCharRange *end, CsFloat &ret ostd::ConstCharRange input, ostd::ConstCharRange *end, CsFloat &ret
) { ) {
auto read_hd = [&input](double r, CsInt &n) { auto read_hd = [&input](double r, CsInt &n) {
while (!input.empty() && p_is_decdigit(input.front())) { while (!input.empty() && isdigit(*input)) {
r = r * 10.0 + double(p_decd_to_int(input.front())); r = r * 10.0 + double(*input - '0');
++n; ++n;
input.pop_front(); ++input;
} }
return r; return r;
}; };
CsInt wn = 0, fn = 0; CsInt wn = 0, fn = 0;
double r = read_hd(0.0, wn); double r = read_hd(0.0, wn);
if (!input.empty() && (input.front() == '.')) { if (!input.empty() && (*input == '.')) {
input.pop_front(); ++input;
r = read_hd(r, fn); r = read_hd(r, fn);
} }
if (!wn && !fn) { if (!wn && !fn) {
@ -195,7 +174,7 @@ static inline bool parse_dec_float(
return true; return true;
} }
CsFloat parse_float(ostd::ConstCharRange input, ostd::ConstCharRange *end) { CsFloat cs_parse_float(ostd::ConstCharRange input, ostd::ConstCharRange *end) {
ostd::ConstCharRange orig = input; ostd::ConstCharRange orig = input;
p_skip_white(input); p_skip_white(input);
if (input.empty()) { if (input.empty()) {
@ -207,7 +186,7 @@ CsFloat parse_float(ostd::ConstCharRange input, ostd::ConstCharRange *end) {
if (input.size() >= 2) { if (input.size() >= 2) {
ostd::ConstCharRange pfx = input.slice(0, 2); ostd::ConstCharRange pfx = input.slice(0, 2);
if ((pfx == "0x") || (pfx == "0X")) { if ((pfx == "0x") || (pfx == "0X")) {
input.pop_front_n(2); input += 2;
if (!parse_hex_float(input, end, ret)) { if (!parse_hex_float(input, end, ret)) {
p_set_end(orig, end); p_set_end(orig, end);
return ret; return ret;
@ -226,5 +205,4 @@ done:
return ret; return ret;
} }
} /* namespace parser */
} /* namespace cscript */ } /* namespace cscript */

View File

@ -4,17 +4,15 @@
#include <ostd/string.hh> #include <ostd/string.hh>
namespace cscript { namespace cscript {
namespace parser {
CsInt parse_int( CsInt cs_parse_int(
ostd::ConstCharRange input, ostd::ConstCharRange *end = nullptr ostd::ConstCharRange input, ostd::ConstCharRange *end = nullptr
); );
CsFloat parse_float( CsFloat cs_parse_float(
ostd::ConstCharRange input, ostd::ConstCharRange *end = nullptr ostd::ConstCharRange input, ostd::ConstCharRange *end = nullptr
); );
} /* namespace parser */
} /* namespace cscript */ } /* namespace cscript */
#endif /* LIBCUBESCRIPT_CS_UTIL_HH */ #endif /* LIBCUBESCRIPT_CS_UTIL_HH */

View File

@ -720,7 +720,7 @@ static ostd::Uint32 const *runcode(CsState &cs, ostd::Uint32 const *code, Tagged
LOOKUPARG(args[numargs++].set_str(ostd::move(id->get_str())), args[numargs++].set_str("")); LOOKUPARG(args[numargs++].set_str(ostd::move(id->get_str())), args[numargs++].set_str(""));
case CODE_LOOKUPU|RET_INT: case CODE_LOOKUPU|RET_INT:
LOOKUPU(arg.set_int(id->get_int()), LOOKUPU(arg.set_int(id->get_int()),
arg.set_int(parser::parse_int(*id->storage.sp)), arg.set_int(cs_parse_int(*id->storage.sp)),
arg.set_int(*id->storage.ip), arg.set_int(*id->storage.ip),
arg.set_int(CsInt(*id->storage.fp)), arg.set_int(CsInt(*id->storage.fp)),
arg.set_int(0)); arg.set_int(0));
@ -730,7 +730,7 @@ static ostd::Uint32 const *runcode(CsState &cs, ostd::Uint32 const *code, Tagged
LOOKUPARG(args[numargs++].set_int(id->get_int()), args[numargs++].set_int(0)); LOOKUPARG(args[numargs++].set_int(id->get_int()), args[numargs++].set_int(0));
case CODE_LOOKUPU|RET_FLOAT: case CODE_LOOKUPU|RET_FLOAT:
LOOKUPU(arg.set_float(id->get_float()), LOOKUPU(arg.set_float(id->get_float()),
arg.set_float(parser::parse_float(*id->storage.sp)), arg.set_float(cs_parse_float(*id->storage.sp)),
arg.set_float(CsFloat(*id->storage.ip)), arg.set_float(CsFloat(*id->storage.ip)),
arg.set_float(*id->storage.fp), arg.set_float(*id->storage.fp),
arg.set_float(0.0f)); arg.set_float(0.0f));
@ -775,10 +775,10 @@ static ostd::Uint32 const *runcode(CsState &cs, ostd::Uint32 const *code, Tagged
args[numargs++].set_str(*cs.identmap[op >> 8]->storage.sp); args[numargs++].set_str(*cs.identmap[op >> 8]->storage.sp);
continue; continue;
case CODE_SVAR|RET_INT: case CODE_SVAR|RET_INT:
args[numargs++].set_int(parser::parse_int(*cs.identmap[op >> 8]->storage.sp)); args[numargs++].set_int(cs_parse_int(*cs.identmap[op >> 8]->storage.sp));
continue; continue;
case CODE_SVAR|RET_FLOAT: case CODE_SVAR|RET_FLOAT:
args[numargs++].set_float(parser::parse_float(*cs.identmap[op >> 8]->storage.sp)); args[numargs++].set_float(cs_parse_float(*cs.identmap[op >> 8]->storage.sp));
continue; continue;
case CODE_SVARM: case CODE_SVARM:
args[numargs++].set_cstr(*cs.identmap[op >> 8]->storage.sp); args[numargs++].set_cstr(*cs.identmap[op >> 8]->storage.sp);

View File

@ -323,7 +323,7 @@ CsFloat TaggedValue::force_float() {
case VAL_STR: case VAL_STR:
case VAL_MACRO: case VAL_MACRO:
case VAL_CSTR: case VAL_CSTR:
rf = parser::parse_float(s); rf = cs_parse_float(s);
break; break;
case VAL_FLOAT: case VAL_FLOAT:
return f; return f;
@ -342,7 +342,7 @@ CsInt TaggedValue::force_int() {
case VAL_STR: case VAL_STR:
case VAL_MACRO: case VAL_MACRO:
case VAL_CSTR: case VAL_CSTR:
ri = parser::parse_int(s); ri = cs_parse_int(s);
break; break;
case VAL_INT: case VAL_INT:
return i; return i;
@ -382,7 +382,7 @@ static inline CsInt cs_get_int(IdentValue const &v, int type) {
case VAL_STR: case VAL_STR:
case VAL_MACRO: case VAL_MACRO:
case VAL_CSTR: case VAL_CSTR:
return parser::parse_int(v.s); return cs_parse_int(v.s);
} }
return 0; return 0;
} }
@ -404,7 +404,7 @@ static inline CsFloat cs_get_float(IdentValue const &v, int type) {
case VAL_STR: case VAL_STR:
case VAL_MACRO: case VAL_MACRO:
case VAL_CSTR: case VAL_CSTR:
return parser::parse_float(v.s); return cs_parse_float(v.s);
} }
return 0.0f; return 0.0f;
} }
@ -520,12 +520,12 @@ static inline bool cs_get_bool(ostd::ConstCharRange s) {
return false; return false;
} }
ostd::ConstCharRange end = s; ostd::ConstCharRange end = s;
CsInt ival = parser::parse_int(end, &end); CsInt ival = cs_parse_int(end, &end);
if (end.empty()) { if (end.empty()) {
return !!ival; return !!ival;
} }
end = s; end = s;
CsFloat fval = parser::parse_float(end, &end); CsFloat fval = cs_parse_float(end, &end);
if (end.empty()) { if (end.empty()) {
return !!fval; return !!fval;
} }

View File

@ -180,8 +180,8 @@ found:
res.set_int(-1); \ res.set_int(-1); \
}); });
CS_CMD_LIST_FIND("listfind=", "i", get_int, parser::parse_int(p.item) == val); CS_CMD_LIST_FIND("listfind=", "i", get_int, cs_parse_int(p.item) == val);
CS_CMD_LIST_FIND("listfind=f", "f", get_float, parser::parse_float(p.item) == val); CS_CMD_LIST_FIND("listfind=f", "f", get_float, cs_parse_float(p.item) == val);
CS_CMD_LIST_FIND("listfind=s", "s", get_strr, p.item == val); CS_CMD_LIST_FIND("listfind=s", "s", get_strr, p.item == val);
#undef CS_CMD_LIST_FIND #undef CS_CMD_LIST_FIND
@ -204,8 +204,8 @@ found:
} \ } \
}); });
CS_CMD_LIST_ASSOC("listassoc=", "i", get_int, parser::parse_int(p.item) == val); CS_CMD_LIST_ASSOC("listassoc=", "i", get_int, cs_parse_int(p.item) == val);
CS_CMD_LIST_ASSOC("listassoc=f", "f", get_float, parser::parse_float(p.item) == val); CS_CMD_LIST_ASSOC("listassoc=f", "f", get_float, cs_parse_float(p.item) == val);
CS_CMD_LIST_ASSOC("listassoc=s", "s", get_strr, p.item == val); CS_CMD_LIST_ASSOC("listassoc=s", "s", get_strr, p.item == val);
#undef CS_CMD_LIST_ASSOC #undef CS_CMD_LIST_ASSOC