From 75baa07855aa1f2a82fa09b6c496a300f34e8c34 Mon Sep 17 00:00:00 2001 From: Valk Richard Li <48872266+ValKmjolnir@users.noreply.github.com> Date: Wed, 14 Aug 2019 21:29:23 +0800 Subject: [PATCH] Add all recognitions without OOP --- version0.3/nasal_parse.h | 395 ++++++++++++++++++++++++++++++--------- 1 file changed, 304 insertions(+), 91 deletions(-) diff --git a/version0.3/nasal_parse.h b/version0.3/nasal_parse.h index ca54c95..3367668 100644 --- a/version0.3/nasal_parse.h +++ b/version0.3/nasal_parse.h @@ -14,13 +14,16 @@ enum token_type __left_curve,__right_curve,// () __semi,// ; __comma,// , + __colon,// : __var,// var reserve word __func,// func reserve word __print,// print reserve word __identifier,__identifiers, __scalar,__scalars, + __hash_member,__hash_members, __statement,__statements, __function,//function(){} + __use_function, __definition,__assignment, __print_function,// print() __loop,__loop_continue,__loop_break,// for()while() continue; break; @@ -38,6 +41,9 @@ class parse public: std::stack parser; public: + void function_call_reduction(); + void hash_member_reduction(); + void hash_members_reduction(); void definition_reduction(); void assignment_reduction(); void identifier_reduction(); @@ -51,9 +57,9 @@ class parse void stack_set_empty(); }; -void parse::definition_reduction() +void parse::function_call_reduction() { - int tbl[5]; + int tbl[5]={0}; std::stack temp; for(int i=4;i>=0;--i) { @@ -63,26 +69,179 @@ void parse::definition_reduction() tbl[i]=temp.top().type; parser.pop(); } - if(tbl[0]==__var && tbl[1]==__identifier && tbl[2]==__equal && (tbl[3]==__scalar || tbl[3]==__identifier || tbl[3]==__function) && tbl[4]==__semi) + for(int i=0;i<5;++i) + { + if(temp.empty()) + break; + parser.push(temp.top()); + temp.pop(); + } + if(tbl[0]==__identifier && tbl[1]==__left_curve + && (tbl[2]==__identifier || tbl[2]==__identifiers || tbl[2]==__scalar || tbl[2]==__scalars) + && tbl[3]==__right_curve && tbl[4]==__semi) + { + parse_unit temp_parse_unit; + temp_parse_unit.type=__use_function; + temp_parse_unit.line=parser.top().line; + for(int i=0;i<5;++i) + parser.pop(); + parser.push(temp_parse_unit); + } + else if(tbl[1]==__identifier && tbl[2]==__left_curve && tbl[3]==__right_curve && tbl[4]==__semi) + { + parse_unit temp_parse_unit; + temp_parse_unit.type=__use_function; + temp_parse_unit.line=parser.top().line; + for(int i=0;i<4;++i) + parser.pop(); + parser.push(temp_parse_unit); + } + return; +} +void parse::hash_member_reduction() +{ + int tbl[3]={0}; + std::stack temp; + for(int i=2;i>=0;--i) + { + if(parser.empty()) + break; + temp.push(parser.top()); + tbl[i]=temp.top().type; + parser.pop(); + } + for(int i=0;i<3;++i) + { + if(temp.empty()) + break; + parser.push(temp.top()); + temp.pop(); + } + if(tbl[0]==__identifier && tbl[1]==__colon && (tbl[2]==__identifier || tbl[2]==__scalar)) + { + parse_unit temp_parse_unit; + temp_parse_unit.type=__hash_member; + temp_parse_unit.line=parser.top().line; + for(int i=0;i<3;++i) + parser.pop(); + parser.push(temp_parse_unit); + } + return; +} +void parse::hash_members_reduction() +{ + int tbl[3]={0}; + std::stack temp; + for(int i=2;i>=0;--i) + { + if(parser.empty()) + break; + temp.push(parser.top()); + tbl[i]=temp.top().type; + parser.pop(); + } + for(int i=0;i<3;++i) + { + if(temp.empty()) + break; + parser.push(temp.top()); + temp.pop(); + } + if((tbl[0]==__hash_member || tbl[0]==__hash_members) && tbl[1]==__comma && tbl[2]==__hash_member) + { + parse_unit temp_parse_unit; + temp_parse_unit.type=__hash_members; + temp_parse_unit.line=parser.top().line; + for(int i=0;i<3;++i) + parser.pop(); + parser.push(temp_parse_unit); + } + return; +} +void parse::definition_reduction() +{ + int tbl[7]={0}; + std::stack temp; + for(int i=6;i>=0;--i) + { + if(parser.empty()) + break; + temp.push(parser.top()); + tbl[i]=temp.top().type; + parser.pop(); + } + for(int i=0;i<7;++i) + { + if(temp.empty()) + break; + parser.push(temp.top()); + temp.pop(); + } + if(tbl[2]==__var && tbl[3]==__identifier && tbl[4]==__equal && (tbl[5]==__scalar || tbl[5]==__identifier) && tbl[6]==__semi) { parse_unit temp_parse_unit; temp_parse_unit.type=__definition; - temp_parse_unit.line=temp.top().line; + temp_parse_unit.line=parser.top().line; + for(int i=0;i<5;++i) + parser.pop(); + parser.push(temp_parse_unit); + } + else if(tbl[0]==__var && tbl[1]==__identifier && tbl[2]==__equal && tbl[3]==__left_bracket + && (tbl[4]==__identifier || tbl[4]==__identifiers || tbl[4]==__scalar || tbl[4]==__scalars) + && tbl[5]==__right_bracket && tbl[6]==__semi) + { + parse_unit temp_parse_unit; + temp_parse_unit.type=__definition; + temp_parse_unit.line=parser.top().line; + for(int i=0;i<7;++i) + parser.pop(); + parser.push(temp_parse_unit); + } + else if(tbl[1]==__var && tbl[2]==__identifier && tbl[3]==__equal && tbl[4]==__left_bracket + && tbl[5]==__right_bracket && tbl[6]==__semi) + { + parse_unit temp_parse_unit; + temp_parse_unit.type=__definition; + temp_parse_unit.line=parser.top().line; + for(int i=0;i<6;++i) + parser.pop(); + parser.push(temp_parse_unit); + } + else if(tbl[0]==__var && tbl[1]==__identifier && tbl[2]==__equal && tbl[3]==__left_brace + && (tbl[4]==__hash_member || tbl[4]==__hash_members) + && tbl[5]==__right_brace && tbl[6]==__semi) + { + parse_unit temp_parse_unit; + temp_parse_unit.type=__definition; + temp_parse_unit.line=parser.top().line; + for(int i=0;i<7;++i) + parser.pop(); + parser.push(temp_parse_unit); + } + else if(tbl[1]==__var && tbl[2]==__identifier && tbl[3]==__equal && tbl[4]==__left_brace + && tbl[5]==__right_brace && tbl[6]==__semi) + { + parse_unit temp_parse_unit; + temp_parse_unit.type=__definition; + temp_parse_unit.line=parser.top().line; + for(int i=0;i<6;++i) + parser.pop(); + parser.push(temp_parse_unit); + } + else if(tbl[3]==__var && tbl[4]==__identifier && tbl[5]==__equal && tbl[6]==__use_function) + { + parse_unit temp_parse_unit; + temp_parse_unit.type=__definition; + temp_parse_unit.line=parser.top().line; + for(int i=0;i<4;++i) + parser.pop(); parser.push(temp_parse_unit); } - else - for(int i=0;i<5;++i) - { - if(temp.empty()) - break; - parser.push(temp.top()); - temp.pop(); - } return; } void parse::assignment_reduction() { - int tbl[4]; + int tbl[4]={0}; std::stack temp; for(int i=3;i>=0;--i) { @@ -92,29 +251,40 @@ void parse::assignment_reduction() tbl[i]=temp.top().type; parser.pop(); } + for(int i=0;i<4;++i) + { + if(temp.empty()) + break; + parser.push(temp.top()); + temp.pop(); + } if(tbl[0]==__identifier && (tbl[1]==__equal || tbl[1]==__add_equal || tbl[1]==__sub_equal || tbl[1]==__mul_equal || tbl[1]==__div_equal || tbl[1] ==__link_equal) - && (tbl[2]==__scalar || tbl[2]==__identifier) - && tbl[3]==__semi) + && (tbl[2]==__scalar || tbl[2]==__identifier) && tbl[3]==__semi) { parse_unit temp_parse_unit; temp_parse_unit.type=__assignment; - temp_parse_unit.line=temp.top().line; + temp_parse_unit.line=parser.top().line; + for(int i=0;i<4;++i) + parser.pop(); + parser.push(temp_parse_unit); + } + else if(tbl[1]==__identifier + && (tbl[2]==__equal || tbl[2]==__add_equal || tbl[2]==__sub_equal || tbl[2]==__mul_equal || tbl[2]==__div_equal || tbl[2] ==__link_equal) + && tbl[3]==__use_function) + { + parse_unit temp_parse_unit; + temp_parse_unit.type=__assignment; + temp_parse_unit.line=parser.top().line; + for(int i=0;i<3;++i) + parser.pop(); parser.push(temp_parse_unit); } - else - for(int i=0;i<4;++i) - { - if(temp.empty()) - break; - parser.push(temp.top()); - temp.pop(); - } return; } void parse::identifier_reduction() { - int tbl[3]; + int tbl[3]={0}; std::stack temp; for(int i=2;i>=0;--i) { @@ -124,26 +294,27 @@ void parse::identifier_reduction() tbl[i]=temp.top().type; parser.pop(); } + for(int i=0;i<3;++i) + { + if(temp.empty()) + break; + parser.push(temp.top()); + temp.pop(); + } if((tbl[0]==__identifier || tbl[0]==__identifiers) && tbl[1]==__comma && tbl[2]==__identifier) { parse_unit temp_parse_unit; temp_parse_unit.type=__identifiers; - temp_parse_unit.line=temp.top().line; + temp_parse_unit.line=parser.top().line; + for(int i=0;i<3;++i) + parser.pop(); parser.push(temp_parse_unit); } - else - for(int i=0;i<3;++i) - { - if(temp.empty()) - break; - parser.push(temp.top()); - temp.pop(); - } return; } void parse::scalar_reduction() { - int tbl[3]; + int tbl[3]={0}; std::stack temp; for(int i=2;i>=0;--i) { @@ -153,26 +324,28 @@ void parse::scalar_reduction() tbl[i]=temp.top().type; parser.pop(); } - if((tbl[0]==__scalar || tbl[0]==__scalars || tbl[0]==__identifier || tbl[0]==__identifiers) && tbl[1]==__comma && tbl[2]==__scalar) + for(int i=0;i<3;++i) + { + if(temp.empty()) + break; + parser.push(temp.top()); + temp.pop(); + } + if(((tbl[0]==__scalar || tbl[0]==__scalars || tbl[0]==__identifier || tbl[0]==__identifiers) && tbl[1]==__comma && tbl[2]==__scalar) + || ((tbl[0]==__scalar || tbl[0]==__scalars) && tbl[1]==__comma && tbl[2]==__identifier)) { parse_unit temp_parse_unit; temp_parse_unit.type=__scalars; - temp_parse_unit.line=temp.top().line; + temp_parse_unit.line=parser.top().line; + for(int i=0;i<3;++i) + parser.pop(); parser.push(temp_parse_unit); } - else - for(int i=0;i<3;++i) - { - if(temp.empty()) - break; - parser.push(temp.top()); - temp.pop(); - } return; } void parse::print_reduction() { - int tbl[5]; + int tbl[5]={0}; std::stack temp; for(int i=4;i>=0;--i) { @@ -182,23 +355,33 @@ void parse::print_reduction() tbl[i]=temp.top().type; parser.pop(); } + for(int i=0;i<5;++i) + { + if(temp.empty()) + break; + parser.push(temp.top()); + temp.pop(); + } if(tbl[0]==__print && tbl[1]==__left_curve && (tbl[2]==__scalar || tbl[2]==__scalars || tbl[2]==__identifier || tbl[2]==__identifiers) && tbl[3]==__right_curve && tbl[4]==__semi) { parse_unit temp_parse_unit; temp_parse_unit.type=__print_function; - temp_parse_unit.line=temp.top().line; + temp_parse_unit.line=parser.top().line; + for(int i=0;i<5;++i) + parser.pop(); + parser.push(temp_parse_unit); + } + else if(tbl[1]==__print && tbl[2]==__left_curve && tbl[3]==__right_curve && tbl[4]==__semi) + { + parse_unit temp_parse_unit; + temp_parse_unit.type=__print_function; + temp_parse_unit.line=parser.top().line; + for(int i=0;i<4;++i) + parser.pop(); parser.push(temp_parse_unit); } - else - for(int i=0;i<5;++i) - { - if(temp.empty()) - break; - parser.push(temp.top()); - temp.pop(); - } return; } void parse::statement_reduction() @@ -217,7 +400,7 @@ void parse::statement_reduction() } void parse::statements_reduction() { - int tbl[2]; + int tbl[2]={0}; std::stack temp; for(int i=1;i>=0;--i) { @@ -227,29 +410,27 @@ void parse::statements_reduction() tbl[i]=temp.top().type; parser.pop(); } + for(int i=0;i<2;++i) + { + if(temp.empty()) + break; + parser.push(temp.top()); + temp.pop(); + } if((tbl[0]==__statement || tbl[0]==__statements) && tbl[1]==__statement) { parse_unit temp_parse_unit; temp_parse_unit.type=__statements; - temp_parse_unit.line=temp.top().line; + temp_parse_unit.line=parser.top().line; + for(int i=0;i<2;++i) + parser.pop(); parser.push(temp_parse_unit); } - else - for(int i=0;i<2;++i) - { - if(temp.empty()) - break; - parser.push(temp.top()); - temp.pop(); - } return; } void parse::function_reduction() { - int real_line=0; - if(!parser.empty()) - real_line=parser.top().line; - int tbl[10]; + int tbl[10]={0}; std::stack temp; for(int i=9;i>=0;--i) { @@ -259,33 +440,57 @@ void parse::function_reduction() tbl[i]=temp.top().type; parser.pop(); } - if(((tbl[0]==__var && tbl[1]==__identifier && tbl[2]==__equal && tbl[3]==__func && tbl[4]==__left_curve + for(int i=0;i<10;++i) + { + if(temp.empty()) + break; + parser.push(temp.top()); + temp.pop(); + } + if(tbl[0]==__var && tbl[1]==__identifier && tbl[2]==__equal && tbl[3]==__func && tbl[4]==__left_curve && (tbl[5]==__identifier || tbl[5]==__identifiers) && tbl[6]==__right_curve - && tbl[7]==__left_brace && (tbl[8]==__statement || tbl[8]==__statements) && tbl[9]==__right_brace)) - || ((tbl[1]==__var && tbl[2]==__identifier && tbl[3]==__equal && tbl[4]==__func && tbl[5]==__left_curve - && tbl[6]==__right_curve && tbl[7]==__left_brace - && (tbl[8]==__statement || tbl[8]==__statements) && tbl[9]==__right_brace)) - || ((tbl[1]==__var && tbl[2]==__identifier && tbl[3]==__equal && tbl[4]==__func && tbl[5]==__left_curve - && (tbl[6]==__identifier || tbl[6]==__identifiers) && tbl[7]==__right_curve - && tbl[8]==__left_brace && tbl[9]==__right_brace)) - || ((tbl[2]==__var && tbl[3]==__identifier && tbl[4]==__equal && tbl[5]==__func - && tbl[6]==__left_curve && tbl[7]==__right_curve - && tbl[8]==__left_brace && tbl[9]==__right_brace)) - ) + && tbl[7]==__left_brace && (tbl[8]==__statement || tbl[8]==__statements) && tbl[9]==__right_brace) { parse_unit temp_parse_unit; temp_parse_unit.type=__function; - temp_parse_unit.line=real_line; + temp_parse_unit.line=parser.top().line; + for(int i=0;i<10;++i) + parser.pop(); + parser.push(temp_parse_unit); + } + else if(tbl[1]==__var && tbl[2]==__identifier && tbl[3]==__equal && tbl[4]==__func && tbl[5]==__left_curve + && tbl[6]==__right_curve && tbl[7]==__left_brace + && (tbl[8]==__statement || tbl[8]==__statements) && tbl[9]==__right_brace) + { + parse_unit temp_parse_unit; + temp_parse_unit.type=__function; + temp_parse_unit.line=parser.top().line; + for(int i=0;i<9;++i) + parser.pop(); + parser.push(temp_parse_unit); + } + else if(tbl[1]==__var && tbl[2]==__identifier && tbl[3]==__equal && tbl[4]==__func && tbl[5]==__left_curve + && (tbl[6]==__identifier || tbl[6]==__identifiers) && tbl[7]==__right_curve + && tbl[8]==__left_brace && tbl[9]==__right_brace) + { + parse_unit temp_parse_unit; + temp_parse_unit.type=__function; + temp_parse_unit.line=parser.top().line; + for(int i=0;i<9;++i) + parser.pop(); + parser.push(temp_parse_unit); + } + else if(tbl[2]==__var && tbl[3]==__identifier && tbl[4]==__equal && tbl[5]==__func + && tbl[6]==__left_curve && tbl[7]==__right_curve + && tbl[8]==__left_brace && tbl[9]==__right_brace) + { + parse_unit temp_parse_unit; + temp_parse_unit.type=__function; + temp_parse_unit.line=parser.top().line; + for(int i=0;i<8;++i) + parser.pop(); parser.push(temp_parse_unit); } - else - for(int i=0;i<10;++i) - { - if(temp.empty()) - break; - parser.push(temp.top()); - temp.pop(); - } return; } @@ -310,7 +515,7 @@ void parse::parse_work(token_list& lexer) { temp_parse.type=__identifier; } - else if(temp->content==";" || temp->content=="," || temp->content=="=") + else if(temp->content==";" || temp->content=="," || temp->content=="=" || temp->content==":") { if(temp->content==";") temp_parse.type=__semi; @@ -318,6 +523,8 @@ void parse::parse_work(token_list& lexer) temp_parse.type=__comma; else if(temp->content=="=") temp_parse.type=__equal; + else if(temp->content==":") + temp_parse.type=__colon; } else if(temp->type==NUMBER || temp->type==STRING) { @@ -379,6 +586,7 @@ void parse::parse_work(token_list& lexer) identifier_reduction(); scalar_reduction(); print_reduction(); + function_call_reduction(); definition_reduction(); assignment_reduction(); if(parser.top().type==__definition) @@ -387,11 +595,16 @@ void parse::parse_work(token_list& lexer) std::cout<<"line "<