diff --git a/lib.nas b/lib.nas index fa46b87..2c5c070 100644 --- a/lib.nas +++ b/lib.nas @@ -90,6 +90,10 @@ var type=func(object) { return nasal_call_builtin_type(object); } +var substr=func(str,begin,length) +{ + return nasal_call_builtin_substr(str,begin,length); +} var io= { diff --git a/nasal_builtin.h b/nasal_builtin.h index 3b7011c..e71a2ac 100644 --- a/nasal_builtin.h +++ b/nasal_builtin.h @@ -267,18 +267,27 @@ int nasal_runtime::builtin_split(int local_scope_addr) std::string delimeter=nasal_vm.gc_get(delimeter_value_addr).get_string(); std::string source=nasal_vm.gc_get(string_value_addr).get_string(); int delimeter_len=delimeter.length(); - if(delimeter_len<1) - { - std::cout<<">> [runtime] builtin_split: delimeter's length must be greater than 0.\n"; - ++error; - return -1; - } int source_len=source.length(); int ret_addr=nasal_vm.gc_alloc(); nasal_vm.gc_get(ret_addr).set_type(vm_vector); nasal_vector& ref_vec=nasal_vm.gc_get(ret_addr).get_vector(); std::string tmp=""; + + if(!delimeter_len) + { + for(int i=0;i> [runtime] builtin_substr: cannot find \"str\" or wrong type(must be string).\n"; + ++error; + return -1; + } + if(begin_addr<0 || !in_builtin_check(begin_addr,vm_number)) + { + std::cout<<">> [runtime] builtin_substr: cannot find \"begin\" or wrong type(must be number).\n"; + ++error; + return -1; + } + if(length_addr<0 || !in_builtin_check(length_addr,vm_number)) + { + std::cout<<">> [runtime] builtin_substr: cannot find \"length\" or wrong type(must be number).\n"; + ++error; + return -1; + } + std::string str=nasal_vm.gc_get(str_addr).get_string(); + int begin=(int)nasal_vm.gc_get(begin_addr).get_number(); + int len=(int)nasal_vm.gc_get(length_addr).get_number(); + if(begin>=str.length() || begin+len>=str.length()) + { + std::cout<<">> [runtime] builtin_substr: index out of range.\n"; + ++error; + return -1; + } + std::string tmp=""; + for(int i=begin;is2[i]) + return 0; + return 1; +} + +var jump_note=func() +{ + while(ptr=len) + { + print("read eof when generating string."); + break; + } + ptr+=1; + return tok_str; +} + +var generate_number=func() +{ + var number=s[ptr]; + ptr+=1; + if(s[ptr]=='x') + { + ptr+=1; + while(ptr' or s[ptr]=='<' or s[ptr]=='!' or s[ptr]=='=') + { + tmp=s[ptr]; + ptr+=1; + if(ptr0) + tmp=s[ptr]; + ptr+=1; + return tmp; +} +var cnt=0; +while(ptr=len) + break; +} \ No newline at end of file