✨ improve code & add new test file
This commit is contained in:
parent
54317a39a7
commit
7f8a0b6445
|
@ -35,8 +35,7 @@ bool is_powerpc();
|
||||||
bool is_superh();
|
bool is_superh();
|
||||||
|
|
||||||
|
|
||||||
// virtual machine stack depth
|
// virtual machine stack depth, both global depth and value stack depth
|
||||||
// both global depth and value stack depth
|
|
||||||
const u32 STACK_DEPTH = 4096;
|
const u32 STACK_DEPTH = 4096;
|
||||||
|
|
||||||
f64 hex2f(const char*);
|
f64 hex2f(const char*);
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
namespace nasal {
|
namespace nasal {
|
||||||
|
|
||||||
var nas_vec::get_val(const i32 n) {
|
var nas_vec::get_value(const i32 n) {
|
||||||
i32 size = elems.size();
|
i32 size = elems.size();
|
||||||
if (n<-size || n>=size) {
|
if (n<-size || n>=size) {
|
||||||
return var::none();
|
return var::none();
|
||||||
|
@ -10,7 +10,7 @@ var nas_vec::get_val(const i32 n) {
|
||||||
return elems[n>=0? n:n+size];
|
return elems[n>=0? n:n+size];
|
||||||
}
|
}
|
||||||
|
|
||||||
var* nas_vec::get_mem(const i32 n) {
|
var* nas_vec::get_memory(const i32 n) {
|
||||||
i32 size = elems.size();
|
i32 size = elems.size();
|
||||||
if (n<-size || n>=size) {
|
if (n<-size || n>=size) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
@ -33,7 +33,7 @@ std::ostream& operator<<(std::ostream& out, nas_vec& vec) {
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
var nas_hash::get_val(const std::string& key) {
|
var nas_hash::get_value(const std::string& key) {
|
||||||
if (elems.count(key)) {
|
if (elems.count(key)) {
|
||||||
return elems.at(key);
|
return elems.at(key);
|
||||||
} else if (!elems.count("parents")) {
|
} else if (!elems.count("parents")) {
|
||||||
|
@ -46,7 +46,7 @@ var nas_hash::get_val(const std::string& key) {
|
||||||
}
|
}
|
||||||
for(auto& i : val.vec().elems) {
|
for(auto& i : val.vec().elems) {
|
||||||
if (i.type==vm_hash) {
|
if (i.type==vm_hash) {
|
||||||
ret = i.hash().get_val(key);
|
ret = i.hash().get_value(key);
|
||||||
}
|
}
|
||||||
if (ret.type!=vm_none) {
|
if (ret.type!=vm_none) {
|
||||||
return ret;
|
return ret;
|
||||||
|
@ -55,7 +55,7 @@ var nas_hash::get_val(const std::string& key) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
var* nas_hash::get_mem(const std::string& key) {
|
var* nas_hash::get_memory(const std::string& key) {
|
||||||
if (elems.count(key)) {
|
if (elems.count(key)) {
|
||||||
return &elems.at(key);
|
return &elems.at(key);
|
||||||
} else if (!elems.count("parents")) {
|
} else if (!elems.count("parents")) {
|
||||||
|
@ -68,7 +68,7 @@ var* nas_hash::get_mem(const std::string& key) {
|
||||||
}
|
}
|
||||||
for(auto& i : val.vec().elems) {
|
for(auto& i : val.vec().elems) {
|
||||||
if (i.type==vm_hash) {
|
if (i.type==vm_hash) {
|
||||||
addr = i.hash().get_mem(key);
|
addr = i.hash().get_memory(key);
|
||||||
}
|
}
|
||||||
if (addr) {
|
if (addr) {
|
||||||
return addr;
|
return addr;
|
||||||
|
@ -160,14 +160,14 @@ std::ostream& operator<<(std::ostream& out, const nas_co& co) {
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
var nas_map::get_val(const std::string& key) {
|
var nas_map::get_value(const std::string& key) {
|
||||||
if (mapper.count(key)) {
|
if (mapper.count(key)) {
|
||||||
return *mapper.at(key);
|
return *mapper.at(key);
|
||||||
}
|
}
|
||||||
return var::none();
|
return var::none();
|
||||||
}
|
}
|
||||||
|
|
||||||
var* nas_map::get_mem(const std::string& key) {
|
var* nas_map::get_memory(const std::string& key) {
|
||||||
if (mapper.count(key)) {
|
if (mapper.count(key)) {
|
||||||
return mapper.at(key);
|
return mapper.at(key);
|
||||||
}
|
}
|
||||||
|
|
|
@ -102,24 +102,22 @@ struct nas_vec {
|
||||||
std::vector<var> elems;
|
std::vector<var> elems;
|
||||||
|
|
||||||
// mark if this is printed, avoid stack overflow
|
// mark if this is printed, avoid stack overflow
|
||||||
bool printed;
|
bool printed = false;
|
||||||
|
|
||||||
nas_vec():printed(false) {}
|
|
||||||
usize size() const {return elems.size();}
|
usize size() const {return elems.size();}
|
||||||
var get_val(const i32);
|
var get_value(const i32);
|
||||||
var* get_mem(const i32);
|
var* get_memory(const i32);
|
||||||
};
|
};
|
||||||
|
|
||||||
struct nas_hash {
|
struct nas_hash {
|
||||||
std::unordered_map<std::string, var> elems;
|
std::unordered_map<std::string, var> elems;
|
||||||
|
|
||||||
// mark if this is printed, avoid stack overflow
|
// mark if this is printed, avoid stack overflow
|
||||||
bool printed;
|
bool printed = false;
|
||||||
|
|
||||||
nas_hash(): printed(false) {}
|
|
||||||
usize size() const {return elems.size();}
|
usize size() const {return elems.size();}
|
||||||
var get_val(const std::string&);
|
var get_value(const std::string&);
|
||||||
var* get_mem(const std::string&);
|
var* get_memory(const std::string&);
|
||||||
};
|
};
|
||||||
|
|
||||||
struct nas_func {
|
struct nas_func {
|
||||||
|
@ -180,9 +178,7 @@ public:
|
||||||
void clear();
|
void clear();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
const std::string& get_ghost_name() const {
|
const auto& get_ghost_name() const {return type_name;}
|
||||||
return type_name;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct context {
|
struct context {
|
||||||
|
@ -220,13 +216,12 @@ struct nas_map {
|
||||||
bool printed = false;
|
bool printed = false;
|
||||||
std::unordered_map<std::string, var*> mapper;
|
std::unordered_map<std::string, var*> mapper;
|
||||||
|
|
||||||
nas_map() {}
|
|
||||||
void clear() {
|
void clear() {
|
||||||
mapper.clear();
|
mapper.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
var get_val(const std::string&);
|
var get_value(const std::string&);
|
||||||
var* get_mem(const std::string&);
|
var* get_memory(const std::string&);
|
||||||
};
|
};
|
||||||
|
|
||||||
struct nas_val {
|
struct nas_val {
|
||||||
|
|
|
@ -612,7 +612,7 @@ inline void vm::o_callv() {
|
||||||
var val = ctx.top[0];
|
var val = ctx.top[0];
|
||||||
var vec = (--ctx.top)[0];
|
var vec = (--ctx.top)[0];
|
||||||
if (vec.type==vm_vec) {
|
if (vec.type==vm_vec) {
|
||||||
ctx.top[0] = vec.vec().get_val(val.to_num());
|
ctx.top[0] = vec.vec().get_value(val.to_num());
|
||||||
if (ctx.top[0].type==vm_none) {
|
if (ctx.top[0].type==vm_none) {
|
||||||
die(report_out_of_range(val.to_num(), vec.vec().size()));
|
die(report_out_of_range(val.to_num(), vec.vec().size()));
|
||||||
return;
|
return;
|
||||||
|
@ -622,7 +622,7 @@ inline void vm::o_callv() {
|
||||||
die("must use string as the key but get "+type_name_string(val));
|
die("must use string as the key but get "+type_name_string(val));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ctx.top[0] = vec.hash().get_val(val.str());
|
ctx.top[0] = vec.hash().get_value(val.str());
|
||||||
if (ctx.top[0].type==vm_none) {
|
if (ctx.top[0].type==vm_none) {
|
||||||
die(report_key_not_found(val.str(), vec.hash()));
|
die(report_key_not_found(val.str(), vec.hash()));
|
||||||
return;
|
return;
|
||||||
|
@ -645,7 +645,7 @@ inline void vm::o_callv() {
|
||||||
die("must use string as the key but get "+type_name_string(val));
|
die("must use string as the key but get "+type_name_string(val));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ctx.top[0] = vec.map().get_val(val.str());
|
ctx.top[0] = vec.map().get_value(val.str());
|
||||||
if (ctx.top[0].type==vm_none) {
|
if (ctx.top[0].type==vm_none) {
|
||||||
die("cannot find symbol \""+val.str()+"\"");
|
die("cannot find symbol \""+val.str()+"\"");
|
||||||
return;
|
return;
|
||||||
|
@ -663,7 +663,7 @@ inline void vm::o_callvi() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// cannot use operator[],because this may cause overflow
|
// cannot use operator[],because this may cause overflow
|
||||||
(++ctx.top)[0] = val.vec().get_val(imm[ctx.pc]);
|
(++ctx.top)[0] = val.vec().get_value(imm[ctx.pc]);
|
||||||
if (ctx.top[0].type==vm_none) {
|
if (ctx.top[0].type==vm_none) {
|
||||||
die(report_out_of_range(imm[ctx.pc], val.vec().size()));
|
die(report_out_of_range(imm[ctx.pc], val.vec().size()));
|
||||||
return;
|
return;
|
||||||
|
@ -678,9 +678,9 @@ inline void vm::o_callh() {
|
||||||
}
|
}
|
||||||
const auto& str = const_string[imm[ctx.pc]];
|
const auto& str = const_string[imm[ctx.pc]];
|
||||||
if (val.type==vm_hash) {
|
if (val.type==vm_hash) {
|
||||||
ctx.top[0] = val.hash().get_val(str);
|
ctx.top[0] = val.hash().get_value(str);
|
||||||
} else {
|
} else {
|
||||||
ctx.top[0] = val.map().get_val(str);
|
ctx.top[0] = val.map().get_value(str);
|
||||||
}
|
}
|
||||||
if (ctx.top[0].type==vm_none) {
|
if (ctx.top[0].type==vm_none) {
|
||||||
val.type==vm_hash?
|
val.type==vm_hash?
|
||||||
|
@ -854,7 +854,7 @@ inline void vm::o_slcend() {
|
||||||
|
|
||||||
inline void vm::o_slc() {
|
inline void vm::o_slc() {
|
||||||
var val = (ctx.top--)[0];
|
var val = (ctx.top--)[0];
|
||||||
var res = ctx.top[-1].vec().get_val(val.to_num());
|
var res = ctx.top[-1].vec().get_value(val.to_num());
|
||||||
if (res.type==vm_none) {
|
if (res.type==vm_none) {
|
||||||
die(report_out_of_range(val.to_num(), ctx.top[-1].vec().size()));
|
die(report_out_of_range(val.to_num(), ctx.top[-1].vec().size()));
|
||||||
return;
|
return;
|
||||||
|
@ -923,7 +923,7 @@ inline void vm::o_mcallv() {
|
||||||
var val = ctx.top[0]; // index
|
var val = ctx.top[0]; // index
|
||||||
var vec = (--ctx.top)[0]; // mcall vector, reserved on stack to avoid gc
|
var vec = (--ctx.top)[0]; // mcall vector, reserved on stack to avoid gc
|
||||||
if (vec.type==vm_vec) {
|
if (vec.type==vm_vec) {
|
||||||
ctx.memr = vec.vec().get_mem(val.to_num());
|
ctx.memr = vec.vec().get_memory(val.to_num());
|
||||||
if (!ctx.memr) {
|
if (!ctx.memr) {
|
||||||
die(report_out_of_range(val.to_num(), vec.vec().size()));
|
die(report_out_of_range(val.to_num(), vec.vec().size()));
|
||||||
return;
|
return;
|
||||||
|
@ -935,10 +935,10 @@ inline void vm::o_mcallv() {
|
||||||
}
|
}
|
||||||
auto& ref = vec.hash();
|
auto& ref = vec.hash();
|
||||||
const auto& str = val.str();
|
const auto& str = val.str();
|
||||||
ctx.memr = ref.get_mem(str);
|
ctx.memr = ref.get_memory(str);
|
||||||
if (!ctx.memr) {
|
if (!ctx.memr) {
|
||||||
ref.elems[str] = nil;
|
ref.elems[str] = nil;
|
||||||
ctx.memr = ref.get_mem(str);
|
ctx.memr = ref.get_memory(str);
|
||||||
}
|
}
|
||||||
} else if (vec.type==vm_map) {
|
} else if (vec.type==vm_map) {
|
||||||
if (val.type!=vm_str) {
|
if (val.type!=vm_str) {
|
||||||
|
@ -947,7 +947,7 @@ inline void vm::o_mcallv() {
|
||||||
}
|
}
|
||||||
auto& ref = vec.map();
|
auto& ref = vec.map();
|
||||||
const auto& str = val.str();
|
const auto& str = val.str();
|
||||||
ctx.memr = ref.get_mem(str);
|
ctx.memr = ref.get_memory(str);
|
||||||
if (!ctx.memr) {
|
if (!ctx.memr) {
|
||||||
die("cannot find symbol \"" + str + "\"");
|
die("cannot find symbol \"" + str + "\"");
|
||||||
}
|
}
|
||||||
|
@ -965,18 +965,18 @@ inline void vm::o_mcallh() {
|
||||||
}
|
}
|
||||||
const auto& str = const_string[imm[ctx.pc]];
|
const auto& str = const_string[imm[ctx.pc]];
|
||||||
if (hash.type==vm_map) {
|
if (hash.type==vm_map) {
|
||||||
ctx.memr = hash.map().get_mem(str);
|
ctx.memr = hash.map().get_memory(str);
|
||||||
if (!ctx.memr) {
|
if (!ctx.memr) {
|
||||||
die("cannot find symbol \"" + str + "\"");
|
die("cannot find symbol \"" + str + "\"");
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto& ref = hash.hash();
|
auto& ref = hash.hash();
|
||||||
ctx.memr = ref.get_mem(str);
|
ctx.memr = ref.get_memory(str);
|
||||||
// create a new key
|
// create a new key
|
||||||
if (!ctx.memr) {
|
if (!ctx.memr) {
|
||||||
ref.elems[str] = nil;
|
ref.elems[str] = nil;
|
||||||
ctx.memr = ref.get_mem(str);
|
ctx.memr = ref.get_memory(str);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
# result.nas
|
# result.nas
|
||||||
# ValKmjolnir 2021
|
# ValKmjolnir 2021
|
||||||
|
|
||||||
var Result=func(){
|
var new = func() {
|
||||||
var (ok, err, flag) = (nil, "", 1);
|
var (ok, err, flag) = (nil, "", 1);
|
||||||
return {
|
return {
|
||||||
Ok: func(val) {
|
Ok: func(val) {
|
||||||
|
@ -15,8 +15,9 @@ var Result=func(){
|
||||||
return me;
|
return me;
|
||||||
},
|
},
|
||||||
unwrap: func() {
|
unwrap: func() {
|
||||||
if(flag)
|
if (flag) {
|
||||||
die(err);
|
die(err);
|
||||||
|
}
|
||||||
return ok;
|
return ok;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -191,7 +191,6 @@ var ansi_escape_sequence=func(){
|
||||||
}
|
}
|
||||||
unix.sleep(0.01);
|
unix.sleep(0.01);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# enable unicode
|
# enable unicode
|
||||||
|
|
|
@ -0,0 +1,28 @@
|
||||||
|
import.module.libkey;
|
||||||
|
|
||||||
|
srand();
|
||||||
|
|
||||||
|
var chars = "abcdefghijklmnopqrstuvwxyz" ~
|
||||||
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" ~
|
||||||
|
"1234567890" ~
|
||||||
|
"!@#$%^&*()_+-=~`[]{}\\|'\";:,.<>/?";
|
||||||
|
chars = split("", chars);
|
||||||
|
|
||||||
|
print("\ec");
|
||||||
|
while(1) {
|
||||||
|
var key = libkey.nonblock();
|
||||||
|
if (key!=nil and chr(key)=="q") {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
var res = "\e[1;1H";
|
||||||
|
for(var i = 0; i<20; i+=1) {
|
||||||
|
for(var j = 0; j<40; j+=1) {
|
||||||
|
res ~= "\e[38;5;" ~ int(rand()*256) ~ ";1m";
|
||||||
|
res ~= chars[int(rand()*size(chars))];
|
||||||
|
res ~= "\e[0m ";
|
||||||
|
}
|
||||||
|
res ~= "\n";
|
||||||
|
}
|
||||||
|
print(res);
|
||||||
|
unix.sleep(1/30);
|
||||||
|
}
|
Loading…
Reference in New Issue