This commit is contained in:
Valk Richard Li 2019-11-22 20:56:16 +08:00 committed by GitHub
parent 80ef21b0b9
commit b4468c6cc1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 65 additions and 13 deletions

View File

@ -478,11 +478,20 @@ var abstract_syntax_tree::call_identifier()
{ {
var place=i->children.front().calculation(); var place=i->children.front().calculation();
if(place.get_type()==__var_number) if(place.get_type()==__var_number)
temp=temp.get_array_member((int)place.get_number()); {
if(place.get_number()>=0)
temp=temp.get_array_member((int)place.get_number());
else
{
exit_type=__get_value_failure;
std::cout<<">>[Runtime-error] line"<<i->line<<": number shouldn't be less than 0 when calling an array."<<std::endl;
break;
}
}
else else
{ {
exit_type=__error_value_type; exit_type=__error_value_type;
std::cout<<">>[Runtime-error] line "<<line<<": "; std::cout<<">>[Runtime-error] line "<<i->line<<": ";
print_detail_token(i->type); print_detail_token(i->type);
std::cout<<": incorrect type \'"; std::cout<<": incorrect type \'";
print_scalar(temp.get_type()); print_scalar(temp.get_type());
@ -496,7 +505,7 @@ var abstract_syntax_tree::call_identifier()
if(temp.get_type()==__null_type) if(temp.get_type()==__null_type)
{ {
exit_type=__get_value_failure; exit_type=__get_value_failure;
std::cout<<">>[Runtime-error] line "<<line<<": cannot find a hash-member named \'"<<i->name<<"\' or this value is set to __null_type. detail: "; std::cout<<">>[Runtime-error] line "<<i->line<<": cannot find a hash-member named \'"<<i->name<<"\' or this value is set to __null_type. detail: ";
temp.print_var(); temp.print_var();
std::cout<<"."<<std::endl; std::cout<<"."<<std::endl;
break; break;
@ -516,7 +525,7 @@ var abstract_syntax_tree::call_identifier()
else else
{ {
exit_type=__error_value_type; exit_type=__error_value_type;
std::cout<<">>[Runtime-error] line "<<line<<": "; std::cout<<">>[Runtime-error] line "<<i->line<<": ";
print_detail_token(i->type); print_detail_token(i->type);
std::cout<<": incorrect type \'"; std::cout<<": incorrect type \'";
print_scalar(temp.get_type()); print_scalar(temp.get_type());
@ -555,11 +564,20 @@ var* abstract_syntax_tree::get_var_addr()
{ {
var place=i->children.front().calculation(); var place=i->children.front().calculation();
if(place.get_type()==__var_number) if(place.get_type()==__var_number)
addr=addr->get_array_member_addr((int)place.get_number()); {
if(place.get_number()>=0)
addr=addr->get_array_member_addr((int)place.get_number());
else
{
exit_type=__get_value_failure;
std::cout<<">>[Runtime-error] line"<<i->line<<": number shouldn't be less than 0 when calling an array."<<std::endl;
break;
}
}
else else
{ {
exit_type=__error_value_type; exit_type=__error_value_type;
std::cout<<">>[Runtime-error] line "<<line<<": "; std::cout<<">>[Runtime-error] line "<<i->line<<": ";
print_detail_token(i->type); print_detail_token(i->type);
std::cout<<": incorrect type \'"; std::cout<<": incorrect type \'";
print_scalar(addr->get_type()); print_scalar(addr->get_type());
@ -573,20 +591,20 @@ var* abstract_syntax_tree::get_var_addr()
if(addr->get_type()==__null_type) if(addr->get_type()==__null_type)
{ {
exit_type=__get_value_failure; exit_type=__get_value_failure;
std::cout<<">>[Runtime-error] line "<<line<<": cannot find a hash-member named \'"<<i->name<<"\'."<<std::endl; std::cout<<">>[Runtime-error] line "<<i->line<<": cannot find a hash-member named \'"<<i->name<<"\'."<<std::endl;
break; break;
} }
} }
else if(i->type==__call_function && addr->get_type()==__var_function) else if(i->type==__call_function && addr->get_type()==__var_function)
{ {
exit_type=__error_value_type; exit_type=__error_value_type;
std::cout<<">>[Runtime-error] line "<<line<<": function-returned value cannot be assigned."<<std::endl; std::cout<<">>[Runtime-error] line "<<i->line<<": function-returned value cannot be assigned."<<std::endl;
break; break;
} }
else else
{ {
exit_type=__error_value_type; exit_type=__error_value_type;
std::cout<<">>[Runtime-error] line "<<line<<": "; std::cout<<">>[Runtime-error] line "<<i->line<<": ";
print_detail_token(i->type); print_detail_token(i->type);
std::cout<<": incorrect type \'"; std::cout<<": incorrect type \'";
print_scalar(addr->get_type()); print_scalar(addr->get_type());

View File

@ -5,16 +5,16 @@
reserve words are those below reserve words are those below
and they are also the reserve words of nasal and they are also the reserve words of nasal
*/ */
std::string reserve_word[15]= std::string reserve_word[14]=
{ {
"for","foreach","forindex","while", "for","foreach","forindex","while",
"var","func","break","continue","return", "var","func","break","continue","return",
"if","else","elsif","nil","and","or" "if","else","elsif","and","or"
}; };
int is_reserve_word(std::string str) int is_reserve_word(std::string str)
{ {
for(int i=0;i<15;++i) for(int i=0;i<14;++i)
if(reserve_word[i]==str) if(reserve_word[i]==str)
return __reserve_word; return __reserve_word;
return __token_identifier; return __token_identifier;
@ -111,6 +111,11 @@ class resource_file
std::cout<<">>[Resource] lack lib file: lib/io.nas ."<<std::endl; std::cout<<">>[Resource] lack lib file: lib/io.nas ."<<std::endl;
else else
input_lib_file(lib_name); input_lib_file(lib_name);
lib_name="lib/basics.nas";
if(access("lib/basics.nas",0))
std::cout<<">>[Resource] lack lib file: lib/basics.nas ."<<std::endl;
else
input_lib_file(lib_name);
return; return;
} }
void input_lib_file(std::string filename) void input_lib_file(std::string filename)

22
balloon/lib/basics.nas Normal file
View File

@ -0,0 +1,22 @@
# this file includes functions:
# append
# subvec
var append(vector,elements...)
{
return __call_special_inline_function(vector,elements);
};
var subvec(vector,begin,length)
{
var new_vector=[];
for(var i=begin;i<begin+length;i+=1)
{
append(new_vector,vector[i]);
}
return new_vector;
};
var int(value)
{
return __call_Cpp_int(value);
};

View File

@ -1,7 +1,14 @@
# the function print is written by Cpp: std::cout # the function print is written by Cpp: std::cout
# so the code is fake XD but the definition is of great use # the function input is written by Cpp: std::cin
# so the code is fake XD but the definitions are of great use
# the definitions are set to avoid redefinition
var print=func(args...) var print=func(args...)
{ {
return __call_cpp_ostream_std_cout(args); return __call_cpp_ostream_std_cout(args);
};
var input=func()
{
return __call_cpp_istream_std_cin();
}; };