This commit is contained in:
Valk Richard Li 2020-01-18 21:31:47 +08:00 committed by GitHub
parent 1c78d339a1
commit 52aef836b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 105 additions and 18 deletions

View File

@ -8,7 +8,16 @@ std::string command;
int main() int main()
{ {
std::cout<<">> Nasal interpreter by github:ValKmjolnir"<<std::endl; #ifdef _WIN32
std::cout<<">>[system] Windows system."<<std::endl;
#endif
#ifdef _linux_
std::cout<<">>[system] Linux system."<<std::endl;
#endif
#ifdef TARGET_OS_MAC
std::cout<<">>[system] MacOS system."<<std::endl;
#endif
std::cout<<">> Nasal interpreter ver 2.0: https://github.com/ValKmjolnir/Nasal-Interpreter"<<std::endl;
std::cout<<">> Input \"help\" to get help ."<<std::endl; std::cout<<">> Input \"help\" to get help ."<<std::endl;
while(1) while(1)
{ {
@ -32,15 +41,12 @@ int main()
else if(command=="cls") else if(command=="cls")
{ {
#ifdef _WIN32 #ifdef _WIN32
#pragma message("windows system detected.")
system("cls"); system("cls");
#endif #endif
#ifdef _linux_ #ifdef _linux_
#pragma message("linux system detected.")
system("clear"); system("clear");
#endif #endif
#ifdef TARGET_OS_MAC #ifdef TARGET_OS_MAC
#pragma message("macOS detected.")
system("clear"); system("clear");
#endif #endif
} }

View File

@ -53,6 +53,7 @@ enum parse_token_type
__parameters, __parameters,
__vector,__hash, __vector,__hash,
__hash_member, __hash_member,
__sub_vector,
__call_function,__call_vector,__call_hash, __call_function,__call_vector,__call_hash,
__normal_statement_block, __normal_statement_block,
__definition,__assignment, __definition,__assignment,
@ -130,6 +131,7 @@ void print_parse_token(int type)
case __vector: context="vector"; break; case __vector: context="vector"; break;
case __hash: context="hash"; break; case __hash: context="hash"; break;
case __hash_member: context="hash_member"; break; case __hash_member: context="hash_member"; break;
case __sub_vector: context="num:num"; break;
case __call_function: context="call_func"; break; case __call_function: context="call_func"; break;
case __call_vector: context="call_vector"; break; case __call_vector: context="call_vector"; break;
case __call_hash: context="call_hash"; break; case __call_hash: context="call_hash"; break;
@ -167,6 +169,8 @@ enum parse_error_type
hash_gen_lack_id, // lack identifier or string when generating a hash hash_gen_lack_id, // lack identifier or string when generating a hash
hash_gen_lack_colon, // lack ':' when generating a hash hash_gen_lack_colon, // lack ':' when generating a hash
hash_gen_lack_end, // lack ',' or '}' when generating a hash hash_gen_lack_end, // lack ',' or '}' when generating a hash
ternary_operator_lack_colon, // lack ':'
}; };
void print_parse_error(int error_type,int line,int error_token_type=__stack_end) void print_parse_error(int error_type,int line,int error_token_type=__stack_end)
@ -230,6 +234,11 @@ void print_parse_error(int error_type,int line,int error_token_type=__stack_end)
print_parse_token(error_token_type); print_parse_token(error_token_type);
std::cout<<"\' ."<<std::endl; std::cout<<"\' ."<<std::endl;
break; break;
case ternary_operator_lack_colon:
std::cout<<error_info_head<<line<<": expect a \':\' here but get \'";
print_parse_token(error_token_type);
std::cout<<"\' ."<<std::endl;
break;
default: default:
std::cout<<error_info_head<<line<<": unknown parse error. error id: other_type."<<std::endl;break; std::cout<<error_info_head<<line<<": unknown parse error. error id: other_type."<<std::endl;break;
} }

View File

