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');
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) {
gen_float(parser::parse_float(word));
gen_float(cs_parse_float(word));
}
void GenState::gen_value(int wordtype, ostd::ConstCharRange word) {
@ -928,7 +928,7 @@ noid:
case VAL_ANY:
case VAL_CANY: {
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);
else gs.gen_int(val);
break;

View File

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

View File

@ -4,17 +4,15 @@
#include <ostd/string.hh>
namespace cscript {
namespace parser {
CsInt parse_int(
CsInt cs_parse_int(
ostd::ConstCharRange input, ostd::ConstCharRange *end = nullptr
);
CsFloat parse_float(
CsFloat cs_parse_float(
ostd::ConstCharRange input, ostd::ConstCharRange *end = nullptr
);
} /* namespace parser */
} /* namespace cscript */
#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(""));
case CODE_LOOKUPU|RET_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(CsInt(*id->storage.fp)),
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));
case CODE_LOOKUPU|RET_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(*id->storage.fp),
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);
continue;
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;
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;
case CODE_SVARM:
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_MACRO:
case VAL_CSTR:
rf = parser::parse_float(s);
rf = cs_parse_float(s);
break;
case VAL_FLOAT:
return f;
@ -342,7 +342,7 @@ CsInt TaggedValue::force_int() {
case VAL_STR:
case VAL_MACRO:
case VAL_CSTR:
ri = parser::parse_int(s);
ri = cs_parse_int(s);
break;
case VAL_INT:
return i;
@ -382,7 +382,7 @@ static inline CsInt cs_get_int(IdentValue const &v, int type) {
case VAL_STR:
case VAL_MACRO:
case VAL_CSTR:
return parser::parse_int(v.s);
return cs_parse_int(v.s);
}
return 0;
}
@ -404,7 +404,7 @@ static inline CsFloat cs_get_float(IdentValue const &v, int type) {
case VAL_STR:
case VAL_MACRO:
case VAL_CSTR:
return parser::parse_float(v.s);
return cs_parse_float(v.s);
}
return 0.0f;
}
@ -520,12 +520,12 @@ static inline bool cs_get_bool(ostd::ConstCharRange s) {
return false;
}
ostd::ConstCharRange end = s;
CsInt ival = parser::parse_int(end, &end);
CsInt ival = cs_parse_int(end, &end);
if (end.empty()) {
return !!ival;
}
end = s;
CsFloat fval = parser::parse_float(end, &end);
CsFloat fval = cs_parse_float(end, &end);
if (end.empty()) {
return !!fval;
}

View File

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