This commit is contained in:
Valk Richard Li 2020-10-25 22:15:49 +08:00
parent 67ae3505fb
commit 9a9277c505
4 changed files with 232 additions and 6 deletions

View File

@ -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=
{

View File

@ -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<source_len;++i)
{
tmp+=source[i];
int str_addr=nasal_vm.gc_alloc();
nasal_vm.gc_get(str_addr).set_type(vm_string);
nasal_vm.gc_get(str_addr).set_string(tmp);
ref_vec.add_elem(str_addr);
tmp="";
}
return ret_addr;
}
for(int i=0;i<source_len;++i)
{
bool check_delimeter=false;
@ -786,4 +795,44 @@ int nasal_runtime::builtin_type(int local_scope_addr)
}
return ret_addr;
}
int nasal_runtime::builtin_substr(int local_scope_addr)
{
int str_addr=in_builtin_find("str");
int begin_addr=in_builtin_find("begin");
int length_addr=in_builtin_find("length");
if(str_addr<0 || !in_builtin_check(str_addr,vm_string))
{
std::cout<<">> [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;i<begin+len;++i)
tmp+=str[i];
int ret_addr=nasal_vm.gc_alloc();
nasal_vm.gc_get(ret_addr).set_type(vm_string);
nasal_vm.gc_get(ret_addr).set_string(tmp);
return ret_addr;
}
#endif

View File

@ -102,6 +102,7 @@ private:
int builtin_import(int);
int builtin_die(int);
int builtin_type(int);
int builtin_substr(int);
void load_builtin_function();
public:
nasal_runtime();
@ -169,6 +170,7 @@ void nasal_runtime::load_builtin_function()
{"nasal_call_import", nasal_runtime::builtin_import},
{"nasal_call_builtin_die", nasal_runtime::builtin_die},
{"nasal_call_builtin_type", nasal_runtime::builtin_type},
{"nasal_call_builtin_substr", nasal_runtime::builtin_substr},
{"", NULL}
};
for(int i=0;builtin_func_table[i].func_pointer;++i)

171
test/lexer.nas Normal file
View File

@ -0,0 +1,171 @@
import("lib.nas");
var s=io.fin("lexer.nas");
s=split('',s);
var len=size(s);
var ptr=0;
var cmp=func(s1,s2)
{
var size1=size(s1);
var size2=size(s2);
if(size1!=size2)
return size1<size2;
for(var i=0;i<size1;i+=1)
if(s1[i]>s2[i])
return 0;
return 1;
}
var jump_note=func()
{
while(ptr<len and s[ptr]!='\n')
ptr+=1;
ptr+=1;
return;
}
var generate_id=func()
{
var tmp="";
while(ptr<len)
{
if('a'[0]<=s[ptr][0] and s[ptr][0]<='z'[0]
or 'A'[0]<=s[ptr][0] and s[ptr][0]<='Z'[0]
or s[ptr]=='_'
or '0'[0]<=s[ptr][0] and s[ptr][0]<='9'[0])
tmp~=s[ptr];
else
break;
ptr+=1;
}
return tmp;
}
var generate_str=func()
{
var tok_str="";
var mark=s[ptr];
ptr+=1;
while(ptr<len and s[ptr]!=mark)
{
if(s[ptr]=='\\')
{
ptr+=1;
if(s[ptr]=='a') tok_str~='\a';
elsif(s[ptr]=='b') tok_str~='\b';
elsif(s[ptr]=='f') tok_str~='\f';
elsif(s[ptr]=='n') tok_str~='\n';
elsif(s[ptr]=='r') tok_str~='\r';
elsif(s[ptr]=='t') tok_str~='\t';
elsif(s[ptr]=='v') tok_str~='\v';
elsif(s[ptr]=='?') tok_str~='?';
elsif(s[ptr]=='0') tok_str~='\0';
elsif(s[ptr]=='\\') tok_str~='\\';
elsif(s[ptr]=='\'') tok_str~='\'';
elsif(s[ptr]=='\"') tok_str~='\"';
else tok_str~=s[ptr];
}
else
tok_str~=s[ptr];
ptr+=1;
}
if(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<len and ('a'[0]<=s[ptr][0] and s[ptr][0]<='f'[0] or '0'[0]<=s[ptr][0] and s[ptr][0]<='9'[0]))
{
number~=s[ptr];
ptr+=1;
}
return num(number);
}
elsif(s[ptr]=='o')
{
ptr+=1;
while(ptr<len and ('0'[0]<=s[ptr][0] and s[ptr][0]<='7'[0]))
{
number~=s[ptr];
ptr+=1;
}
return num(number);
}
while(ptr<len and ('0'[0]<=s[ptr][0] and s[ptr][0]<='9'[0]))
{
number~=s[ptr];
ptr+=1;
}
if(s[ptr]=='.')
number~=s[ptr];
else
return num(number);
return num(number);
}
var generate_operator=func()
{
var tmp="";
if(s[ptr]=='+' or s[ptr]=='-' or s[ptr]=='~' or s[ptr]=='/' or s[ptr]=='*' or s[ptr]=='>' or s[ptr]=='<' or s[ptr]=='!' or s[ptr]=='=')
{
tmp=s[ptr];
ptr+=1;
if(ptr<len and s[ptr]=='=')
{
tmp~=s[ptr];
ptr+=1;
}
return tmp;
}
elsif(s[ptr]=='.')
{
if(ptr+2<len and s[ptr+1]=='.' and s[ptr+2]=='.')
{
tmp='...';
ptr+=3;
}
else
{
tmp='.';
ptr+=1;
}
return tmp;
}
elsif(s[ptr]!=' ' and s[ptr]!='\t' and s[ptr]!='\n' and s[ptr]!='\r' and s[ptr][0]>0)
tmp=s[ptr];
ptr+=1;
return tmp;
}
var cnt=0;
while(ptr<len)
{
if(s[ptr]=='#')
jump_note();
elsif('a'[0]<=s[ptr][0] and s[ptr][0]<='z'[0] or 'A'[0]<=s[ptr][0] and s[ptr][0]<='Z'[0] or s[ptr]=='_')
print("(",cnt+=1," | ",generate_id(),")");
elsif(s[ptr]=='\'' or s[ptr]=='\"')
print("(",cnt+=1," | \"",generate_str(),"\")");
elsif('0'[0]<=s[ptr][0] and s[ptr][0]<='9'[0])
print("(",cnt+=1," | ",generate_number(),")");
else
{
var tmp=generate_operator();
if(size(tmp))
print("(",cnt+=1," | ",tmp,")");
}
if(ptr>=len)
break;
}