avoid compilation warnings

This commit is contained in:
ValKmjolnir 2023-11-23 21:35:11 +08:00
parent 01ceaf7e66
commit cacf0ae86a
6 changed files with 66 additions and 59 deletions

View File

@ -10,7 +10,7 @@ void filehandle_destructor(void* ptr) {
var builtin_readfile(context* ctx, gc* ngc) {
auto filename = ctx->localr[1];
if (filename.type!=vm_type::vm_str) {
if (!filename.is_str()) {
return nas_err("io::readfile", "\"filename\" must be string");
}
std::ifstream in(filename.str(), std::ios::binary);
@ -25,7 +25,7 @@ var builtin_fout(context* ctx, gc* ngc) {
auto local = ctx->localr;
auto filename = local[1];
auto source = local[2];
if (filename.type!=vm_type::vm_str) {
if (!filename.is_str()) {
return nas_err("io::fout", "\"filename\" must be string");
}
std::ofstream out(filename.str());
@ -38,7 +38,7 @@ var builtin_fout(context* ctx, gc* ngc) {
var builtin_exists(context* ctx, gc* ngc) {
auto filename = ctx->localr[1];
if (filename.type!=vm_type::vm_str) {
if (!filename.is_str()) {
return zero;
}
return access(filename.str().c_str(), F_OK)!=-1? one:zero;
@ -48,10 +48,10 @@ var builtin_open(context* ctx, gc* ngc) {
auto local = ctx->localr;
auto name = local[1];
auto mode = local[2];
if (name.type!=vm_type::vm_str) {
if (!name.is_str()) {
return nas_err("io::open", "\"filename\" must be string");
}
if (mode.type!=vm_type::vm_str) {
if (!mode.is_str()) {
return nas_err("io::open", "\"mode\" must be string");
}
auto file_descriptor = fopen(name.str().c_str(), mode.str().c_str());
@ -82,10 +82,10 @@ var builtin_read(context* ctx, gc* ngc) {
if (!file_descriptor.object_check(file_type_name)) {
return nas_err("io::read", "not a valid filehandle");
}
if (buffer.type!=vm_type::vm_str || buffer.val.gcobj->unmutable) {
if (!buffer.is_str() || buffer.val.gcobj->unmutable) {
return nas_err("io::read", "\"buf\" must be mutable string");
}
if (length.type!=vm_type::vm_num) {
if (!length.is_num()) {
return nas_err("io::read", "\"len\" must be number");
}
if (length.num()<=0 || length.num()>=(1<<30)) {
@ -112,7 +112,7 @@ var builtin_write(context* ctx, gc* ngc) {
if (!file_descriptor.object_check(file_type_name)) {
return nas_err("io::write", "not a valid filehandle");
}
if (source.type!=vm_type::vm_str) {
if (!source.is_str()) {
return nas_err("io::write", "\"str\" must be string");
}
return var::num(static_cast<f64>(fwrite(
@ -170,7 +170,7 @@ var builtin_readln(context* ctx, gc* ngc) {
var builtin_stat(context* ctx, gc* ngc) {
auto name = ctx->localr[1];
if (name.type!=vm_type::vm_str) {
if (!name.is_str()) {
return nas_err("io::stat", "\"filename\" must be string");
}
struct stat buffer;

View File

@ -5,7 +5,7 @@ namespace nasal {
var builtin_pow(context* ctx, gc* ngc) {
auto x = ctx->localr[1];
auto y = ctx->localr[2];
if (x.type!=vm_type::vm_num || y.type!=vm_type::vm_num) {
if (!x.is_num() || !y.is_num()) {
return var::num(std::nan(""));
}
return var::num(std::pow(x.num(), y.num()));
@ -49,7 +49,7 @@ var builtin_sqrt(context* ctx, gc* ngc) {
var builtin_atan2(context* ctx, gc* ngc) {
auto x = ctx->localr[1];
auto y = ctx->localr[2];
if (x.type!=vm_type::vm_num || y.type!=vm_type::vm_num) {
if (!x.is_num() || !y.is_num()) {
return var::num(std::nan(""));
}
return var::num(atan2(y.num(), x.num()));

View File

@ -33,7 +33,7 @@ var builtin_append(context* ctx, gc* ngc) {
auto local = ctx->localr;
var vec = local[1];
var elem = local[2];
if (vec.type!=vm_type::vm_vec) {
if (!vec.is_vec()) {
return nas_err("append", "\"vec\" must be vector");
}
auto& v = vec.vec().elems;
@ -47,10 +47,10 @@ var builtin_setsize(context* ctx, gc* ngc) {
auto local = ctx->localr;
var vec = local[1];
var size = local[2];
if (vec.type!=vm_type::vm_vec) {
if (!vec.is_vec()) {
return nas_err("setsize", "\"vec\" must be vector");
}
if (size.type!=vm_type::vm_num || size.num()<0) {
if (!size.is_num() || size.num()<0) {
return nil;
}
vec.vec().elems.resize(static_cast<i64>(size.num()), nil);
@ -59,7 +59,7 @@ var builtin_setsize(context* ctx, gc* ngc) {
var builtin_system(context* ctx, gc* ngc) {
auto str = ctx->localr[1];
if (str.type!=vm_type::vm_str) {
if (!str.is_str()) {
return var::num(-1);
}
return var::num(static_cast<f64>(system(str.str().c_str())));
@ -69,7 +69,7 @@ var builtin_input(context* ctx, gc* ngc) {
auto local = ctx->localr;
var end = local[1];
var ret = ngc->alloc(vm_type::vm_str);
if (end.type!=vm_type::vm_str || end.str().length()>1 || !end.str().length()) {
if (!end.is_str() || end.str().length()>1 || !end.str().length()) {
std::cin >> ret.str();
} else {
std::getline(std::cin, ret.str(), end.str()[0]);
@ -81,10 +81,10 @@ var builtin_split(context* ctx, gc* ngc) {
auto local = ctx->localr;
var delimeter = local[1];
var str = local[2];
if (delimeter.type!=vm_type::vm_str) {
if (!delimeter.is_str()) {
return nas_err("split", "\"separator\" must be string");
}
if (str.type!=vm_type::vm_str) {
if (!str.is_str()) {
return nas_err("split", "\"str\" must be string");
}
const auto& deli = delimeter.str();
@ -119,7 +119,7 @@ var builtin_split(context* ctx, gc* ngc) {
var builtin_rand(context* ctx, gc* ngc) {
auto val = ctx->localr[1];
if (val.type!=vm_type::vm_num && val.type!=vm_type::vm_nil) {
if (!val.is_num() && !val.is_nil()) {
return nas_err("rand", "\"seed\" must be nil or number");
}
if (val.is_num()) {
@ -146,7 +146,7 @@ var builtin_id(context* ctx, gc* ngc) {
var builtin_int(context* ctx, gc* ngc) {
auto val = ctx->localr[1];
if (val.type!=vm_type::vm_num && val.type!=vm_type::vm_str) {
if (!val.is_num() && !val.is_str()) {
return nil;
}
return var::num(static_cast<f64>(static_cast<i32>(val.to_num())));
@ -167,7 +167,7 @@ var builtin_num(context* ctx, gc* ngc) {
if (val.is_num()) {
return val;
}
if (val.type!=vm_type::vm_str) {
if (!val.is_str()) {
return nil;
}
auto res = val.to_num();
@ -179,7 +179,7 @@ var builtin_num(context* ctx, gc* ngc) {
var builtin_pop(context* ctx, gc* ngc) {
auto val = ctx->localr[1];
if (val.type!=vm_type::vm_vec) {
if (!val.is_vec()) {
return nas_err("pop", "\"vec\" must be vector");
}
auto& vec = val.vec().elems;
@ -204,13 +204,14 @@ var builtin_size(context* ctx, gc* ngc) {
case vm_type::vm_vec: num = val.vec().size(); break;
case vm_type::vm_hash: num = val.hash().size(); break;
case vm_type::vm_map: num = val.map().mapper.size(); break;
default: break;
}
return var::num(num);
}
var builtin_time(context* ctx, gc* ngc) {
auto val = ctx->localr[1];
if (val.type!=vm_type::vm_num) {
if (!val.is_num()) {
return nas_err("time", "\"begin\" must be number");
}
auto begin = static_cast<time_t>(val.num());
@ -221,7 +222,7 @@ var builtin_contains(context* ctx, gc* ngc) {
auto local = ctx->localr;
var hash = local[1];
var key = local[2];
if (hash.type!=vm_type::vm_hash || key.type!=vm_type::vm_str) {
if (!hash.is_hash() || !key.is_str()) {
return zero;
}
return hash.hash().elems.count(key.str())? one:zero;
@ -231,10 +232,10 @@ var builtin_delete(context* ctx, gc* ngc) {
auto local = ctx->localr;
var hash = local[1];
var key = local[2];
if (hash.type!=vm_type::vm_hash) {
if (!hash.is_hash()) {
return nas_err("delete", "\"hash\" must be hash");
}
if (key.type!=vm_type::vm_str) {
if (!key.is_str()) {
return nil;
}
if (hash.hash().elems.count(key.str())) {
@ -245,7 +246,7 @@ var builtin_delete(context* ctx, gc* ngc) {
var builtin_keys(context* ctx, gc* ngc) {
auto hash = ctx->localr[1];
if (hash.type!=vm_type::vm_hash && hash.type!=vm_type::vm_map) {
if (!hash.is_hash() && !hash.is_map()) {
return nas_err("keys", "\"hash\" must be hash");
}
// avoid being sweeped
@ -291,6 +292,7 @@ var builtin_type(context* ctx, gc* ngc) {
case vm_type::vm_ghost: return ngc->newstr("ghost");
case vm_type::vm_co: return ngc->newstr("coroutine");
case vm_type::vm_map: return ngc->newstr("namespace");
default: break;
}
return nil;
}
@ -300,13 +302,13 @@ var builtin_substr(context* ctx, gc* ngc) {
var str = local[1];
var beg = local[2];
var len = local[3];
if (str.type!=vm_type::vm_str) {
if (!str.is_str()) {
return nas_err("substr", "\"str\" must be string");
}
if (beg.type!=vm_type::vm_num || beg.num()<0) {
if (!beg.is_num() || beg.num()<0) {
return nas_err("substr", "\"begin\" should be number >= 0");
}
if (len.type!=vm_type::vm_num || len.num()<0) {
if (!len.is_num() || len.num()<0) {
return nas_err("substr", "\"length\" should be number >= 0");
}
auto begin = static_cast<usize>(beg.num());
@ -322,7 +324,7 @@ var builtin_streq(context* ctx, gc* ngc) {
var a = local[1];
var b = local[2];
return var::num(static_cast<f64>(
(a.type!=vm_type::vm_str || b.type!=vm_type::vm_str)? 0:(a.str()==b.str())
(!a.is_str() || !b.is_str())? 0:(a.str()==b.str())
));
}
@ -330,10 +332,10 @@ var builtin_left(context* ctx, gc* ngc) {
auto local = ctx->localr;
var str = local[1];
var len = local[2];
if (str.type!=vm_type::vm_str) {
if (!str.is_str()) {
return nas_err("left", "\"string\" must be string");
}
if (len.type!=vm_type::vm_num) {
if (!len.is_num()) {
return nas_err("left", "\"length\" must be number");
}
if (len.num()<0) {
@ -346,10 +348,10 @@ var builtin_right(context* ctx, gc* ngc) {
auto local = ctx->localr;
var str = local[1];
var len = local[2];
if (str.type!=vm_type::vm_str) {
if (!str.is_str()) {
return nas_err("right", "\"string\" must be string");
}
if (len.type!=vm_type::vm_num) {
if (!len.is_num()) {
return nas_err("right", "\"length\" must be number");
}
i32 length = static_cast<i32>(len.num());
@ -367,7 +369,7 @@ var builtin_cmp(context* ctx, gc* ngc) {
auto local = ctx->localr;
var a = local[1];
var b = local[2];
if (a.type!=vm_type::vm_str || b.type!=vm_type::vm_str) {
if (!a.is_str() || !b.is_str()) {
return nas_err("cmp", "\"a\" and \"b\" must be string");
}
return var::num(static_cast<f64>(strcmp(
@ -410,7 +412,7 @@ var builtin_char(context* ctx, gc* ngc) {
var builtin_values(context* ctx, gc* ngc) {
auto hash = ctx->localr[1];
if (hash.type!=vm_type::vm_hash && hash.type!=vm_type::vm_map) {
if (!hash.is_hash() && !hash.is_map()) {
return nas_err("values", "\"hash\" must be hash or namespace");
}
auto vec = ngc->alloc(vm_type::vm_vec);
@ -429,7 +431,7 @@ var builtin_values(context* ctx, gc* ngc) {
var builtin_sleep(context* ctx, gc* ngc) {
auto val = ctx->localr[1];
if (val.type!=vm_type::vm_num) {
if (!val.is_num()) {
return nil;
}
#if defined(_WIN32) && !defined(_GLIBCXX_HAS_GTHREADS)
@ -561,7 +563,7 @@ std::string md5(const std::string& src) {
var builtin_md5(context* ctx, gc* ngc) {
auto str = ctx->localr[1];
if (str.type!=vm_type::vm_str) {
if (!str.is_str()) {
return nas_err("md5", "\"str\" must be string");
}
return ngc->newstr(md5(str.str()));
@ -576,7 +578,7 @@ var builtin_millisec(context* ctx, gc* ngc) {
var builtin_gcextend(context* ctx, gc* ngc) {
auto type = ctx->localr[1];
if (type.type!=vm_type::vm_str) {
if (!type.is_str()) {
return nil;
}
const auto& s = type.str();

View File

@ -42,16 +42,16 @@ var nas_hash::get_value(const std::string& key) {
} else if (!elems.count("parents")) {
return var::none();
}
var ret = var::none();
var val = elems.at("parents");
if (val.type!=vm_type::vm_vec) {
auto ret = var::none();
auto& val = elems.at("parents");
if (!val.is_vec()) {
return ret;
}
for(auto& i : val.vec().elems) {
if (i.is_hash()) {
ret = i.hash().get_value(key);
}
if (ret.type!=vm_type::vm_none) {
if (!ret.is_none()) {
return ret;
}
}
@ -65,8 +65,8 @@ var* nas_hash::get_memory(const std::string& key) {
return nullptr;
}
var* addr = nullptr;
var val = elems.at("parents");
if (val.type!=vm_type::vm_vec) {
var& val = elems.at("parents");
if (!val.is_vec()) {
return addr;
}
for(auto& i : val.vec().elems) {
@ -208,6 +208,7 @@ nas_val::nas_val(vm_type val_type) {
case vm_type::vm_ghost: ptr.obj = new nas_ghost; break;
case vm_type::vm_co: ptr.co = new nas_co; break;
case vm_type::vm_map: ptr.map = new nas_map; break;
default: break;
}
}
@ -221,6 +222,7 @@ nas_val::~nas_val() {
case vm_type::vm_ghost:delete ptr.obj; break;
case vm_type::vm_co: delete ptr.co; break;
case vm_type::vm_map: delete ptr.map; break;
default: break;
}
type = vm_type::vm_nil;
}
@ -235,6 +237,7 @@ void nas_val::clear() {
case vm_type::vm_ghost:ptr.obj->clear(); break;
case vm_type::vm_co: ptr.co->clear(); break;
case vm_type::vm_map: ptr.map->clear(); break;
default: break;
}
}
@ -266,6 +269,7 @@ std::ostream& operator<<(std::ostream& out, var& ref) {
case vm_type::vm_ghost:out << ref.ghost(); break;
case vm_type::vm_co: out << ref.co(); break;
case vm_type::vm_map: out << ref.map(); break;
default: break;
}
return out;
}

View File

@ -381,6 +381,7 @@ std::string vm::type_name_string(const var& value) const {
case vm_type::vm_ghost: return "ghost type";
case vm_type::vm_co: return "coroutine";
case vm_type::vm_map: return "namespace";
default: break;
}
return "unknown";
}

View File

@ -503,7 +503,7 @@ inline void vm::o_eq() {
} else if (val1.is_str() && val2.is_str()) {
ctx.top[0] = (val1.str()==val2.str())? one:zero;
} else if ((val1.is_num() || val2.is_num())
&& val1.type!=vm_type::vm_nil && val2.type!=vm_type::vm_nil) {
&& !val1.is_nil() && !val2.is_nil()) {
ctx.top[0] = (val1.to_num()==val2.to_num())? one:zero;
} else {
ctx.top[0] = (val1==val2)? one:zero;
@ -518,7 +518,7 @@ inline void vm::o_neq() {
} else if (val1.is_str() && val2.is_str()) {
ctx.top[0] = (val1.str()!=val2.str())? one:zero;
} else if ((val1.is_num() || val2.is_num())
&& val1.type!=vm_type::vm_nil && val2.type!=vm_type::vm_nil) {
&& !val1.is_nil() && !val2.is_nil()) {
ctx.top[0] = (val1.to_num()!=val2.to_num())? one:zero;
} else {
ctx.top[0] = (val1!=val2)? one:zero;
@ -567,7 +567,7 @@ inline void vm::o_jf() {
}
inline void vm::o_cnt() {
if (ctx.top[0].type!=vm_type::vm_vec) {
if (!ctx.top[0].is_vec()) {
die("must use vector in forindex/foreach but get "+
type_name_string(ctx.top[0])
);
@ -620,7 +620,7 @@ inline void vm::o_callv() {
return;
}
} else if (vec.is_hash()) {
if (val.type!=vm_type::vm_str) {
if (!val.is_str()) {
die("must use string as the key but get "+type_name_string(val));
return;
}
@ -643,7 +643,7 @@ inline void vm::o_callv() {
static_cast<f64>(static_cast<u8>(str[num>=0? num:num+len]))
);
} else if (vec.is_map()) {
if (val.type!=vm_type::vm_str) {
if (!val.is_str()) {
die("must use string as the key but get "+type_name_string(val));
return;
}
@ -660,7 +660,7 @@ inline void vm::o_callv() {
inline void vm::o_callvi() {
var val = ctx.top[0];
if (val.type!=vm_type::vm_vec) {
if (!val.is_vec()) {
die("must use a vector but get "+type_name_string(val));
return;
}
@ -674,7 +674,7 @@ inline void vm::o_callvi() {
inline void vm::o_callh() {
var val = ctx.top[0];
if (val.type!=vm_type::vm_hash && val.type!=vm_type::vm_map) {
if (!val.is_hash() && !val.is_map()) {
die("must call a hash but get "+type_name_string(val));
return;
}
@ -697,7 +697,7 @@ inline void vm::o_callh() {
inline void vm::o_callfv() {
const u32 argc = imm[ctx.pc]; // arguments counter
var* local = ctx.top-argc+1; // arguments begin address
if (local[-1].type!=vm_type::vm_func) {
if (!local[-1].is_func()) {
die("must call a function but get "+type_name_string(local[-1]));
return;
}
@ -766,7 +766,7 @@ inline void vm::o_callfv() {
inline void vm::o_callfh() {
const auto& hash = ctx.top[0].hash().elems;
if (ctx.top[-1].type!=vm_type::vm_func) {
if (!ctx.top[-1].is_func()) {
die("must call a function but get "+type_name_string(ctx.top[-1]));
return;
}
@ -844,7 +844,7 @@ inline void vm::o_slcbeg() {
// | resource_vec | <-- top[-1]
// +--------------+
(++ctx.top)[0] = ngc.alloc(vm_type::vm_vec);
if (ctx.top[-1].type!=vm_type::vm_vec) {
if (!ctx.top[-1].is_vec()) {
die("must slice a vector but get "+type_name_string(ctx.top[-1]));
return;
}
@ -933,7 +933,7 @@ inline void vm::o_mcallv() {
return;
}
} else if (vec.is_hash()) { // do mcallh but use the mcallv way
if (val.type!=vm_type::vm_str) {
if (!val.is_str()) {
die("must use string as the key but get "+type_name_string(val));
return;
}
@ -945,7 +945,7 @@ inline void vm::o_mcallv() {
ctx.memr = ref.get_memory(str);
}
} else if (vec.is_map()) {
if (val.type!=vm_type::vm_str) {
if (!val.is_str()) {
die("must use string as the key but get "+type_name_string(val));
return;
}
@ -963,7 +963,7 @@ inline void vm::o_mcallv() {
inline void vm::o_mcallh() {
var hash = ctx.top[0]; // mcall hash, reserved on stack to avoid gc
if (hash.type!=vm_type::vm_hash && hash.type!=vm_type::vm_map) {
if (!hash.is_hash() && !hash.is_map()) {
die("must call a hash/namespace but get "+type_name_string(hash));
return;
}