🚀 add native function println & add test/occupation.nas

This commit is contained in:
ValKmjolnir 2022-06-04 19:22:28 +08:00
parent 4e6cd82ccb
commit cb0fee04a9
7 changed files with 77 additions and 15 deletions

View File

@ -202,9 +202,7 @@ var values=func(hash){
# println has the same function as print.
# but it will output a '\n' after using print.
var println=func(elems...){
__builtin_print(elems);
elems=['\n'];
return __builtin_print(elems);
return __builtin_println(elems);
}
var isfunc=func(f){

View File

@ -34,6 +34,7 @@ test:nasal
-@ ./nasal -op -t -d -o test/md5compare.nas
-@ ./nasal -op -d test/module_test.nas
@ ./nasal -op -e test/nasal_test.nas
@ ./nasal -op -c test/occupation.nas
@ ./nasal -op -t -d test/pi.nas
@ ./nasal -op -t -d test/prime.nas
@ ./nasal -op -e test/qrcode.nas

View File

@ -10,6 +10,7 @@
// to add new builtin function, declare it here and write the definition below
#define nas_native(name) nasal_ref name(nasal_ref*,nasal_gc&)
nas_native(builtin_print);
nas_native(builtin_println);
nas_native(builtin_abort);
nas_native(builtin_append);
nas_native(builtin_setsize);
@ -114,6 +115,7 @@ struct
} builtin[]=
{
{"__builtin_print", builtin_print },
{"__builtin_println", builtin_println },
{"__builtin_abort", builtin_abort },
{"__builtin_append", builtin_append },
{"__builtin_setsize", builtin_setsize },
@ -205,13 +207,9 @@ struct
{nullptr, nullptr }
};
nasal_ref builtin_print(nasal_ref* local,nasal_gc& gc)
inline void print_core(std::vector<nasal_ref>& elems)
{
// get arguments
// local[0] is reserved for 'me'
nasal_ref vec=local[1];
// main process
for(auto& i:vec.vec().elems)
for(auto& i:elems)
switch(i.type)
{
case vm_none: std::cout<<"null"; break;
@ -224,10 +222,25 @@ nasal_ref builtin_print(nasal_ref* local,nasal_gc& gc)
case vm_obj: std::cout<<"<object>"; break;
case vm_co: std::cout<<"<coroutine>"; break;
}
}
nasal_ref builtin_print(nasal_ref* local,nasal_gc& gc)
{
// get arguments
// local[0] is reserved for 'me'
nasal_ref vec=local[1];
// main process
print_core(vec.vec().elems);
std::cout<<std::flush;
// generate return value
return nil;
}
nasal_ref builtin_println(nasal_ref* local,nasal_gc& gc)
{
nasal_ref vec=local[1];
print_core(vec.vec().elems);
std::cout<<std::endl;
return nil;
}
nasal_ref builtin_abort(nasal_ref* local,nasal_gc& gc)
{
std::abort();

View File

@ -202,9 +202,7 @@ var values=func(hash){
# println has the same function as print.
# but it will output a '\n' after using print.
var println=func(elems...){
__builtin_print(elems);
elems=['\n'];
return __builtin_print(elems);
return __builtin_println(elems);
}
var isfunc=func(f){

View File

@ -1,16 +1,16 @@
var source=[
"main.cpp ",
"nasal_err.h ",
"nasal_ast.h ",
"nasal_builtin.h ",
"nasal_codegen.h ",
"nasal_opt.h ",
"nasal_dbg.h ",
"nasal_err.h ",
"nasal_gc.h ",
"nasal_import.h ",
"nasal_lexer.h ",
"nasal_opt.h ",
"nasal_parse.h ",
"nasal_vm.h ",
"nasal_dbg.h ",
"nasal.h "
];
@ -55,6 +55,7 @@ var testfile=[
"test/md5compare.nas ",
"test/module_test.nas ",
"test/nasal_test.nas ",
"test/occupation.nas ",
"test/pi.nas ",
"test/prime.nas ",
"test/qrcode.nas ",

View File

@ -86,6 +86,7 @@ var filechecksum=func(){
"./test/md5compare.nas ",
"./test/module_test.nas ",
"./test/nasal_test.nas ",
"./test/occupation.nas ",
"./test/pi.nas ",
"./test/prime.nas ",
"./test/qrcode.nas ",

50
test/occupation.nas Normal file
View File

@ -0,0 +1,50 @@
var cpu_stat=func(){
var cpu=split("\n",io.fin("/proc/stat"))[0];
cpu=split(" ",cpu);
cpu={
name:cpu[0],
user:cpu[1],
nice:cpu[2],
system:cpu[3],
idle:cpu[4],
iowait:cpu[5],
irq:cpu[6],
softirq:cpu[7],
};
return cpu;
}
var cpu_occupation=func(){
var cpu0=cpu_stat();
unix.sleep(1);
var cpu1=cpu_stat();
var t0=cpu0.user+cpu0.nice+cpu0.system+cpu0.idle+cpu0.iowait+cpu0.irq+cpu0.softirq;
var t1=cpu1.user+cpu1.nice+cpu1.system+cpu1.idle+cpu1.iowait+cpu1.irq+cpu1.softirq;
var interval=cpu1.idle-cpu0.idle;
return t0==t1?0:(1-interval/(t1-t0))*100;
}
var mem_occupation=func(){
var meminfo=split("\n",io.fin("/proc/meminfo"));
var mem_res={};
forindex(var i;meminfo){
var tmp=split(" ",meminfo[i])[0:1];
tmp[0]=substr(tmp[0],0,size(tmp[0])-1);
mem_res[tmp[0]]=num(tmp[1]);
}
return mem_res;
}
func(){
if(os.platform()=="windows"){
println("haven't supported yet.");
return;
}
while(1){
var mem=mem_occupation();
var mem_occ=(mem.MemTotal-mem.MemFree)/mem.MemTotal*100;
var cpu_occ=cpu_occupation();
println("CPU occupation(%) : ",cpu_occ>90?"\e[91m":"\e[32m",cpu_occ,"\e[0m");
println("Memory total(GB) : \e[36m",mem.MemTotal/1024/1024,"\e[0m");
println("Memory free(GB) : \e[36m",mem.MemFree/1024/1024,"\e[0m");
println("Memory occupation(%): ",mem_occ>30?"\e[91m":"\e[32m",mem_occ,"\e[0m");
}
}();