Add OOP detectors
This commit is contained in:
parent
75baa07855
commit
21b57f51f5
|
@ -253,6 +253,8 @@ void RunProcess(std::string &FileName)
|
|||
fin.close();
|
||||
ResourceProgram[0]=0;
|
||||
nasal_lexer.delete_all();
|
||||
nasal_var_stack.delete_all();
|
||||
nasal_func_stack.delete_all();
|
||||
InputFile(FileName);
|
||||
while(Syn!=SCANEND && Syn!=ERRORFOUND)
|
||||
{
|
||||
|
|
|
@ -15,19 +15,22 @@ enum token_type
|
|||
__semi,// ;
|
||||
__comma,// ,
|
||||
__colon,// :
|
||||
__dot,// .
|
||||
__var,// var reserve word
|
||||
__func,// func reserve word
|
||||
__print,// print reserve word
|
||||
__identifier,__identifiers,
|
||||
__scalar,__scalars,
|
||||
__hash_member,__hash_members,
|
||||
__array_search,__hash_search,
|
||||
__statement,__statements,
|
||||
__function,//function(){}
|
||||
__use_function,
|
||||
__definition,__assignment,
|
||||
__print_function,// print()
|
||||
__loop,__loop_continue,__loop_break,// for()while() continue; break;
|
||||
__choose// if else if else
|
||||
__choose,// if else if else
|
||||
__return,__func_return
|
||||
};
|
||||
|
||||
struct parse_unit
|
||||
|
@ -41,6 +44,10 @@ class parse
|
|||
public:
|
||||
std::stack<parse_unit> parser;
|
||||
public:
|
||||
void func_return_reduction();
|
||||
void class_function_reduction();
|
||||
void array_search_reduction();
|
||||
void hash_search_reduction();
|
||||
void function_call_reduction();
|
||||
void hash_member_reduction();
|
||||
void hash_members_reduction();
|
||||
|
@ -57,6 +64,163 @@ class parse
|
|||
void stack_set_empty();
|
||||
};
|
||||
|
||||
void parse::func_return_reduction()
|
||||
{
|
||||
int tbl[3]={0};
|
||||
std::stack<parse_unit> 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]==__return && (tbl[1]==__scalar || tbl[1]==__identifier || tbl[1]==__use_function) && tbl[2]==__semi)
|
||||
{
|
||||
parse_unit temp_parse_unit;
|
||||
temp_parse_unit.type=__func_return;
|
||||
temp_parse_unit.line=parser.top().line;
|
||||
for(int i=0;i<3;++i)
|
||||
parser.pop();
|
||||
parser.push(temp_parse_unit);
|
||||
}
|
||||
return;
|
||||
}
|
||||
void parse::class_function_reduction()
|
||||
{
|
||||
int tbl[10]={0};
|
||||
std::stack<parse_unit> temp;
|
||||
for(int i=9;i>=0;--i)
|
||||
{
|
||||
if(parser.empty())
|
||||
break;
|
||||
temp.push(parser.top());
|
||||
tbl[i]=temp.top().type;
|
||||
parser.pop();
|
||||
}
|
||||
for(int i=0;i<10;++i)
|
||||
{
|
||||
if(temp.empty())
|
||||
break;
|
||||
parser.push(temp.top());
|
||||
temp.pop();
|
||||
}
|
||||
if(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)
|
||||
{
|
||||
parse_unit temp_parse_unit;
|
||||
temp_parse_unit.type=__function;
|
||||
temp_parse_unit.line=parser.top().line;
|
||||
for(int i=0;i<7;++i)
|
||||
parser.pop();
|
||||
parser.push(temp_parse_unit);
|
||||
}
|
||||
else if(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<6;++i)
|
||||
parser.pop();
|
||||
parser.push(temp_parse_unit);
|
||||
}
|
||||
else if(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<6;++i)
|
||||
parser.pop();
|
||||
parser.push(temp_parse_unit);
|
||||
}
|
||||
else if(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<5;++i)
|
||||
parser.pop();
|
||||
parser.push(temp_parse_unit);
|
||||
}
|
||||
return;
|
||||
}
|
||||
void parse::array_search_reduction()
|
||||
{
|
||||
int tbl[4]={0};
|
||||
std::stack<parse_unit> temp;
|
||||
for(int i=3;i>=0;--i)
|
||||
{
|
||||
if(parser.empty())
|
||||
break;
|
||||
temp.push(parser.top());
|
||||
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]==__left_bracket
|
||||
&& (tbl[2]==__identifier || tbl[2]==__scalar || tbl[2]==__use_function)
|
||||
&& tbl[3]==__right_bracket)
|
||||
{
|
||||
parse_unit temp_parse_unit;
|
||||
temp_parse_unit.type=__identifier;
|
||||
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_search_reduction()
|
||||
{
|
||||
int tbl[3]={0};
|
||||
std::stack<parse_unit> 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]==__dot && (tbl[2]==__identifier || tbl[2]==__function))
|
||||
{
|
||||
parse_unit temp_parse_unit;
|
||||
temp_parse_unit.type=__identifier;
|
||||
temp_parse_unit.line=parser.top().line;
|
||||
for(int i=0;i<3;++i)
|
||||
parser.pop();
|
||||
parser.push(temp_parse_unit);
|
||||
}
|
||||
return;
|
||||
}
|
||||
void parse::function_call_reduction()
|
||||
{
|
||||
int tbl[5]={0};
|
||||
|
@ -117,7 +281,7 @@ void parse::hash_member_reduction()
|
|||
parser.push(temp.top());
|
||||
temp.pop();
|
||||
}
|
||||
if(tbl[0]==__identifier && tbl[1]==__colon && (tbl[2]==__identifier || tbl[2]==__scalar))
|
||||
if(tbl[0]==__identifier && tbl[1]==__colon && (tbl[2]==__identifier || tbl[2]==__scalar || tbl[2]==__function))
|
||||
{
|
||||
parse_unit temp_parse_unit;
|
||||
temp_parse_unit.type=__hash_member;
|
||||
|
@ -260,7 +424,7 @@ void parse::assignment_reduction()
|
|||
}
|
||||
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[2]==__use_function) && tbl[3]==__semi)
|
||||
{
|
||||
parse_unit temp_parse_unit;
|
||||
temp_parse_unit.type=__assignment;
|
||||
|
@ -363,7 +527,7 @@ void parse::print_reduction()
|
|||
temp.pop();
|
||||
}
|
||||
if(tbl[0]==__print && tbl[1]==__left_curve
|
||||
&& (tbl[2]==__scalar || tbl[2]==__scalars || tbl[2]==__identifier || tbl[2]==__identifiers)
|
||||
&& (tbl[2]==__scalar || tbl[2]==__scalars || tbl[2]==__identifier || tbl[2]==__identifiers || tbl[2]==__use_function)
|
||||
&& tbl[3]==__right_curve && tbl[4]==__semi)
|
||||
{
|
||||
parse_unit temp_parse_unit;
|
||||
|
@ -387,8 +551,8 @@ void parse::print_reduction()
|
|||
void parse::statement_reduction()
|
||||
{
|
||||
int top_type=parser.top().type;
|
||||
if(top_type==__definition || top_type==__assignment || top_type==__print_function
|
||||
|| top_type==__loop || top_type==__loop_continue || top_type==__loop_break || top_type==__choose)
|
||||
if(top_type==__definition || top_type==__assignment || top_type==__print_function || top_type==__use_function
|
||||
|| top_type==__loop || top_type==__loop_continue || top_type==__loop_break || top_type==__choose || top_type==__func_return)
|
||||
{
|
||||
parse_unit temp_parse_unit;
|
||||
temp_parse_unit.type=__statement;
|
||||
|
@ -498,11 +662,12 @@ void parse::parse_work(token_list& lexer)
|
|||
{
|
||||
parse_unit temp_parse;
|
||||
token_unit *temp=lexer.get_head();
|
||||
stack_set_empty();
|
||||
while(temp->next)
|
||||
{
|
||||
temp=temp->next;
|
||||
temp_parse.line=temp->line;
|
||||
if(temp->content=="var" || temp->content=="print" || temp->content=="func")
|
||||
if(temp->content=="var" || temp->content=="print" || temp->content=="func" || temp->content=="return" || temp->content=="nil")
|
||||
{
|
||||
if(temp->content=="var")
|
||||
temp_parse.type=__var;
|
||||
|
@ -510,12 +675,16 @@ void parse::parse_work(token_list& lexer)
|
|||
temp_parse.type=__print;
|
||||
else if(temp->content=="func")
|
||||
temp_parse.type=__func;
|
||||
else if(temp->content=="return")
|
||||
temp_parse.type=__return;
|
||||
else if(temp->content=="nil")
|
||||
temp_parse.type=__scalar;
|
||||
}
|
||||
else if(temp->type==IDENTIFIER)
|
||||
{
|
||||
temp_parse.type=__identifier;
|
||||
}
|
||||
else if(temp->content==";" || temp->content=="," || temp->content=="=" || temp->content==":")
|
||||
else if(temp->content==";" || temp->content=="," || temp->content=="=" || temp->content==":" || temp->content==".")
|
||||
{
|
||||
if(temp->content==";")
|
||||
temp_parse.type=__semi;
|
||||
|
@ -525,6 +694,8 @@ void parse::parse_work(token_list& lexer)
|
|||
temp_parse.type=__equal;
|
||||
else if(temp->content==":")
|
||||
temp_parse.type=__colon;
|
||||
else if(temp->content==".")
|
||||
temp_parse.type=__dot;
|
||||
}
|
||||
else if(temp->type==NUMBER || temp->type==STRING)
|
||||
{
|
||||
|
@ -585,8 +756,11 @@ void parse::parse_work(token_list& lexer)
|
|||
|
||||
identifier_reduction();
|
||||
scalar_reduction();
|
||||
array_search_reduction();
|
||||
hash_search_reduction();
|
||||
print_reduction();
|
||||
function_call_reduction();
|
||||
func_return_reduction();
|
||||
definition_reduction();
|
||||
assignment_reduction();
|
||||
if(parser.top().type==__definition)
|
||||
|
@ -597,10 +771,10 @@ void parse::parse_work(token_list& lexer)
|
|||
std::cout<<"line "<<parser.top().line<<": print()"<<std::endl;
|
||||
else if(parser.top().type==__use_function)
|
||||
std::cout<<"line "<<parser.top().line<<": call func()"<<std::endl;
|
||||
|
||||
statement_reduction();//must be put before statements
|
||||
statements_reduction();
|
||||
function_reduction();
|
||||
class_function_reduction();
|
||||
if(parser.top().type==__function)
|
||||
std::cout<<"line "<<parser.top().line<<": function()"<<std::endl;
|
||||
hash_member_reduction();
|
||||
|
|
Loading…
Reference in New Issue