📝 format
This commit is contained in:
parent
94d0ce9c2d
commit
985dee8001
|
@ -317,41 +317,4 @@ std::ostream& operator<<(std::ostream& out, var& ref) {
|
|||
return out;
|
||||
}
|
||||
|
||||
bool var::object_check(const std::string& name) const {
|
||||
return is_ghost() && ghost().type_name == name && ghost().pointer;
|
||||
}
|
||||
|
||||
var var::none() {
|
||||
return {vm_type::vm_none, static_cast<u64>(0)};
|
||||
}
|
||||
|
||||
var var::nil() {
|
||||
return {vm_type::vm_nil, static_cast<u64>(0)};
|
||||
}
|
||||
|
||||
var var::ret(u64 pc) {
|
||||
return {vm_type::vm_ret, pc};
|
||||
}
|
||||
|
||||
var var::cnt(i64 n) {
|
||||
return {vm_type::vm_cnt, n};
|
||||
}
|
||||
|
||||
var var::num(f64 n) {
|
||||
return {vm_type::vm_num, n};
|
||||
}
|
||||
|
||||
var var::gcobj(nas_val* p) {
|
||||
return {p->type, p};
|
||||
}
|
||||
|
||||
var var::addr(var* p) {
|
||||
return {vm_type::vm_addr, p};
|
||||
}
|
||||
|
||||
var nas_err(const std::string& error_function_name, const std::string& info) {
|
||||
std::cerr << "[vm] " << error_function_name << ": " << info << "\n";
|
||||
return var::none();
|
||||
}
|
||||
|
||||
}
|
|
@ -104,13 +104,27 @@ public:
|
|||
|
||||
public:
|
||||
// create new var object
|
||||
static var none();
|
||||
static var nil();
|
||||
static var ret(u64);
|
||||
static var cnt(i64);
|
||||
static var num(f64);
|
||||
static var gcobj(nas_val*);
|
||||
static var addr(var*);
|
||||
static var none() {
|
||||
return var(vm_type::vm_none, static_cast<u64>(0));
|
||||
}
|
||||
static var nil() {
|
||||
return var(vm_type::vm_nil, static_cast<u64>(0));
|
||||
}
|
||||
static var ret(u64 pc) {
|
||||
return var(vm_type::vm_ret, pc);
|
||||
}
|
||||
static var cnt(i64 n) {
|
||||
return var(vm_type::vm_cnt, n);
|
||||
}
|
||||
static var num(f64 n) {
|
||||
return var(vm_type::vm_num, n);
|
||||
}
|
||||
static var gcobj(nas_val* p) {
|
||||
return var(p->type, p);
|
||||
}
|
||||
static var addr(var* p) {
|
||||
return var(vm_type::vm_addr, p);
|
||||
}
|
||||
|
||||
public:
|
||||
// get value
|
||||
|
@ -162,7 +176,8 @@ public:
|
|||
// number and string can be translated to each other
|
||||
f64 to_num() const;
|
||||
std::string to_str();
|
||||
bool object_check(const std::string&) const;
|
||||
inline bool object_check(const std::string&) const;
|
||||
friend std::ostream& operator<<(std::ostream&, var&);
|
||||
};
|
||||
|
||||
struct nas_vec {
|
||||
|
@ -174,6 +189,7 @@ struct nas_vec {
|
|||
auto size() const { return elems.size(); }
|
||||
var get_value(const i32);
|
||||
var* get_memory(const i32);
|
||||
friend std::ostream& operator<<(std::ostream&, nas_vec&);
|
||||
};
|
||||
|
||||
struct nas_hash {
|
||||
|
@ -185,6 +201,7 @@ struct nas_hash {
|
|||
auto size() const { return elems.size(); }
|
||||
var get_value(const std::string&);
|
||||
var* get_memory(const std::string&);
|
||||
friend std::ostream& operator<<(std::ostream&, nas_hash&);
|
||||
};
|
||||
|
||||
struct nas_func {
|
||||
|
@ -206,6 +223,7 @@ struct nas_func {
|
|||
parameter_size(0), local_size(0),
|
||||
dynamic_parameter_name("") {}
|
||||
void clear();
|
||||
friend std::ostream& operator<<(std::ostream&, nas_func&);
|
||||
};
|
||||
|
||||
struct nas_upval {
|
||||
|
@ -250,6 +268,7 @@ public:
|
|||
~nas_ghost() { clear(); }
|
||||
void set(const std::string&, destructor, marker, void*);
|
||||
void clear();
|
||||
friend std::ostream& operator<<(std::ostream&, const nas_ghost&);
|
||||
|
||||
public:
|
||||
const auto& get_ghost_name() const { return type_name; }
|
||||
|
@ -290,6 +309,7 @@ struct nas_co {
|
|||
delete[] ctx.stack;
|
||||
}
|
||||
void clear();
|
||||
friend std::ostream& operator<<(std::ostream&, const nas_co&);
|
||||
};
|
||||
|
||||
struct nas_map {
|
||||
|
@ -304,21 +324,21 @@ public:
|
|||
|
||||
var get_value(const std::string&);
|
||||
var* get_memory(const std::string&);
|
||||
friend std::ostream& operator<<(std::ostream&, nas_map&);
|
||||
};
|
||||
|
||||
std::ostream& operator<<(std::ostream&, nas_vec&);
|
||||
std::ostream& operator<<(std::ostream&, nas_hash&);
|
||||
std::ostream& operator<<(std::ostream&, nas_func&);
|
||||
std::ostream& operator<<(std::ostream&, nas_map&);
|
||||
std::ostream& operator<<(std::ostream&, const nas_ghost&);
|
||||
std::ostream& operator<<(std::ostream&, const nas_co&);
|
||||
std::ostream& operator<<(std::ostream&, var&);
|
||||
|
||||
const var zero = var::num(0);
|
||||
const var one = var::num(1);
|
||||
const var nil = var::nil();
|
||||
|
||||
inline bool var::object_check(const std::string& name) const {
|
||||
return is_ghost() && ghost().type_name == name && ghost().pointer;
|
||||
}
|
||||
|
||||
// use to print error log and return error value
|
||||
var nas_err(const std::string&, const std::string&);
|
||||
static var nas_err(const std::string& func, const std::string& info) {
|
||||
std::cerr << "[vm] " << func << ": " << info << "\n";
|
||||
return var::none();
|
||||
}
|
||||
|
||||
}
|
|
@ -536,13 +536,13 @@ void vm::die(const std::string& str) {
|
|||
if (!ngc.cort) {
|
||||
// in main context, exit directly
|
||||
std::exit(1);
|
||||
} else {
|
||||
}
|
||||
|
||||
// in coroutine, shut down the coroutine and return to main context
|
||||
ctx.pc = 0; // mark coroutine 'dead'
|
||||
ngc.context_reserve(); // switch context to main
|
||||
ctx.top[0] = nil; // generate return value 'nil'
|
||||
}
|
||||
}
|
||||
|
||||
void vm::run(const codegen& gen,
|
||||
const linker& linker,
|
||||
|
|
|
@ -48,14 +48,15 @@ func() {
|
|||
func {
|
||||
var co = coroutine.create(func() {
|
||||
var (a, b) = coroutine.yield(a + b);
|
||||
println("coroutine.yield get ",a," ",b);
|
||||
println("[0] coroutine.yield get ", a, " ", b);
|
||||
(a, b) = coroutine.yield(a + b);
|
||||
println("coroutine.yield get ",a," ",b);
|
||||
println("[0] coroutine.yield get ", a, " ", b);
|
||||
return "end";
|
||||
});
|
||||
|
||||
for(var i = 0; i < 5; i += 1)
|
||||
println("coroutine.resume get ",coroutine.resume(co,i,i+1));
|
||||
println("[0] coroutine.resume get ", coroutine.resume(co, i, i + 1));
|
||||
print("\n");
|
||||
}();
|
||||
|
||||
# test crash in coroutines
|
||||
|
@ -63,15 +64,16 @@ var co=coroutine.create(func {
|
|||
var b = func() { b() }
|
||||
coroutine.yield(b);
|
||||
b();
|
||||
coroutine.yield(0);
|
||||
coroutine.yield(0); # unreachable
|
||||
});
|
||||
|
||||
println("coroutine yield: ",coroutine.resume(co));
|
||||
println("coroutine state:\e[32m ",coroutine.status(co),"\e[0m");
|
||||
println("coroutine error: ",coroutine.resume(co));
|
||||
println("coroutine state:\e[91m ",coroutine.status(co),"\e[0m");
|
||||
println("coroutine yield: ",coroutine.resume(co));
|
||||
println("coroutine state:\e[91m ",coroutine.status(co),"\e[0m");
|
||||
println("[1] coroutine yield: ", coroutine.resume(co));
|
||||
println("[1] coroutine state after yield:\e[32m ", coroutine.status(co), "\e[0m");
|
||||
println("[1] coroutine stackoverflow error: ", coroutine.resume(co));
|
||||
println("[1] coroutine state after error:\e[91m ", coroutine.status(co), "\e[0m");
|
||||
println("[1] coroutine yield after error: ", coroutine.resume(co));
|
||||
println("[1] coroutine state after error:\e[91m ", coroutine.status(co), "\e[0m");
|
||||
print("\n");
|
||||
|
||||
var co = coroutine.create(func {
|
||||
var a = 1;
|
||||
|
@ -82,13 +84,13 @@ var co = coroutine.create(func {
|
|||
coroutine.yield(b());
|
||||
});
|
||||
|
||||
println("coroutine yield: ",coroutine.resume(co));
|
||||
println("coroutine state:\e[32m ",coroutine.status(co),"\e[0m");
|
||||
println("coroutine error: ",coroutine.resume(co));
|
||||
println("coroutine state:\e[91m ",coroutine.status(co),"\e[0m");
|
||||
println("coroutine yield: ",coroutine.resume(co));
|
||||
println("coroutine state:\e[91m ",coroutine.status(co),"\e[0m");
|
||||
println("ok");
|
||||
println("[2] coroutine yield: ", coroutine.resume(co));
|
||||
println("[2] coroutine state:\e[32m ", coroutine.status(co), "\e[0m");
|
||||
println("[2] coroutine error: ", coroutine.resume(co));
|
||||
println("[2] coroutine state:\e[91m ", coroutine.status(co), "\e[0m");
|
||||
println("[2] coroutine yield: ", coroutine.resume(co));
|
||||
println("[2] coroutine state:\e[91m ", coroutine.status(co), "\e[0m");
|
||||
println("[2] ok\n");
|
||||
|
||||
# pressure test
|
||||
for(var t = 0; t < 10; t += 1) {
|
||||
|
@ -112,7 +114,8 @@ for(var t=0;t<10;t+=1) {
|
|||
print(" ", bar.bar(rate), " ",
|
||||
padding.leftpad(str(int(rate*100)),3), "% | ",
|
||||
str(1e3 * int(counter / tm.elapsedMSec())),
|
||||
" tasks/s \r");
|
||||
" tasks/s \r"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -121,5 +124,6 @@ for(var t=0;t<10;t+=1) {
|
|||
consumer();
|
||||
println(" ", bar.bar(1), " 100% | ",
|
||||
str(int(1e3 * counter / tm.elapsedMSec())),
|
||||
" tasks/s ");
|
||||
" tasks/s "
|
||||
);
|
||||
}
|
Loading…
Reference in New Issue