update
This commit is contained in:
parent
170c47d7cc
commit
75948eb28b
|
@ -9,7 +9,7 @@ std::string command;
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
std::cout<<">> Nasal interpreter by github:ValKmjolnir"<<std::endl;
|
std::cout<<">> Nasal interpreter by github:ValKmjolnir"<<std::endl;
|
||||||
std::cout<<">> Input \"help\" to get help."<<std::endl;
|
std::cout<<">> Input \"help\" to get help ."<<std::endl;
|
||||||
while(1)
|
while(1)
|
||||||
{
|
{
|
||||||
std::cout<<">> ";
|
std::cout<<">> ";
|
||||||
|
@ -76,7 +76,19 @@ int main()
|
||||||
}
|
}
|
||||||
else if(command=="ast")
|
else if(command=="ast")
|
||||||
{
|
{
|
||||||
;
|
lexer.scanner(res.get_source());
|
||||||
|
lexer.generate_detail_token();
|
||||||
|
if(!lexer.get_error())
|
||||||
|
{
|
||||||
|
parser.get_token_list(lexer.get_detail_token());
|
||||||
|
parser.main_generate();
|
||||||
|
if(!parser.get_error())
|
||||||
|
parser.get_root().print_tree(1);
|
||||||
|
else
|
||||||
|
std::cout<<">>[Parse] error occurred,stop."<<std::endl;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
std::cout<<">>[Lexer] error occurred,stop."<<std::endl;
|
||||||
}
|
}
|
||||||
else if(command=="exit")
|
else if(command=="exit")
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -5,6 +5,7 @@ class nasal_parse
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
std::stack<token> parse;
|
std::stack<token> parse;
|
||||||
|
std::stack<token> checked;
|
||||||
token this_token;
|
token this_token;
|
||||||
int error;
|
int error;
|
||||||
int warning;
|
int warning;
|
||||||
|
@ -14,9 +15,15 @@ class nasal_parse
|
||||||
void print_detail_token();
|
void print_detail_token();
|
||||||
void get_token_list(std::list<token>&);
|
void get_token_list(std::list<token>&);
|
||||||
void get_token();
|
void get_token();
|
||||||
|
void push_token();
|
||||||
|
int get_error();
|
||||||
|
abstract_syntax_tree& get_root();
|
||||||
|
|
||||||
// abstract_syntax_tree generation
|
// abstract_syntax_tree generation
|
||||||
void main_generate();
|
void main_generate();
|
||||||
|
abstract_syntax_tree number_expr();
|
||||||
|
abstract_syntax_tree string_expr();
|
||||||
|
abstract_syntax_tree nil_expr();
|
||||||
};
|
};
|
||||||
|
|
||||||
void nasal_parse::print_detail_token()
|
void nasal_parse::print_detail_token()
|
||||||
|
@ -29,6 +36,8 @@ void nasal_parse::print_detail_token()
|
||||||
{
|
{
|
||||||
if(tmp.top().line!=line)
|
if(tmp.top().line!=line)
|
||||||
{
|
{
|
||||||
|
for(int i=line+1;i<tmp.top().line;++i)
|
||||||
|
std::cout<<std::endl<<i<<"\t";
|
||||||
line=tmp.top().line;
|
line=tmp.top().line;
|
||||||
std::cout<<std::endl<<line<<"\t"<<space;
|
std::cout<<std::endl<<line<<"\t"<<space;
|
||||||
}
|
}
|
||||||
|
@ -51,6 +60,11 @@ void nasal_parse::print_detail_token()
|
||||||
|
|
||||||
void nasal_parse::get_token_list(std::list<token>& detail_token)
|
void nasal_parse::get_token_list(std::list<token>& detail_token)
|
||||||
{
|
{
|
||||||
|
while(!parse.empty())
|
||||||
|
parse.pop();
|
||||||
|
while(!checked.empty())
|
||||||
|
checked.pop();
|
||||||
|
// clear stack
|
||||||
std::stack<token> tmp;
|
std::stack<token> tmp;
|
||||||
for(std::list<token>::iterator i=detail_token.begin();i!=detail_token.end();++i)
|
for(std::list<token>::iterator i=detail_token.begin();i!=detail_token.end();++i)
|
||||||
tmp.push(*i);
|
tmp.push(*i);
|
||||||
|
@ -68,15 +82,39 @@ void nasal_parse::get_token()
|
||||||
{
|
{
|
||||||
this_token=parse.top();
|
this_token=parse.top();
|
||||||
parse.pop();
|
parse.pop();
|
||||||
|
checked.push(this_token);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
this_token.type=__stack_end;
|
this_token.type=__stack_end;
|
||||||
this_token.str="__stack_end";
|
this_token.str="__stack_end";
|
||||||
|
std::cout<<">>[Parse-error] empty token stack."<<std::endl;
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void nasal_parse::push_token()
|
||||||
|
{
|
||||||
|
if(!checked.empty())
|
||||||
|
{
|
||||||
|
parse.push(checked.top());
|
||||||
|
checked.pop();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
std::cout<<">>[Parse-error] empty checked token stack."<<std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int nasal_parse::get_error()
|
||||||
|
{
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract_syntax_tree& nasal_parse::get_root()
|
||||||
|
{
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
void nasal_parse::main_generate()
|
void nasal_parse::main_generate()
|
||||||
{
|
{
|
||||||
error=0;
|
error=0;
|
||||||
|
@ -88,8 +126,55 @@ void nasal_parse::main_generate()
|
||||||
while(!parse.empty())
|
while(!parse.empty())
|
||||||
{
|
{
|
||||||
get_token();
|
get_token();
|
||||||
|
switch(this_token.type)
|
||||||
|
{
|
||||||
|
case __var:break;
|
||||||
|
case __number:
|
||||||
|
case __string:
|
||||||
|
case __id:break;
|
||||||
|
case __left_curve:break;
|
||||||
|
case __left_bracket:break;
|
||||||
|
case __if:break;
|
||||||
|
case __while:
|
||||||
|
case __for:
|
||||||
|
case __foreach:
|
||||||
|
case __forindex:break;
|
||||||
|
case __semi:break;
|
||||||
|
default:
|
||||||
|
++error;
|
||||||
|
std::cout<<">>[Parse-error] line "<<this_token.line<<": error token \'";
|
||||||
|
print_parse_token(this_token.type);
|
||||||
|
std::cout<<"\' in main scope."<<std::endl;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
std::cout<<">>[Parse] complete generation. "<<error<<" error(s), "<<warning<<" warning(s)."<<std::endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
abstract_syntax_tree nasal_parse::number_expr()
|
||||||
|
{
|
||||||
|
abstract_syntax_tree node;
|
||||||
|
node.set_line(this_token.line);
|
||||||
|
node.set_type(__number);
|
||||||
|
node.set_number(this_token.str);
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract_syntax_tree nasal_parse::string_expr()
|
||||||
|
{
|
||||||
|
abstract_syntax_tree node;
|
||||||
|
node.set_line(this_token.line);
|
||||||
|
node.set_type(__string);
|
||||||
|
node.set_string(this_token.str);
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract_syntax_tree nasal_parse::nil_expr()
|
||||||
|
{
|
||||||
|
abstract_syntax_tree node;
|
||||||
|
node.set_line(this_token.line);
|
||||||
|
node.set_type(__nil);
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue