configuration header with types
parent
52d8c23a4d
commit
17511ab844
12
Makefile
12
Makefile
|
@ -29,9 +29,9 @@ $(LIBCS_LIB): $(LIBCS_OBJ)
|
|||
clean:
|
||||
rm -f $(LIBCS_LIB) $(LIBCS_OBJ)
|
||||
|
||||
cubescript.o: cubescript.hh cs_vm.hh
|
||||
cs_gen.o: cubescript.hh cs_vm.hh
|
||||
cs_vm.o: cubescript.hh cs_vm.hh
|
||||
lib_str.o: cubescript.hh
|
||||
lib_math.o: cubescript.hh
|
||||
lib_list.o: cubescript.hh
|
||||
cubescript.o: cubescript.hh cubescript_conf.hh cs_vm.hh
|
||||
cs_gen.o: cubescript.hh cubescript_conf.hh cs_vm.hh
|
||||
cs_vm.o: cubescript.hh cubescript_conf.hh cs_vm.hh
|
||||
lib_str.o: cubescript.hh cubescript_conf.hh
|
||||
lib_math.o: cubescript.hh cubescript_conf.hh
|
||||
lib_list.o: cubescript.hh cubescript_conf.hh
|
||||
|
|
10
cs_gen.cc
10
cs_gen.cc
|
@ -10,12 +10,12 @@ namespace cscript {
|
|||
|
||||
char *cs_dup_ostr(ostd::ConstCharRange s);
|
||||
|
||||
int cs_parse_int(ostd::ConstCharRange s) {
|
||||
CsInt cs_parse_int(ostd::ConstCharRange s) {
|
||||
if (s.empty()) return 0;
|
||||
return parseint(s.data());
|
||||
}
|
||||
|
||||
float cs_parse_float(ostd::ConstCharRange s) {
|
||||
CsFloat cs_parse_float(ostd::ConstCharRange s) {
|
||||
if (s.empty()) return 0.0f;
|
||||
return parsefloat(s.data());
|
||||
}
|
||||
|
@ -309,7 +309,7 @@ lookupid:
|
|||
numargs++;
|
||||
break;
|
||||
case 'b':
|
||||
gs.gen_int(INT_MIN);
|
||||
gs.gen_int(CsIntMin);
|
||||
numargs++;
|
||||
break;
|
||||
case 'f':
|
||||
|
@ -803,7 +803,7 @@ noid:
|
|||
case VAL_CANY: {
|
||||
char *end = idname.get();
|
||||
ostd::Size idlen = strlen(idname.get());
|
||||
int val = int(strtoul(idname.get(), &end, 0));
|
||||
CsInt val = CsInt(strtoul(idname.get(), &end, 0));
|
||||
if (end < &idname[idlen]) gs.gen_str(idname.get(), rettype == VAL_CANY);
|
||||
else gs.gen_int(val);
|
||||
break;
|
||||
|
@ -849,7 +849,7 @@ noid:
|
|||
if (more) more = compilearg(gs, VAL_INT, prevargs + numargs);
|
||||
if (!more) {
|
||||
if (rep) break;
|
||||
gs.gen_int(INT_MIN);
|
||||
gs.gen_int(CsIntMin);
|
||||
fakeargs++;
|
||||
}
|
||||
numargs++;
|
||||
|
|
46
cs_vm.cc
46
cs_vm.cc
|
@ -63,7 +63,7 @@ static void bcode_ref(ostd::Uint32 *code) {
|
|||
bcode_incr(&code[-1]);
|
||||
break;
|
||||
case CODE_OFFSET:
|
||||
code -= int(code[-1] >> 8);
|
||||
code -= ostd::Ptrdiff(code[-1] >> 8);
|
||||
bcode_incr(code);
|
||||
break;
|
||||
}
|
||||
|
@ -81,7 +81,7 @@ static void bcode_unref(ostd::Uint32 *code) {
|
|||
bcode_decr(&code[-1]);
|
||||
break;
|
||||
case CODE_OFFSET:
|
||||
code -= int(code[-1] >> 8);
|
||||
code -= ostd::Ptrdiff(code[-1] >> 8);
|
||||
bcode_decr(code);
|
||||
break;
|
||||
}
|
||||
|
@ -243,7 +243,7 @@ static inline void callcommand(CsState &cs, Ident *id, TaggedValue *args, Tagged
|
|||
case 'b':
|
||||
if (++i >= numargs) {
|
||||
if (rep) break;
|
||||
args[i].set_int(INT_MIN);
|
||||
args[i].set_int(CsIntMin);
|
||||
fakeargs++;
|
||||
} else args[i].force_int();
|
||||
break;
|
||||
|
@ -310,7 +310,7 @@ static inline void callcommand(CsState &cs, Ident *id, TaggedValue *args, Tagged
|
|||
break;
|
||||
case 'N':
|
||||
if (++i < numargs) args[i].cleanup();
|
||||
args[i].set_int(lookup ? -1 : i - fakeargs);
|
||||
args[i].set_int(CsInt(lookup ? -1 : i - fakeargs));
|
||||
break;
|
||||
case 'C': {
|
||||
i = ostd::max(i + 1, numargs);
|
||||
|
@ -517,16 +517,16 @@ static ostd::Uint32 const *runcode(CsState &cs, ostd::Uint32 const *code, Tagged
|
|||
args[numargs++].set_null();
|
||||
continue;
|
||||
case CODE_VAL|RET_INT:
|
||||
args[numargs++].set_int(int(*code++));
|
||||
args[numargs++].set_int(CsInt(*code++));
|
||||
continue;
|
||||
case CODE_VALI|RET_INT:
|
||||
args[numargs++].set_int(int(op) >> 8);
|
||||
args[numargs++].set_int(CsInt(op) >> 8);
|
||||
continue;
|
||||
case CODE_VAL|RET_FLOAT:
|
||||
args[numargs++].set_float(*reinterpret_cast<float const *>(code++));
|
||||
args[numargs++].set_float(*reinterpret_cast<CsFloat const *>(code++));
|
||||
continue;
|
||||
case CODE_VALI|RET_FLOAT:
|
||||
args[numargs++].set_float(float(int(op) >> 8));
|
||||
args[numargs++].set_float(CsFloat(CsInt(op) >> 8));
|
||||
continue;
|
||||
|
||||
case CODE_DUP|RET_NULL:
|
||||
|
@ -721,7 +721,7 @@ static ostd::Uint32 const *runcode(CsState &cs, ostd::Uint32 const *code, Tagged
|
|||
LOOKUPU(arg.set_int(id->get_int()),
|
||||
arg.set_int(parseint(*id->storage.sp)),
|
||||
arg.set_int(*id->storage.ip),
|
||||
arg.set_int(int(*id->storage.fp)),
|
||||
arg.set_int(CsInt(*id->storage.fp)),
|
||||
arg.set_int(0));
|
||||
case CODE_LOOKUP|RET_INT:
|
||||
LOOKUP(args[numargs++].set_int(id->get_int()));
|
||||
|
@ -730,7 +730,7 @@ static ostd::Uint32 const *runcode(CsState &cs, ostd::Uint32 const *code, Tagged
|
|||
case CODE_LOOKUPU|RET_FLOAT:
|
||||
LOOKUPU(arg.set_float(id->get_float()),
|
||||
arg.set_float(parsefloat(*id->storage.sp)),
|
||||
arg.set_float(float(*id->storage.ip)),
|
||||
arg.set_float(CsFloat(*id->storage.ip)),
|
||||
arg.set_float(*id->storage.fp),
|
||||
arg.set_float(0.0f));
|
||||
case CODE_LOOKUP|RET_FLOAT:
|
||||
|
@ -795,7 +795,7 @@ static ostd::Uint32 const *runcode(CsState &cs, ostd::Uint32 const *code, Tagged
|
|||
args[numargs++].set_str(ostd::move(intstr(*cs.identmap[op >> 8]->storage.ip)));
|
||||
continue;
|
||||
case CODE_IVAR|RET_FLOAT:
|
||||
args[numargs++].set_float(float(*cs.identmap[op >> 8]->storage.ip));
|
||||
args[numargs++].set_float(CsFloat(*cs.identmap[op >> 8]->storage.ip));
|
||||
continue;
|
||||
case CODE_IVAR1:
|
||||
cs.set_var_int_checked(cs.identmap[op >> 8], args[--numargs].i);
|
||||
|
@ -1126,50 +1126,50 @@ ostd::String CsState::run_str(Ident *id, TvalRange args) {
|
|||
return s;
|
||||
}
|
||||
|
||||
int CsState::run_int(Bytecode const *code) {
|
||||
CsInt CsState::run_int(Bytecode const *code) {
|
||||
TaggedValue ret;
|
||||
run_ret(code, ret);
|
||||
int i = ret.get_int();
|
||||
CsInt i = ret.get_int();
|
||||
ret.cleanup();
|
||||
return i;
|
||||
}
|
||||
|
||||
int CsState::run_int(ostd::ConstCharRange code) {
|
||||
CsInt CsState::run_int(ostd::ConstCharRange code) {
|
||||
TaggedValue ret;
|
||||
run_ret(code, ret);
|
||||
int i = ret.get_int();
|
||||
CsInt i = ret.get_int();
|
||||
ret.cleanup();
|
||||
return i;
|
||||
}
|
||||
|
||||
int CsState::run_int(Ident *id, TvalRange args) {
|
||||
CsInt CsState::run_int(Ident *id, TvalRange args) {
|
||||
TaggedValue ret;
|
||||
run_ret(id, args, ret);
|
||||
int i = ret.get_int();
|
||||
CsInt i = ret.get_int();
|
||||
ret.cleanup();
|
||||
return i;
|
||||
}
|
||||
|
||||
float CsState::run_float(Bytecode const *code) {
|
||||
CsFloat CsState::run_float(Bytecode const *code) {
|
||||
TaggedValue ret;
|
||||
run_ret(code, ret);
|
||||
float f = ret.get_float();
|
||||
CsFloat f = ret.get_float();
|
||||
ret.cleanup();
|
||||
return f;
|
||||
}
|
||||
|
||||
float CsState::run_float(ostd::ConstCharRange code) {
|
||||
CsFloat CsState::run_float(ostd::ConstCharRange code) {
|
||||
TaggedValue ret;
|
||||
run_ret(code, ret);
|
||||
float f = ret.get_float();
|
||||
CsFloat f = ret.get_float();
|
||||
ret.cleanup();
|
||||
return f;
|
||||
}
|
||||
|
||||
float CsState::run_float(Ident *id, TvalRange args) {
|
||||
CsFloat CsState::run_float(Ident *id, TvalRange args) {
|
||||
TaggedValue ret;
|
||||
run_ret(id, args, ret);
|
||||
float f = ret.get_float();
|
||||
CsFloat f = ret.get_float();
|
||||
ret.cleanup();
|
||||
return f;
|
||||
}
|
||||
|
|
16
cs_vm.hh
16
cs_vm.hh
|
@ -148,7 +148,7 @@ struct GenState {
|
|||
code.push(CODE_VALI | RET_NULL);
|
||||
}
|
||||
|
||||
void gen_int(int i = 0) {
|
||||
void gen_int(CsInt i = 0) {
|
||||
if (i >= -0x800000 && i <= 0x7FFFFF)
|
||||
code.push(CODE_VALI | RET_INT | (i << 8));
|
||||
else {
|
||||
|
@ -159,12 +159,12 @@ struct GenState {
|
|||
|
||||
void gen_int(ostd::ConstCharRange word);
|
||||
|
||||
void gen_float(float f = 0.0f) {
|
||||
if (int(f) == f && f >= -0x800000 && f <= 0x7FFFFF)
|
||||
code.push(CODE_VALI | RET_FLOAT | (int(f) << 8));
|
||||
void gen_float(CsFloat f = 0.0f) {
|
||||
if (CsInt(f) == f && f >= -0x800000 && f <= 0x7FFFFF)
|
||||
code.push(CODE_VALI | RET_FLOAT | (CsInt(f) << 8));
|
||||
else {
|
||||
union {
|
||||
float f;
|
||||
CsFloat f;
|
||||
ostd::Uint32 u;
|
||||
} c;
|
||||
c.f = f;
|
||||
|
@ -203,11 +203,11 @@ struct GenState {
|
|||
}
|
||||
};
|
||||
|
||||
int parseint(char const *s);
|
||||
float parsefloat(char const *s);
|
||||
CsInt parseint(char const *s);
|
||||
CsFloat parsefloat(char const *s);
|
||||
|
||||
ostd::String intstr(int v);
|
||||
ostd::String floatstr(float v);
|
||||
ostd::String floatstr(CsFloat v);
|
||||
|
||||
bool cs_check_num(ostd::ConstCharRange s);
|
||||
|
||||
|
|
|
@ -3,28 +3,28 @@
|
|||
|
||||
namespace cscript {
|
||||
|
||||
int parseint(char const *s) {
|
||||
return int(strtoul(s, nullptr, 0));
|
||||
CsInt parseint(char const *s) {
|
||||
return CsInt(strtoul(s, nullptr, 0));
|
||||
}
|
||||
|
||||
float parsefloat(char const *s) {
|
||||
CsFloat parsefloat(char const *s) {
|
||||
/* not all platforms (windows) can parse hexadecimal integers via strtod */
|
||||
char *end;
|
||||
double val = strtod(s, &end);
|
||||
return val || end==s || (*end!='x' && *end!='X') ? float(val) : float(parseint(s));
|
||||
return val || end==s || (*end!='x' && *end!='X') ? CsFloat(val) : CsFloat(parseint(s));
|
||||
}
|
||||
|
||||
float cs_parse_float(ostd::ConstCharRange s);
|
||||
CsFloat cs_parse_float(ostd::ConstCharRange s);
|
||||
|
||||
ostd::String intstr(int v) {
|
||||
ostd::String intstr(CsInt v) {
|
||||
char buf[256];
|
||||
snprintf(buf, sizeof(buf), "%d", v);
|
||||
snprintf(buf, sizeof(buf), IntFormat, v);
|
||||
return buf;
|
||||
}
|
||||
|
||||
ostd::String floatstr(float v) {
|
||||
ostd::String floatstr(CsFloat v) {
|
||||
char buf[256];
|
||||
snprintf(buf, sizeof(buf), v == int(v) ? "%.1f" : "%.7g", v);
|
||||
snprintf(buf, sizeof(buf), v == CsInt(v) ? RoundFloatFormat : FloatFormat, v);
|
||||
return buf;
|
||||
}
|
||||
|
||||
|
@ -52,7 +52,7 @@ bool cs_check_num(ostd::ConstCharRange s) {
|
|||
Ident::Ident(): type(ID_UNKNOWN) {}
|
||||
|
||||
/* ID_IVAR */
|
||||
Ident::Ident(ostd::ConstCharRange n, int m, int x, int *s,
|
||||
Ident::Ident(ostd::ConstCharRange n, CsInt m, CsInt x, CsInt *s,
|
||||
VarCb f, int flagsv)
|
||||
: type(ID_IVAR), flags(flagsv | (m > x ? IDF_READONLY : 0)), name(n),
|
||||
minval(m), maxval(x), cb_var(ostd::move(f)) {
|
||||
|
@ -60,7 +60,7 @@ Ident::Ident(ostd::ConstCharRange n, int m, int x, int *s,
|
|||
}
|
||||
|
||||
/* ID_FVAR */
|
||||
Ident::Ident(ostd::ConstCharRange n, float m, float x, float *s,
|
||||
Ident::Ident(ostd::ConstCharRange n, CsFloat m, CsFloat x, CsFloat *s,
|
||||
VarCb f, int flagsv)
|
||||
: type(ID_FVAR), flags(flagsv | (m > x ? IDF_READONLY : 0)), name(n),
|
||||
minvalf(m), maxvalf(x), cb_var(ostd::move(f)) {
|
||||
|
@ -80,12 +80,12 @@ Ident::Ident(ostd::ConstCharRange n, char *a, int flagsv)
|
|||
val.s = a;
|
||||
val.len = strlen(a);
|
||||
}
|
||||
Ident::Ident(ostd::ConstCharRange n, int a, int flagsv)
|
||||
Ident::Ident(ostd::ConstCharRange n, CsInt a, int flagsv)
|
||||
: type(ID_ALIAS), valtype(VAL_INT), flags(flagsv), name(n), code(nullptr),
|
||||
stack(nullptr) {
|
||||
val.i = a;
|
||||
}
|
||||
Ident::Ident(ostd::ConstCharRange n, float a, int flagsv)
|
||||
Ident::Ident(ostd::ConstCharRange n, CsFloat a, int flagsv)
|
||||
: type(ID_ALIAS), valtype(VAL_FLOAT), flags(flagsv), name(n), code(nullptr),
|
||||
stack(nullptr) {
|
||||
val.f = a;
|
||||
|
@ -263,7 +263,7 @@ void CsState::set_alias(ostd::ConstCharRange name, TaggedValue &v) {
|
|||
}
|
||||
}
|
||||
|
||||
void CsState::print_var_int(Ident *id, int i) {
|
||||
void CsState::print_var_int(Ident *id, CsInt i) {
|
||||
if (i < 0) {
|
||||
writefln("%s = %d", id->name, i);
|
||||
return;
|
||||
|
@ -279,7 +279,7 @@ void CsState::print_var_int(Ident *id, int i) {
|
|||
writefln("%s = %d", id->name, i);
|
||||
}
|
||||
|
||||
void CsState::print_var_float(Ident *id, float f) {
|
||||
void CsState::print_var_float(Ident *id, CsFloat f) {
|
||||
writefln("%s = %s", id->name, floatstr(f));
|
||||
}
|
||||
|
||||
|
@ -326,8 +326,8 @@ void TaggedValue::force_null() {
|
|||
set_null();
|
||||
}
|
||||
|
||||
float TaggedValue::force_float() {
|
||||
float rf = 0.0f;
|
||||
CsFloat TaggedValue::force_float() {
|
||||
CsFloat rf = 0.0f;
|
||||
switch (get_type()) {
|
||||
case VAL_INT:
|
||||
rf = i;
|
||||
|
@ -345,8 +345,8 @@ float TaggedValue::force_float() {
|
|||
return rf;
|
||||
}
|
||||
|
||||
int TaggedValue::force_int() {
|
||||
int ri = 0;
|
||||
CsInt TaggedValue::force_int() {
|
||||
CsInt ri = 0;
|
||||
switch (get_type()) {
|
||||
case VAL_FLOAT:
|
||||
ri = f;
|
||||
|
@ -385,10 +385,10 @@ ostd::ConstCharRange TaggedValue::force_str() {
|
|||
return s;
|
||||
}
|
||||
|
||||
static inline int cs_get_int(IdentValue const &v, int type) {
|
||||
static inline CsInt cs_get_int(IdentValue const &v, int type) {
|
||||
switch (type) {
|
||||
case VAL_FLOAT:
|
||||
return int(v.f);
|
||||
return CsInt(v.f);
|
||||
case VAL_INT:
|
||||
return v.i;
|
||||
case VAL_STR:
|
||||
|
@ -399,20 +399,20 @@ static inline int cs_get_int(IdentValue const &v, int type) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
int TaggedValue::get_int() const {
|
||||
CsInt TaggedValue::get_int() const {
|
||||
return cs_get_int(*this, get_type());
|
||||
}
|
||||
|
||||
int Ident::get_int() const {
|
||||
CsInt Ident::get_int() const {
|
||||
return cs_get_int(val, get_valtype());
|
||||
}
|
||||
|
||||
static inline float cs_get_float(IdentValue const &v, int type) {
|
||||
static inline CsFloat cs_get_float(IdentValue const &v, int type) {
|
||||
switch (type) {
|
||||
case VAL_FLOAT:
|
||||
return v.f;
|
||||
case VAL_INT:
|
||||
return float(v.i);
|
||||
return CsFloat(v.i);
|
||||
case VAL_STR:
|
||||
case VAL_MACRO:
|
||||
case VAL_CSTR:
|
||||
|
@ -421,11 +421,11 @@ static inline float cs_get_float(IdentValue const &v, int type) {
|
|||
return 0.0f;
|
||||
}
|
||||
|
||||
float TaggedValue::get_float() const {
|
||||
CsFloat TaggedValue::get_float() const {
|
||||
return cs_get_float(*this, get_type());
|
||||
}
|
||||
|
||||
float Ident::get_float() const {
|
||||
CsFloat Ident::get_float() const {
|
||||
return cs_get_float(val, get_valtype());
|
||||
}
|
||||
|
||||
|
@ -722,7 +722,7 @@ bool cs_override_var(CsState &cs, Ident *id, SF sf, RF rf, CF cf) {
|
|||
return true;
|
||||
}
|
||||
|
||||
void CsState::set_var_int(ostd::ConstCharRange name, int v,
|
||||
void CsState::set_var_int(ostd::ConstCharRange name, CsInt v,
|
||||
bool dofunc, bool doclamp) {
|
||||
Ident *id = idents.at(name);
|
||||
if (!id || id->type != ID_IVAR)
|
||||
|
@ -740,7 +740,7 @@ void CsState::set_var_int(ostd::ConstCharRange name, int v,
|
|||
id->changed();
|
||||
}
|
||||
|
||||
void CsState::set_var_float(ostd::ConstCharRange name, float v,
|
||||
void CsState::set_var_float(ostd::ConstCharRange name, CsFloat v,
|
||||
bool dofunc, bool doclamp) {
|
||||
Ident *id = idents.at(name);
|
||||
if (!id || id->type != ID_FVAR)
|
||||
|
@ -774,14 +774,14 @@ void CsState::set_var_str(ostd::ConstCharRange name, ostd::ConstCharRange v,
|
|||
id->changed();
|
||||
}
|
||||
|
||||
ostd::Maybe<int> CsState::get_var_int(ostd::ConstCharRange name) {
|
||||
ostd::Maybe<CsInt> CsState::get_var_int(ostd::ConstCharRange name) {
|
||||
Ident *id = idents.at(name);
|
||||
if (!id || id->type != ID_IVAR)
|
||||
return ostd::nothing;
|
||||
return *id->storage.ip;
|
||||
}
|
||||
|
||||
ostd::Maybe<float> CsState::get_var_float(ostd::ConstCharRange name) {
|
||||
ostd::Maybe<CsFloat> CsState::get_var_float(ostd::ConstCharRange name) {
|
||||
Ident *id = idents.at(name);
|
||||
if (!id || id->type != ID_FVAR)
|
||||
return ostd::nothing;
|
||||
|
@ -795,28 +795,28 @@ ostd::Maybe<ostd::String> CsState::get_var_str(ostd::ConstCharRange name) {
|
|||
return ostd::String(*id->storage.sp);
|
||||
}
|
||||
|
||||
ostd::Maybe<int> CsState::get_var_min_int(ostd::ConstCharRange name) {
|
||||
ostd::Maybe<CsInt> CsState::get_var_min_int(ostd::ConstCharRange name) {
|
||||
Ident *id = idents.at(name);
|
||||
if (!id || id->type != ID_IVAR)
|
||||
return ostd::nothing;
|
||||
return id->minval;
|
||||
}
|
||||
|
||||
ostd::Maybe<int> CsState::get_var_max_int(ostd::ConstCharRange name) {
|
||||
ostd::Maybe<CsInt> CsState::get_var_max_int(ostd::ConstCharRange name) {
|
||||
Ident *id = idents.at(name);
|
||||
if (!id || id->type != ID_IVAR)
|
||||
return ostd::nothing;
|
||||
return id->maxval;
|
||||
}
|
||||
|
||||
ostd::Maybe<float> CsState::get_var_min_float(ostd::ConstCharRange name) {
|
||||
ostd::Maybe<CsFloat> CsState::get_var_min_float(ostd::ConstCharRange name) {
|
||||
Ident *id = idents.at(name);
|
||||
if (!id || id->type != ID_FVAR)
|
||||
return ostd::nothing;
|
||||
return id->minvalf;
|
||||
}
|
||||
|
||||
ostd::Maybe<float> CsState::get_var_max_float(ostd::ConstCharRange name) {
|
||||
ostd::Maybe<CsFloat> CsState::get_var_max_float(ostd::ConstCharRange name) {
|
||||
Ident *id = idents.at(name);
|
||||
if (!id || id->type != ID_FVAR)
|
||||
return ostd::nothing;
|
||||
|
@ -833,7 +833,7 @@ CsState::get_alias(ostd::ConstCharRange name) {
|
|||
return ostd::move(id->get_str());
|
||||
}
|
||||
|
||||
int cs_clamp_var(CsState &cs, Ident *id, int v) {
|
||||
CsInt cs_clamp_var(CsState &cs, Ident *id, CsInt v) {
|
||||
if (v < id->minval)
|
||||
v = id->minval;
|
||||
else if (v > id->maxval)
|
||||
|
@ -849,7 +849,7 @@ int cs_clamp_var(CsState &cs, Ident *id, int v) {
|
|||
return v;
|
||||
}
|
||||
|
||||
void CsState::set_var_int_checked(Ident *id, int v) {
|
||||
void CsState::set_var_int_checked(Ident *id, CsInt v) {
|
||||
if (id->flags & IDF_READONLY) {
|
||||
cs_debug_code(*this, "variable '%s' is read only", id->name);
|
||||
return;
|
||||
|
@ -866,7 +866,7 @@ void CsState::set_var_int_checked(Ident *id, int v) {
|
|||
}
|
||||
|
||||
void CsState::set_var_int_checked(Ident *id, TvalRange args) {
|
||||
int v = args[0].force_int();
|
||||
CsInt v = args[0].force_int();
|
||||
if ((id->flags & IDF_HEX) && (args.size() > 1)) {
|
||||
v = (v << 16) | (args[1].force_int() << 8);
|
||||
if (args.size() > 2)
|
||||
|
@ -875,7 +875,7 @@ void CsState::set_var_int_checked(Ident *id, TvalRange args) {
|
|||
set_var_int_checked(id, v);
|
||||
}
|
||||
|
||||
float cs_clamp_fvar(CsState &cs, Ident *id, float v) {
|
||||
CsFloat cs_clamp_fvar(CsState &cs, Ident *id, CsFloat v) {
|
||||
if (v < id->minvalf)
|
||||
v = id->minvalf;
|
||||
else if (v > id->maxvalf)
|
||||
|
@ -887,7 +887,7 @@ float cs_clamp_fvar(CsState &cs, Ident *id, float v) {
|
|||
return v;
|
||||
}
|
||||
|
||||
void CsState::set_var_float_checked(Ident *id, float v) {
|
||||
void CsState::set_var_float_checked(Ident *id, CsFloat v) {
|
||||
if (id->flags & IDF_READONLY) {
|
||||
cs_debug_code(*this, "variable '%s' is read only", id->name);
|
||||
return;
|
||||
|
@ -990,7 +990,7 @@ void cs_init_lib_io(CsState &cs) {
|
|||
});
|
||||
}
|
||||
|
||||
static inline void cs_set_iter(Ident &id, int i, IdentStack &stack) {
|
||||
static inline void cs_set_iter(Ident &id, CsInt i, IdentStack &stack) {
|
||||
if (id.stack == &stack) {
|
||||
if (id.get_valtype() != VAL_INT) {
|
||||
if (id.get_valtype() == VAL_STR) {
|
||||
|
@ -1009,12 +1009,12 @@ static inline void cs_set_iter(Ident &id, int i, IdentStack &stack) {
|
|||
id.push_arg(v, stack);
|
||||
}
|
||||
|
||||
static inline void cs_do_loop(CsState &cs, Ident &id, int offset, int n,
|
||||
int step, Bytecode *cond, Bytecode *body) {
|
||||
static inline void cs_do_loop(CsState &cs, Ident &id, CsInt offset, CsInt n,
|
||||
CsInt step, Bytecode *cond, Bytecode *body) {
|
||||
if (n <= 0 || (id.type != ID_ALIAS))
|
||||
return;
|
||||
IdentStack stack;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
for (CsInt i = 0; i < n; ++i) {
|
||||
cs_set_iter(id, offset + i * step, stack);
|
||||
if (cond && !cs.run_bool(cond)) break;
|
||||
cs.run_int(body);
|
||||
|
@ -1023,14 +1023,14 @@ static inline void cs_do_loop(CsState &cs, Ident &id, int offset, int n,
|
|||
}
|
||||
|
||||
static inline void cs_loop_conc(
|
||||
CsState &cs, TaggedValue &res, Ident &id, int offset, int n,
|
||||
int step, Bytecode *body, bool space
|
||||
CsState &cs, TaggedValue &res, Ident &id, CsInt offset, CsInt n,
|
||||
CsInt step, Bytecode *body, bool space
|
||||
) {
|
||||
if (n <= 0 || id.type != ID_ALIAS)
|
||||
return;
|
||||
IdentStack stack;
|
||||
ostd::Vector<char> s;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
for (CsInt i = 0; i < n; ++i) {
|
||||
cs_set_iter(id, offset + i * step, stack);
|
||||
TaggedValue v;
|
||||
cs.run_ret(body, v);
|
||||
|
@ -1127,11 +1127,11 @@ void cs_init_lib_base(CsState &cs) {
|
|||
} \
|
||||
});
|
||||
|
||||
CS_CMD_CASE("case", "i", int, args[0].get_int(),
|
||||
CS_CMD_CASE("case", "i", CsInt, args[0].get_int(),
|
||||
((args[i].get_type() == VAL_NULL) ||
|
||||
(args[i].get_int() == val)));
|
||||
|
||||
CS_CMD_CASE("casef", "f", float, args[0].get_float(),
|
||||
CS_CMD_CASE("casef", "f", CsFloat, args[0].get_float(),
|
||||
((args[i].get_type() == VAL_NULL) ||
|
||||
(args[i].get_float() == val)));
|
||||
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
#ifndef CUBESCRIPT_HH
|
||||
#define CUBESCRIPT_HH
|
||||
#ifndef LIBCUBESCRIPT_CUBESCRIPT_HH
|
||||
#define LIBCUBESCRIPT_CUBESCRIPT_HH
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "cubescript_conf.hh"
|
||||
|
||||
#include <ostd/platform.hh>
|
||||
#include <ostd/types.hh>
|
||||
#include <ostd/string.hh>
|
||||
|
@ -66,8 +68,8 @@ struct Ident;
|
|||
|
||||
struct IdentValue {
|
||||
union {
|
||||
int i; /* ID_IVAR, VAL_INT */
|
||||
float f; /* ID_FVAR, VAL_FLOAT */
|
||||
CsInt i; /* ID_IVAR, VAL_INT */
|
||||
CsFloat f; /* ID_FVAR, VAL_FLOAT */
|
||||
Bytecode const *code; /* VAL_CODE */
|
||||
Ident *id; /* VAL_IDENT */
|
||||
char *s; /* ID_SVAR, VAL_STR */
|
||||
|
@ -83,11 +85,11 @@ struct OSTD_EXPORT TaggedValue: IdentValue {
|
|||
return p_type;
|
||||
}
|
||||
|
||||
void set_int(int val) {
|
||||
void set_int(CsInt val) {
|
||||
p_type = VAL_INT;
|
||||
i = val;
|
||||
}
|
||||
void set_float(float val) {
|
||||
void set_float(CsFloat val) {
|
||||
p_type = VAL_FLOAT;
|
||||
f = val;
|
||||
}
|
||||
|
@ -131,8 +133,8 @@ struct OSTD_EXPORT TaggedValue: IdentValue {
|
|||
|
||||
ostd::String get_str() const;
|
||||
ostd::ConstCharRange get_strr() const;
|
||||
int get_int() const;
|
||||
float get_float() const;
|
||||
CsInt get_int() const;
|
||||
CsFloat get_float() const;
|
||||
Bytecode *get_code() const;
|
||||
Ident *get_ident() const;
|
||||
void get_val(TaggedValue &r) const;
|
||||
|
@ -140,8 +142,8 @@ struct OSTD_EXPORT TaggedValue: IdentValue {
|
|||
bool get_bool() const;
|
||||
|
||||
void force_null();
|
||||
float force_float();
|
||||
int force_int();
|
||||
CsFloat force_float();
|
||||
CsInt force_int();
|
||||
ostd::ConstCharRange force_str();
|
||||
|
||||
bool code_is_empty() const;
|
||||
|
@ -162,8 +164,8 @@ struct IdentStack {
|
|||
};
|
||||
|
||||
union IdentValuePtr {
|
||||
int *ip; /* ID_IVAR */
|
||||
float *fp; /* ID_FVAR */
|
||||
CsInt *ip; /* ID_IVAR */
|
||||
CsFloat *fp; /* ID_FVAR */
|
||||
char **sp; /* ID_SVAR */
|
||||
};
|
||||
|
||||
|
@ -190,10 +192,10 @@ struct OSTD_EXPORT Ident {
|
|||
struct { /* ID_IVAR, ID_FVAR, ID_SVAR */
|
||||
union {
|
||||
struct { /* ID_IVAR */
|
||||
int minval, maxval;
|
||||
CsInt minval, maxval;
|
||||
};
|
||||
struct { /* ID_FVAR */
|
||||
float minvalf, maxvalf;
|
||||
CsFloat minvalf, maxvalf;
|
||||
};
|
||||
};
|
||||
IdentValuePtr storage;
|
||||
|
@ -216,13 +218,13 @@ struct OSTD_EXPORT Ident {
|
|||
|
||||
/* ID_IVAR */
|
||||
Ident(
|
||||
ostd::ConstCharRange n, int m, int x, int *s,
|
||||
ostd::ConstCharRange n, CsInt m, CsInt x, CsInt *s,
|
||||
VarCb f = VarCb(), int flags = 0
|
||||
);
|
||||
|
||||
/* ID_FVAR */
|
||||
Ident(
|
||||
ostd::ConstCharRange n, float m, float x, float *s,
|
||||
ostd::ConstCharRange n, CsFloat m, CsFloat x, CsFloat *s,
|
||||
VarCb f = VarCb(), int flags = 0
|
||||
);
|
||||
|
||||
|
@ -234,8 +236,8 @@ struct OSTD_EXPORT Ident {
|
|||
|
||||
/* ID_ALIAS */
|
||||
Ident(ostd::ConstCharRange n, char *a, int flags);
|
||||
Ident(ostd::ConstCharRange n, int a, int flags);
|
||||
Ident(ostd::ConstCharRange n, float a, int flags);
|
||||
Ident(ostd::ConstCharRange n, CsInt a, int flags);
|
||||
Ident(ostd::ConstCharRange n, CsFloat a, int flags);
|
||||
Ident(ostd::ConstCharRange n, int flags);
|
||||
Ident(ostd::ConstCharRange n, TaggedValue const &v, int flags);
|
||||
|
||||
|
@ -270,8 +272,8 @@ struct OSTD_EXPORT Ident {
|
|||
valtype = VAL_NULL;
|
||||
}
|
||||
|
||||
float get_float() const;
|
||||
int get_int() const;
|
||||
CsFloat get_float() const;
|
||||
CsInt get_int() const;
|
||||
ostd::String get_str() const;
|
||||
ostd::ConstCharRange get_strr() const;
|
||||
void get_val(TaggedValue &r) const;
|
||||
|
@ -348,8 +350,8 @@ struct OSTD_EXPORT CsState {
|
|||
|
||||
int identflags = 0;
|
||||
int nodebug = 0;
|
||||
int numargs = 0;
|
||||
int dbgalias = 4;
|
||||
CsInt numargs = 0;
|
||||
CsInt dbgalias = 4;
|
||||
|
||||
CsState();
|
||||
~CsState();
|
||||
|
@ -386,13 +388,13 @@ struct OSTD_EXPORT CsState {
|
|||
ostd::String run_str(ostd::ConstCharRange code);
|
||||
ostd::String run_str(Ident *id, TvalRange args);
|
||||
|
||||
int run_int(Bytecode const *code);
|
||||
int run_int(ostd::ConstCharRange code);
|
||||
int run_int(Ident *id, TvalRange args);
|
||||
CsInt run_int(Bytecode const *code);
|
||||
CsInt run_int(ostd::ConstCharRange code);
|
||||
CsInt run_int(Ident *id, TvalRange args);
|
||||
|
||||
float run_float(Bytecode const *code);
|
||||
float run_float(ostd::ConstCharRange code);
|
||||
float run_float(Ident *id, TvalRange args);
|
||||
CsFloat run_float(Bytecode const *code);
|
||||
CsFloat run_float(ostd::ConstCharRange code);
|
||||
CsFloat run_float(Ident *id, TvalRange args);
|
||||
|
||||
bool run_bool(Bytecode const *code);
|
||||
bool run_bool(ostd::ConstCharRange code);
|
||||
|
@ -407,37 +409,37 @@ struct OSTD_EXPORT CsState {
|
|||
void set_alias(ostd::ConstCharRange name, TaggedValue &v);
|
||||
|
||||
void set_var_int(
|
||||
ostd::ConstCharRange name, int v,
|
||||
ostd::ConstCharRange name, CsInt v,
|
||||
bool dofunc = true, bool doclamp = true
|
||||
);
|
||||
void set_var_float(
|
||||
ostd::ConstCharRange name, float v,
|
||||
ostd::ConstCharRange name, CsFloat v,
|
||||
bool dofunc = true, bool doclamp = true
|
||||
);
|
||||
void set_var_str(
|
||||
ostd::ConstCharRange name, ostd::ConstCharRange v, bool dofunc = true
|
||||
);
|
||||
|
||||
void set_var_int_checked(Ident *id, int v);
|
||||
void set_var_int_checked(Ident *id, CsInt v);
|
||||
void set_var_int_checked(Ident *id, TvalRange args);
|
||||
void set_var_float_checked(Ident *id, float v);
|
||||
void set_var_float_checked(Ident *id, CsFloat v);
|
||||
void set_var_str_checked(Ident *id, ostd::ConstCharRange v);
|
||||
|
||||
ostd::Maybe<int> get_var_int(ostd::ConstCharRange name);
|
||||
ostd::Maybe<float> get_var_float(ostd::ConstCharRange name);
|
||||
ostd::Maybe<CsInt> get_var_int(ostd::ConstCharRange name);
|
||||
ostd::Maybe<CsFloat> get_var_float(ostd::ConstCharRange name);
|
||||
ostd::Maybe<ostd::String> get_var_str(ostd::ConstCharRange name);
|
||||
|
||||
ostd::Maybe<int> get_var_min_int(ostd::ConstCharRange name);
|
||||
ostd::Maybe<int> get_var_max_int(ostd::ConstCharRange name);
|
||||
ostd::Maybe<CsInt> get_var_min_int(ostd::ConstCharRange name);
|
||||
ostd::Maybe<CsInt> get_var_max_int(ostd::ConstCharRange name);
|
||||
|
||||
ostd::Maybe<float> get_var_min_float(ostd::ConstCharRange name);
|
||||
ostd::Maybe<float> get_var_max_float(ostd::ConstCharRange name);
|
||||
ostd::Maybe<CsFloat> get_var_min_float(ostd::ConstCharRange name);
|
||||
ostd::Maybe<CsFloat> get_var_max_float(ostd::ConstCharRange name);
|
||||
|
||||
ostd::Maybe<ostd::String> get_alias(ostd::ConstCharRange name);
|
||||
|
||||
void print_var(Ident *id);
|
||||
void print_var_int(Ident *id, int i);
|
||||
void print_var_float(Ident *id, float f);
|
||||
void print_var_int(Ident *id, CsInt i);
|
||||
void print_var_float(Ident *id, CsFloat f);
|
||||
void print_var_str(Ident *id, ostd::ConstCharRange s);
|
||||
};
|
||||
|
||||
|
@ -578,14 +580,15 @@ namespace util {
|
|||
);
|
||||
|
||||
template<typename R>
|
||||
inline ostd::Ptrdiff format_int(R &&writer, int val) {
|
||||
return ostd::format(ostd::forward<R>(writer), "%d", val);
|
||||
inline ostd::Ptrdiff format_int(R &&writer, CsInt val) {
|
||||
return ostd::format(ostd::forward<R>(writer), IntFormat, val);
|
||||
}
|
||||
|
||||
template<typename R>
|
||||
inline ostd::Ptrdiff format_float(R &&writer, int val) {
|
||||
inline ostd::Ptrdiff format_float(R &&writer, CsFloat val) {
|
||||
return ostd::format(
|
||||
ostd::forward<R>(writer), (val == int(val)) ? "%.1f" : "%.7g", val
|
||||
ostd::forward<R>(writer),
|
||||
(val == CsInt(val)) ? RoundFloatFormat : FloatFormat, val
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -606,7 +609,7 @@ namespace util {
|
|||
break;
|
||||
}
|
||||
case VAL_FLOAT: {
|
||||
auto r = format_float(ostd::forward<R>(writer), vals[i].i);
|
||||
auto r = format_float(ostd::forward<R>(writer), vals[i].f);
|
||||
if (r > 0) {
|
||||
ret += ostd::Size(r);
|
||||
}
|
||||
|
@ -631,4 +634,4 @@ namespace util {
|
|||
|
||||
} /* namespace cscript */
|
||||
|
||||
#endif /* CUBESCRIPT_HH */
|
||||
#endif /* LIBCUBESCRIPT_CUBESCRIPT_HH */
|
||||
|
|
44
lib_list.cc
44
lib_list.cc
|
@ -3,8 +3,8 @@
|
|||
namespace cscript {
|
||||
|
||||
char *cs_dup_ostr(ostd::ConstCharRange s);
|
||||
int cs_parse_int(ostd::ConstCharRange s);
|
||||
float cs_parse_float(ostd::ConstCharRange s);
|
||||
CsInt cs_parse_int(ostd::ConstCharRange s);
|
||||
CsFloat cs_parse_float(ostd::ConstCharRange s);
|
||||
ostd::ConstCharRange cs_parse_str(ostd::ConstCharRange str);
|
||||
char const *parseword(char const *p);
|
||||
|
||||
|
@ -190,7 +190,7 @@ static void cs_loop_list_conc(
|
|||
r.push_n(vstr.data(), vstr.size());
|
||||
v.cleanup();
|
||||
}
|
||||
if (n >= 0)
|
||||
if (n)
|
||||
id->pop_arg();
|
||||
r.push('\0');
|
||||
ostd::Size len = r.size();
|
||||
|
@ -211,7 +211,7 @@ static void cs_init_lib_list_sort(CsState &cs);
|
|||
|
||||
void cs_init_lib_list(CsState &cs) {
|
||||
cs.add_command("listlen", "s", [&cs](TvalRange args, TaggedValue &res) {
|
||||
res.set_int(int(util::list_length(args[0].get_strr())));
|
||||
res.set_int(CsInt(util::list_length(args[0].get_strr())));
|
||||
});
|
||||
|
||||
cs.add_command("at", "si1V", [&cs](TvalRange args, TaggedValue &res) {
|
||||
|
@ -222,7 +222,7 @@ void cs_init_lib_list(CsState &cs) {
|
|||
p.item = str;
|
||||
for (ostd::Size i = 1; i < args.size(); ++i) {
|
||||
p.input = str;
|
||||
int pos = args[i].get_int();
|
||||
CsInt pos = args[i].get_int();
|
||||
for (; pos > 0; --pos)
|
||||
if (!p.parse()) break;
|
||||
if (pos > 0 || !p.parse())
|
||||
|
@ -235,15 +235,15 @@ void cs_init_lib_list(CsState &cs) {
|
|||
});
|
||||
|
||||
cs.add_command("sublist", "siiN", [&cs](TvalRange args, TaggedValue &res) {
|
||||
int skip = args[1].get_int(),
|
||||
count = args[2].get_int(),
|
||||
numargs = args[2].get_int();
|
||||
CsInt skip = args[1].get_int(),
|
||||
count = args[2].get_int(),
|
||||
numargs = args[2].get_int();
|
||||
|
||||
int offset = ostd::max(skip, 0),
|
||||
len = (numargs >= 3) ? ostd::max(count, 0) : -1;
|
||||
CsInt offset = ostd::max(skip, 0),
|
||||
len = (numargs >= 3) ? ostd::max(count, 0) : -1;
|
||||
|
||||
ListParser p(args[0].get_strr());
|
||||
for (int i = 0; i < offset; ++i)
|
||||
for (CsInt i = 0; i < offset; ++i)
|
||||
if (!p.parse()) break;
|
||||
if (len < 0) {
|
||||
if (offset > 0)
|
||||
|
@ -273,7 +273,7 @@ void cs_init_lib_list(CsState &cs) {
|
|||
++n;
|
||||
cs_set_iter(*id, cs_dup_ostr(p.item), stack);
|
||||
if (cs.run_bool(body)) {
|
||||
res.set_int(n);
|
||||
res.set_int(CsInt(n));
|
||||
goto found;
|
||||
}
|
||||
}
|
||||
|
@ -311,7 +311,7 @@ found:
|
|||
|
||||
#define CS_CMD_LIST_FIND(name, fmt, gmeth, cmp) \
|
||||
cs.add_command(name, "s" fmt "i", [&cs](TvalRange args, TaggedValue &res) { \
|
||||
int n = 0, skip = args[2].get_int(); \
|
||||
CsInt n = 0, skip = args[2].get_int(); \
|
||||
auto val = args[1].gmeth(); \
|
||||
for (ListParser p(args[0].get_strr()); p.parse(); ++n) { \
|
||||
if (cmp) { \
|
||||
|
@ -369,7 +369,7 @@ found:
|
|||
cs_set_iter(*id, p.element().disown(), stack);
|
||||
cs.run_int(body);
|
||||
}
|
||||
if (n >= 0)
|
||||
if (n)
|
||||
id->pop_arg();
|
||||
});
|
||||
|
||||
|
@ -386,7 +386,7 @@ found:
|
|||
: cs_dup_ostr(""), stack2);
|
||||
cs.run_int(body);
|
||||
}
|
||||
if (n >= 0) {
|
||||
if (n) {
|
||||
id->pop_arg();
|
||||
id2->pop_arg();
|
||||
}
|
||||
|
@ -409,7 +409,7 @@ found:
|
|||
: cs_dup_ostr(""), stack3);
|
||||
cs.run_int(body);
|
||||
}
|
||||
if (n >= 0) {
|
||||
if (n) {
|
||||
id->pop_arg();
|
||||
id2->pop_arg();
|
||||
id3->pop_arg();
|
||||
|
@ -446,7 +446,7 @@ found:
|
|||
r.push_n(p.quote.data(), p.quote.size());
|
||||
}
|
||||
}
|
||||
if (n >= 0)
|
||||
if (n)
|
||||
id->pop_arg();
|
||||
r.push('\0');
|
||||
ostd::Size len = r.size() - 1;
|
||||
|
@ -466,7 +466,7 @@ found:
|
|||
if (cs.run_bool(body))
|
||||
r++;
|
||||
}
|
||||
if (n >= 0)
|
||||
if (n)
|
||||
id->pop_arg();
|
||||
res.set_int(r);
|
||||
});
|
||||
|
@ -535,13 +535,13 @@ found:
|
|||
#undef CS_CMD_LIST_MERGE
|
||||
|
||||
cs.add_command("listsplice", "ssii", [&cs](TvalRange args, TaggedValue &res) {
|
||||
int offset = ostd::max(args[2].get_int(), 0);
|
||||
int len = ostd::max(args[3].get_int(), 0);
|
||||
CsInt offset = ostd::max(args[2].get_int(), 0);
|
||||
CsInt len = ostd::max(args[3].get_int(), 0);
|
||||
ostd::ConstCharRange s = args[0].get_strr();
|
||||
ostd::ConstCharRange vals = args[1].get_strr();
|
||||
char const *list = s.data();
|
||||
ListParser p(s);
|
||||
for (int i = 0; i < offset; ++i)
|
||||
for (CsInt i = 0; i < offset; ++i)
|
||||
if (!p.parse())
|
||||
break;
|
||||
char const *qend = !p.quote.empty() ? &p.quote[p.quote.size()] : list;
|
||||
|
@ -553,7 +553,7 @@ found:
|
|||
buf.push(' ');
|
||||
buf.push_n(vals.data(), vals.size());
|
||||
}
|
||||
for (int i = 0; i < len; ++i)
|
||||
for (CsInt i = 0; i < len; ++i)
|
||||
if (!p.parse())
|
||||
break;
|
||||
p.skip();
|
||||
|
|
38
lib_math.cc
38
lib_math.cc
|
@ -4,8 +4,8 @@
|
|||
|
||||
namespace cscript {
|
||||
|
||||
static constexpr float PI = 3.14159265358979f;
|
||||
static constexpr float RAD = PI / 180.0f;
|
||||
static constexpr CsFloat PI = 3.14159265358979f;
|
||||
static constexpr CsFloat RAD = PI / 180.0f;
|
||||
|
||||
void cs_init_lib_math(CsState &cs) {
|
||||
cs.add_command("sin", "f", [&cs](TvalRange args, TaggedValue &res) {
|
||||
|
@ -50,7 +50,7 @@ void cs_init_lib_math(CsState &cs) {
|
|||
|
||||
#define CS_CMD_MIN_MAX(name, fmt, type, op) \
|
||||
cs.add_command(#name, #fmt "1V", [&cs](TvalRange args, TaggedValue &res) { \
|
||||
type v = !args.empty() ? args[0].fmt : 0; \
|
||||
auto v = !args.empty() ? args[0].fmt : 0; \
|
||||
for (ostd::Size i = 1; i < args.size(); ++i) v = op(v, args[i].fmt); \
|
||||
res.set_##type(v); \
|
||||
})
|
||||
|
@ -85,10 +85,10 @@ void cs_init_lib_math(CsState &cs) {
|
|||
} else {
|
||||
r = (r < 0) ? ceil(r - 0.5) : floor(r + 0.5);
|
||||
}
|
||||
res.set_float(float(r));
|
||||
res.set_float(CsFloat(r));
|
||||
});
|
||||
|
||||
#define CS_CMD_MATH(name, fmt, type, op, initval, unaryop) \
|
||||
#define CS_CMD_MATH(name, fmt, vtype, type, op, initval, unaryop) \
|
||||
cs.add_command(name, #fmt "1V", [&cs](TvalRange args, TaggedValue &res) { \
|
||||
type val; \
|
||||
if (args.size() >= 2) { \
|
||||
|
@ -107,13 +107,13 @@ void cs_init_lib_math(CsState &cs) {
|
|||
});
|
||||
|
||||
#define CS_CMD_MATHIN(name, op, initval, unaryop) \
|
||||
CS_CMD_MATH(#name, i, int, val = val op val2, initval, unaryop)
|
||||
CS_CMD_MATH(#name, i, CsInt, int, val = val op val2, initval, unaryop)
|
||||
|
||||
#define CS_CMD_MATHI(name, initval, unaryop) \
|
||||
CS_CMD_MATHIN(name, name, initval, unaryop)
|
||||
|
||||
#define CS_CMD_MATHFN(name, op, initval, unaryop) \
|
||||
CS_CMD_MATH(#name "f", f, float, val = val op val2, initval, unaryop)
|
||||
CS_CMD_MATH(#name "f", f, CsFloat, float, val = val op val2, initval, unaryop)
|
||||
|
||||
#define CS_CMD_MATHF(name, initval, unaryop) \
|
||||
CS_CMD_MATHFN(name, name, initval, unaryop)
|
||||
|
@ -130,26 +130,26 @@ void cs_init_lib_math(CsState &cs) {
|
|||
CS_CMD_MATHI(&~, 0, {});
|
||||
CS_CMD_MATHI(|~, 0, {});
|
||||
|
||||
CS_CMD_MATH("<<", i, int, {
|
||||
CS_CMD_MATH("<<", i, CsInt, int, {
|
||||
val = (val2 < 32) ? (val << ostd::max(val2, 0)) : 0;
|
||||
}, 0, {});
|
||||
CS_CMD_MATH(">>", i, int, val >>= ostd::clamp(val2, 0, 31), 0, {});
|
||||
CS_CMD_MATH(">>", i, CsInt, int, val >>= ostd::clamp(val2, 0, 31), 0, {});
|
||||
|
||||
CS_CMD_MATHF(+, 0, {});
|
||||
CS_CMD_MATHF(*, 1, {});
|
||||
CS_CMD_MATHF(-, 0, val = -val);
|
||||
|
||||
#define CS_CMD_DIV(name, fmt, type, op) \
|
||||
CS_CMD_MATH(#name, fmt, type, { if (val2) op; else val = 0; }, 0, {})
|
||||
#define CS_CMD_DIV(name, fmt, vtype, type, op) \
|
||||
CS_CMD_MATH(#name, fmt, vtype, type, { if (val2) op; else val = 0; }, 0, {})
|
||||
|
||||
CS_CMD_DIV(div, i, int, val /= val2);
|
||||
CS_CMD_DIV(mod, i, int, val %= val2);
|
||||
CS_CMD_DIV(divf, f, float, val /= val2);
|
||||
CS_CMD_DIV(modf, f, float, val = fmod(val, val2));
|
||||
CS_CMD_DIV(div, i, CsInt, int, val /= val2);
|
||||
CS_CMD_DIV(mod, i, CsInt, int, val %= val2);
|
||||
CS_CMD_DIV(divf, f, CsFloat, float, val /= val2);
|
||||
CS_CMD_DIV(modf, f, CsFloat, float, val = fmod(val, val2));
|
||||
|
||||
#undef CS_CMD_DIV
|
||||
|
||||
CS_CMD_MATH("pow", f, float, val = pow(val, val2), 0, {});
|
||||
CS_CMD_MATH("pow", f, CsFloat, float, val = pow(val, val2), 0, {});
|
||||
|
||||
#undef CS_CMD_MATHF
|
||||
#undef CS_CMD_MATHFN
|
||||
|
@ -166,12 +166,12 @@ void cs_init_lib_math(CsState &cs) {
|
|||
val = args[i-1].fmt op args[i].fmt; \
|
||||
} else \
|
||||
val = ((args.size() > 0) ? args[0].fmt : 0) op 0; \
|
||||
res.set_int(int(val)); \
|
||||
res.set_int(CsInt(val)); \
|
||||
})
|
||||
|
||||
#define CS_CMD_CMPIN(name, op) CS_CMD_CMP(#name, i, int, op)
|
||||
#define CS_CMD_CMPIN(name, op) CS_CMD_CMP(#name, i, CsInt, op)
|
||||
#define CS_CMD_CMPI(name) CS_CMD_CMPIN(name, name)
|
||||
#define CS_CMD_CMPFN(name, op) CS_CMD_CMP(#name "f", f, float, op)
|
||||
#define CS_CMD_CMPFN(name, op) CS_CMD_CMP(#name "f", f, CsFloat, op)
|
||||
#define CS_CMD_CMPF(name) CS_CMD_CMPFN(name, name)
|
||||
|
||||
CS_CMD_CMPIN(=, ==);
|
||||
|
|
30
lib_str.cc
30
lib_str.cc
|
@ -6,7 +6,7 @@ void cs_init_lib_string(CsState &cs) {
|
|||
cs.add_command("strstr", "ss", [&cs](TvalRange args, TaggedValue &res) {
|
||||
ostd::ConstCharRange a = args[0].get_strr(), b = args[1].get_strr();
|
||||
ostd::ConstCharRange s = a;
|
||||
for (int i = 0; b.size() <= s.size(); ++i) {
|
||||
for (CsInt i = 0; b.size() <= s.size(); ++i) {
|
||||
if (b == s.slice(0, b.size())) {
|
||||
res.set_int(i);
|
||||
return;
|
||||
|
@ -17,13 +17,13 @@ void cs_init_lib_string(CsState &cs) {
|
|||
});
|
||||
|
||||
cs.add_command("strlen", "s", [&cs](TvalRange args, TaggedValue &res) {
|
||||
res.set_int(int(args[0].get_strr().size()));
|
||||
res.set_int(CsInt(args[0].get_strr().size()));
|
||||
});
|
||||
|
||||
cs.add_command("strcode", "si", [&cs](TvalRange args, TaggedValue &res) {
|
||||
ostd::ConstCharRange str = args[0].get_strr();
|
||||
int i = args[1].get_int();
|
||||
if (i >= int(str.size())) {
|
||||
CsInt i = args[1].get_int();
|
||||
if (i >= CsInt(str.size())) {
|
||||
res.set_int(0);
|
||||
} else {
|
||||
res.set_int(ostd::byte(str[i]));
|
||||
|
@ -99,7 +99,7 @@ void cs_init_lib_string(CsState &cs) {
|
|||
f.pop_front();
|
||||
if (ic >= '1' && ic <= '9') {
|
||||
int i = ic - '0';
|
||||
ostd::String sub = ostd::move((i < int(args.size()))
|
||||
ostd::String sub = ostd::move((ostd::Size(i) < args.size())
|
||||
? args[i].get_str() : ostd::String(""));
|
||||
s.push_n(sub.data(), sub.size());
|
||||
} else s.push(ic);
|
||||
|
@ -120,9 +120,9 @@ void cs_init_lib_string(CsState &cs) {
|
|||
|
||||
cs.add_command("substr", "siiN", [&cs](TvalRange args, TaggedValue &res) {
|
||||
ostd::ConstCharRange s = args[0].get_strr();
|
||||
int start = args[1].get_int(), count = args[2].get_int();
|
||||
int numargs = args[3].get_int();
|
||||
int len = int(s.size()), offset = ostd::clamp(start, 0, len);
|
||||
CsInt start = args[1].get_int(), count = args[2].get_int();
|
||||
CsInt numargs = args[3].get_int();
|
||||
CsInt len = CsInt(s.size()), offset = ostd::clamp(start, 0, len);
|
||||
res.set_str(ostd::ConstCharRange(
|
||||
&s[offset],
|
||||
(numargs >= 3) ? ostd::clamp(count, 0, len - offset)
|
||||
|
@ -139,7 +139,7 @@ void cs_init_lib_string(CsState &cs) {
|
|||
val = strcmp(args[i-1].s, args[i].s) op 0; \
|
||||
} else \
|
||||
val = (!args.empty() ? args[0].s[0] : 0) op 0; \
|
||||
res.set_int(int(val)); \
|
||||
res.set_int(CsInt(val)); \
|
||||
})
|
||||
|
||||
CS_CMD_CMPS(strcmp, ==);
|
||||
|
@ -199,12 +199,12 @@ void cs_init_lib_string(CsState &cs) {
|
|||
cs.add_command("strsplice", "ssii", [&cs](TvalRange args, TaggedValue &res) {
|
||||
ostd::ConstCharRange s = args[0].get_strr();
|
||||
ostd::ConstCharRange vals = args[1].get_strr();
|
||||
int skip = args[2].get_int(),
|
||||
count = args[3].get_int();
|
||||
int slen = int(s.size()),
|
||||
vlen = int(vals.size());
|
||||
int offset = ostd::clamp(skip, 0, slen),
|
||||
len = ostd::clamp(count, 0, slen - offset);
|
||||
CsInt skip = args[2].get_int(),
|
||||
count = args[3].get_int();
|
||||
CsInt slen = CsInt(s.size()),
|
||||
vlen = CsInt(vals.size());
|
||||
CsInt offset = ostd::clamp(skip, 0, slen),
|
||||
len = ostd::clamp(count, 0, slen - offset);
|
||||
char *p = new char[slen - len + vlen + 1];
|
||||
if (offset)
|
||||
memcpy(p, s.data(), offset);
|
||||
|
|
Loading…
Reference in New Issue