improve code & add new test file

This commit is contained in:
ValKmjolnir 2023-10-20 00:24:17 +08:00
parent 54317a39a7
commit 7f8a0b6445
7 changed files with 74 additions and 52 deletions

View File

@ -35,8 +35,7 @@ bool is_powerpc();
bool is_superh();
// virtual machine stack depth
// both global depth and value stack depth
// virtual machine stack depth, both global depth and value stack depth
const u32 STACK_DEPTH = 4096;
f64 hex2f(const char*);

View File

@ -2,7 +2,7 @@
namespace nasal {
var nas_vec::get_val(const i32 n) {
var nas_vec::get_value(const i32 n) {
i32 size = elems.size();
if (n<-size || n>=size) {
return var::none();
@ -10,7 +10,7 @@ var nas_vec::get_val(const i32 n) {
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();
if (n<-size || n>=size) {
return nullptr;
@ -33,7 +33,7 @@ std::ostream& operator<<(std::ostream& out, nas_vec& vec) {
return out;
}
var nas_hash::get_val(const std::string& key) {
var nas_hash::get_value(const std::string& key) {
if (elems.count(key)) {
return elems.at(key);
} else if (!elems.count("parents")) {
@ -46,7 +46,7 @@ var nas_hash::get_val(const std::string& key) {
}
for(auto& i : val.vec().elems) {
if (i.type==vm_hash) {
ret = i.hash().get_val(key);
ret = i.hash().get_value(key);
}
if (ret.type!=vm_none) {
return ret;
@ -55,7 +55,7 @@ var nas_hash::get_val(const std::string& key) {
return ret;
}
var* nas_hash::get_mem(const std::string& key) {
var* nas_hash::get_memory(const std::string& key) {
if (elems.count(key)) {
return &elems.at(key);
} else if (!elems.count("parents")) {
@ -68,7 +68,7 @@ var* nas_hash::get_mem(const std::string& key) {
}
for(auto& i : val.vec().elems) {
if (i.type==vm_hash) {
addr = i.hash().get_mem(key);
addr = i.hash().get_memory(key);
}
if (addr) {
return addr;
@ -160,14 +160,14 @@ std::ostream& operator<<(std::ostream& out, const nas_co& co) {
return out;
}
var nas_map::get_val(const std::string& key) {
var nas_map::get_value(const std::string& key) {
if (mapper.count(key)) {
return *mapper.at(key);
}
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)) {
return mapper.at(key);
}

View File

@ -101,25 +101,23 @@ public:
struct nas_vec {
std::vector<var> elems;
// mark if this is printed, avoid stackoverflow
bool printed;
// mark if this is printed, avoid stack overflow
bool printed = false;
nas_vec():printed(false) {}
usize size() const {return elems.size();}
var get_val(const i32);
var* get_mem(const i32);
var get_value(const i32);
var* get_memory(const i32);
};
struct nas_hash {
std::unordered_map<std::string, var> elems;
// mark if this is printed, avoid stackoverflow
bool printed;
// mark if this is printed, avoid stack overflow
bool printed = false;
nas_hash(): printed(false) {}
usize size() const {return elems.size();}
var get_val(const std::string&);
var* get_mem(const std::string&);
var get_value(const std::string&);
var* get_memory(const std::string&);
};
struct nas_func {
@ -180,9 +178,7 @@ public:
void clear();
public:
const std::string& get_ghost_name() const {
return type_name;
}
const auto& get_ghost_name() const {return type_name;}
};
struct context {
@ -220,13 +216,12 @@ struct nas_map {
bool printed = false;
std::unordered_map<std::string, var*> mapper;
nas_map() {}
void clear() {
mapper.clear();
}
var get_val(const std::string&);
var* get_mem(const std::string&);
var get_value(const std::string&);
var* get_memory(const std::string&);
};
struct nas_val {

View File

@ -612,7 +612,7 @@ inline void vm::o_callv() {
var val = ctx.top[0];
var vec = (--ctx.top)[0];
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) {
die(report_out_of_range(val.to_num(), vec.vec().size()));
return;
@ -622,7 +622,7 @@ inline void vm::o_callv() {
die("must use string as the key but get "+type_name_string(val));
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) {
die(report_key_not_found(val.str(), vec.hash()));
return;
@ -645,7 +645,7 @@ inline void vm::o_callv() {
die("must use string as the key but get "+type_name_string(val));
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) {
die("cannot find symbol \""+val.str()+"\"");
return;
@ -663,7 +663,7 @@ inline void vm::o_callvi() {
return;
}
// 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) {
die(report_out_of_range(imm[ctx.pc], val.vec().size()));
return;
@ -678,9 +678,9 @@ inline void vm::o_callh() {
}
const auto& str = const_string[imm[ctx.pc]];
if (val.type==vm_hash) {
ctx.top[0] = val.hash().get_val(str);
ctx.top[0] = val.hash().get_value(str);
} else {
ctx.top[0] = val.map().get_val(str);
ctx.top[0] = val.map().get_value(str);
}
if (ctx.top[0].type==vm_none) {
val.type==vm_hash?
@ -854,7 +854,7 @@ inline void vm::o_slcend() {
inline void vm::o_slc() {
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) {
die(report_out_of_range(val.to_num(), ctx.top[-1].vec().size()));
return;
@ -923,7 +923,7 @@ inline void vm::o_mcallv() {
var val = ctx.top[0]; // index
var vec = (--ctx.top)[0]; // mcall vector, reserved on stack to avoid gc
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) {
die(report_out_of_range(val.to_num(), vec.vec().size()));
return;
@ -935,10 +935,10 @@ inline void vm::o_mcallv() {
}
auto& ref = vec.hash();
const auto& str = val.str();
ctx.memr = ref.get_mem(str);
ctx.memr = ref.get_memory(str);
if (!ctx.memr) {
ref.elems[str] = nil;
ctx.memr = ref.get_mem(str);
ctx.memr = ref.get_memory(str);
}
} else if (vec.type==vm_map) {
if (val.type!=vm_str) {
@ -947,7 +947,7 @@ inline void vm::o_mcallv() {
}
auto& ref = vec.map();
const auto& str = val.str();
ctx.memr = ref.get_mem(str);
ctx.memr = ref.get_memory(str);
if (!ctx.memr) {
die("cannot find symbol \"" + str + "\"");
}
@ -965,18 +965,18 @@ inline void vm::o_mcallh() {
}
const auto& str = const_string[imm[ctx.pc]];
if (hash.type==vm_map) {
ctx.memr = hash.map().get_mem(str);
ctx.memr = hash.map().get_memory(str);
if (!ctx.memr) {
die("cannot find symbol \"" + str + "\"");
}
return;
}
auto& ref = hash.hash();
ctx.memr = ref.get_mem(str);
ctx.memr = ref.get_memory(str);
// create a new key
if (!ctx.memr) {
ref.elems[str] = nil;
ctx.memr = ref.get_mem(str);
ctx.memr = ref.get_memory(str);
}
}

View File

@ -1,22 +1,23 @@
# result.nas
# ValKmjolnir 2021
var Result=func(){
var (ok,err,flag)=(nil,"",1);
return{
Ok:func(val){
ok=val;
flag=0;
var new = func() {
var (ok, err, flag) = (nil, "", 1);
return {
Ok: func(val) {
ok = val;
flag = 0;
return me;
},
Err:func(info){
err=info;
flag=1;
Err: func(info) {
err = info;
flag = 1;
return me;
},
unwrap:func(){
if(flag)
unwrap: func() {
if (flag) {
die(err);
}
return ok;
}
};

View File

@ -191,7 +191,6 @@ var ansi_escape_sequence=func(){
}
unix.sleep(0.01);
}
}
# enable unicode

28
test/flush_screen.nas Normal file
View File

@ -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);
}