add native function abort(), assert()

This commit is contained in:
ValKmjolnir 2022-04-06 21:25:20 +08:00
parent 651ae4ef77
commit bf5737ecfd
5 changed files with 79 additions and 6 deletions

35
lib.nas
View File

@ -66,6 +66,11 @@ var floor=func(val){
return __builtin_floor(val);
}
# abort using std::abort
var abort=func(){
__builtin_abort();
}
# abs gets absolute number.
var abs=func(n){
return n>0?n:-n;
@ -255,6 +260,13 @@ var isa=func(object,class){
return 0;
}
# assert aborts when condition is not true
var assert=func(condition,message="assertion failed!"){
if(condition)
return 1;
die(message);
}
var io=
{
SEEK_SET:0,
@ -424,4 +436,25 @@ var M2IN=39.3701;
var M2NM=0.00054;
var MPS2KT=1.9438;
var NM2M=1852;
var R2D=180/math.pi;
var R2D=180/math.pi;
# functions that not supported in this runtime:
var bind=func(function,locals,outer_scope={}){
die("this runtime does not support bind");
}
var call=func(function,args=[],_me=nil,locals={},error=[]){
die("this runtime does not support call");
}
var caller=func(level=1){
die("this runtime does not support caller");
}
var closure=func(function,level=1){
die("this runtime uses \"vm_upval\" instead of \"vm_hash\" as the closure");
}
var compile=func(code,filename="<compile>"){
die("this runtime uses static code generator");
}

View File

@ -84,12 +84,11 @@ void execute(const std::string& file,const uint32_t cmd)
parse.compile(lexer);
// linker gets parser's ast and load import files to this ast
linker.link(parse,file);
if(cmd&VM_ASTINFO)
parse.print();
// optimizer does simple optimization on ast
if(cmd&VM_OPTIMIZE)
optimize(parse.ast());
if(cmd&VM_ASTINFO)
parse.print();
// code generator gets parser's ast and linker's import file list to generate code
gen.compile(parse,linker);

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_abort);
nas_native(builtin_append);
nas_native(builtin_setsize);
nas_native(builtin_system);
@ -101,6 +102,7 @@ struct
} builtin[]=
{
{"__builtin_print", builtin_print },
{"__builtin_abort", builtin_abort },
{"__builtin_append", builtin_append },
{"__builtin_setsize", builtin_setsize },
{"__builtin_system", builtin_system },
@ -201,6 +203,11 @@ nasal_ref builtin_print(nasal_ref* local,nasal_gc& gc)
// generate return value
return nil;
}
nasal_ref builtin_abort(nasal_ref* local,nasal_gc& gc)
{
std::abort();
return nil;
}
nasal_ref builtin_append(nasal_ref* local,nasal_gc& gc)
{
nasal_ref vec=local[1];

View File

@ -66,6 +66,11 @@ var floor=func(val){
return __builtin_floor(val);
}
# abort using std::abort
var abort=func(){
__builtin_abort();
}
# abs gets absolute number.
var abs=func(n){
return n>0?n:-n;
@ -255,6 +260,13 @@ var isa=func(object,class){
return 0;
}
# assert aborts when condition is not true
var assert=func(condition,message="assertion failed!"){
if(condition)
return 1;
die(message);
}
var io=
{
SEEK_SET:0,
@ -424,4 +436,25 @@ var M2IN=39.3701;
var M2NM=0.00054;
var MPS2KT=1.9438;
var NM2M=1852;
var R2D=180/math.pi;
var R2D=180/math.pi;
# functions that not supported in this runtime:
var bind=func(function,locals,outer_scope={}){
die("this runtime does not support bind");
}
var call=func(function,args=[],_me=nil,locals={},error=[]){
die("this runtime does not support call");
}
var caller=func(level=1){
die("this runtime does not support caller");
}
var closure=func(function,level=1){
die("this runtime uses \"vm_upval\" instead of \"vm_hash\" as the closure");
}
var compile=func(code,filename="<compile>"){
die("this runtime uses static code generator");
}

View File

@ -192,7 +192,8 @@ var _md5=func(){
var C=0x98badcfe;
var D=0x10325476;
for(var i=0;i<size(res);i+=16){
res_size=size(res);
for(var i=0;i<res_size;i+=16){
var (f,a,b,c,d)=(0,A,B,C,D);
for(var j=0;j<64;j+=1){
f=functions[j](b,c,d);