From bf5737ecfd789b8f96fcca80b18e42a334c5566f Mon Sep 17 00:00:00 2001 From: ValKmjolnir Date: Wed, 6 Apr 2022 21:25:20 +0800 Subject: [PATCH] add native function abort(), assert() --- lib.nas | 35 ++++++++++++++++++++++++++++++++++- main.cpp | 5 ++--- nasal_builtin.h | 7 +++++++ stl/lib.nas | 35 ++++++++++++++++++++++++++++++++++- test/md5.nas | 3 ++- 5 files changed, 79 insertions(+), 6 deletions(-) diff --git a/lib.nas b/lib.nas index a3054ae..cad887a 100644 --- a/lib.nas +++ b/lib.nas @@ -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; \ No newline at end of file +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=""){ + die("this runtime uses static code generator"); +} \ No newline at end of file diff --git a/main.cpp b/main.cpp index 244ba54..8b4aa43 100644 --- a/main.cpp +++ b/main.cpp @@ -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); diff --git a/nasal_builtin.h b/nasal_builtin.h index da5f205..14829ef 100644 --- a/nasal_builtin.h +++ b/nasal_builtin.h @@ -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]; diff --git a/stl/lib.nas b/stl/lib.nas index a3054ae..cad887a 100644 --- a/stl/lib.nas +++ b/stl/lib.nas @@ -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; \ No newline at end of file +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=""){ + die("this runtime uses static code generator"); +} \ No newline at end of file diff --git a/test/md5.nas b/test/md5.nas index 08c63ea..9ba6e7b 100644 --- a/test/md5.nas +++ b/test/md5.nas @@ -192,7 +192,8 @@ var _md5=func(){ var C=0x98badcfe; var D=0x10325476; - for(var i=0;i