@ -60,12 +60,7 @@ void nasal_parse::print_detail_token()
std::cout<<" "; std::cout<<" ";
tmp.pop(); tmp.pop();
if(!tmp.empty() && tmp.top().type==__right_brace) if(!tmp.empty() && tmp.top().type==__right_brace)
{ space.pop_back();
std::string str="";
for(int i=0;i<space.length()-1;++i)
str+=space[i];
space=str;
}
} }
std::cout<<std::endl; std::cout<<std::endl;
return; return;
@ -244,7 +239,24 @@ abstract_syntax_tree nasal_parse::calculation()
calc_node=tmp_node; calc_node=tmp_node;
this->get_token(); this->get_token();
} }
this->push_token(); if(this_token.type==__ques_mark)
{
// <expr> '?' <expr> ';' <expr>
tmp_node.set_node_line(this_token.line);
tmp_node.set_node_type(__ques_mark);
tmp_node.add_children(calc_node);
tmp_node.add_children(calculation());
this->get_token();
if(this_token.type!=__colon)
{
++error;
print_parse_error(ternary_operator_lack_colon,this_token.line,this_token.type);
}
tmp_node.add_children(calculation());
calc_node=tmp_node;
}
else
this->push_token();
return calc_node; return calc_node;
} }
@ -376,11 +388,13 @@ abstract_syntax_tree nasal_parse::scalar_generate()
this->push_token(); this->push_token();
this->push_token(); this->push_token();
scalar_node=function_generate(); scalar_node=function_generate();
// function
} }
else else
{ {
scalar_node.set_node_type(__id); scalar_node.set_node_type(__id);
scalar_node.set_var_name(this_token.str); scalar_node.set_var_name(this_token.str);
// func id
} }
break; break;
default: default:
@ -393,25 +407,66 @@ abstract_syntax_tree nasal_parse::scalar_generate()
{ {
if(this_token.type==__left_curve) if(this_token.type==__left_curve)
{ {
// call function
abstract_syntax_tree call_func_node;
call_func_node.set_node_line(this_token.line);
call_func_node.set_node_type(__call_function);
} }
else if(this_token.type==__left_bracket) else if(this_token.type==__left_bracket)
{ {
abstract_syntax_tree call_vector_node; abstract_syntax_tree call_vector_node;
call_vector_node.set_node_line(this_token.line); call_vector_node.set_node_line(this_token.line);
call_vector_node.set_node_type(__call_vector); call_vector_node.set_node_type(__call_vector);
// call_vector_node.add_children(calculation()); // there are many kinds of ways to call a vector
// this->get_token(); // such as: id[0] id[0:12] id[-2:0] id[2:] id[4,3,1,5,2]
// if(this_token.type==__colon) abstract_syntax_tree tmp=calculation();
// calculation(); this->get_token();
// else if(this_token.type==__colon)
// this->push_token(); {
abstract_syntax_tree subvec_node;
this->get_token();
subvec_node.set_node_line(this_token.line);
subvec_node.set_node_type(__sub_vector);
subvec_node.add_children(tmp);
if(this_token.type!=__right_bracket)
{
this->push_token();
subvec_node.add_children(calculation());
}
else
this->push_token();
call_vector_node.add_children(subvec_node);
}
else if(this_token.type==__comma)
{
call_vector_node.add_children(tmp);
while(this_token.type!=__right_bracket)
{
call_vector_node.add_children(calculation());
this->get_token();
if(this_token.type!=__comma && this_token.type!=__right_bracket)
{
++error;
print_parse_error(call_vector_lack_bracket,this_token.line,this_token.type);
break;
}
}
this->push_token();
}
else if(this_token.type==__right_bracket)
{
this->push_token();
call_vector_node.add_children(tmp);
}
this->get_token(); this->get_token();
if(this_token.type!=__right_bracket) if(this_token.type!=__right_bracket)
{ {
++error; ++error;
print_parse_error(call_vector_lack_bracket,this_token.line,this_token.type); print_parse_error(call_vector_lack_bracket,this_token.line,this_token.type);
break;
} }
scalar_node.add_children(call_vector_node);
} }
else if(this_token.type==__dot) else if(this_token.type==__dot)
{ {
@ -479,6 +534,13 @@ abstract_syntax_tree nasal_parse::hash_generate()
print_parse_error(hash_gen_lack_end,this_token.line,this_token.type); print_parse_error(hash_gen_lack_end,this_token.line,this_token.type);
break; break;
} }
if(this_token.type==__comma)
{
this->get_token();
if(this_token.type!=__right_brace)
this->push_token();
// {name:scalar,}
}
hash_node.add_children(hash_member_node); hash_node.add_children(hash_member_node);
} }
} }
@ -505,6 +567,13 @@ abstract_syntax_tree nasal_parse::vector_generate()
print_parse_error(vector_gen_lack_end,this_token.line,this_token.type); print_parse_error(vector_gen_lack_end,this_token.line,this_token.type);
break; break;
} }
if(this_token.type==__comma)
{
this->get_token();
if(this_token.type!=__right_bracket)
this->push_token();
// [0,1,2,]
}
} }
} }
return vector_node; return vector_node;

View File

@ -43,6 +43,7 @@
| <additive_calc> | <additive_calc>
| <multive_calc> | <multive_calc>
| [('-' | '!')] <scalar> | [('-' | '!')] <scalar>
| <calculation> '?' <calculation> ':' <calculation>
; ;
<and_calc> =<or_calc> {<and> <or_calc>} ; <and_calc> =<or_calc> {<and> <or_calc>} ;
<or_calc> =<additive_calc> {<or> <additive_calc>} ; <or_calc> =<additive_calc> {<or> <additive_calc>} ;

View File

@ -12,9 +12,11 @@ nil;
[]; [];
{}; {};
[0,1,2,3,4,5][2]; # 2 [0,1,2,3,4,5][2]; # 2
[0,1,2,3,4,5][5,4,3,2+1][0:2][0]; # 5
{str:"hello"}.str; # "hello" {str:"hello"}.str; # "hello"
{str:"hello"}["str"]; # "hello" {str:"hello"}["str"]; # "hello"
{"str":"hello\"\"\n"}["str"]; # "hello" {"str":"hello\"\"\n"}["str"]; # "hello"
20? 1:0;
# normal scalar # normal scalar
var number_1=1; var number_1=1;