Complete add & sub
This commit is contained in:
parent
bf2929d71f
commit
dfd1b6c022
|
@ -9,6 +9,7 @@ class ast_tree_node
|
|||
int line;
|
||||
int type;
|
||||
std::list<ast_tree_node> children;
|
||||
double num;
|
||||
public:
|
||||
ast_tree_node()
|
||||
{
|
||||
|
@ -43,13 +44,13 @@ class ast_tree_node
|
|||
{
|
||||
return children;
|
||||
}
|
||||
virtual void print_all_tree(int tabnum=0)
|
||||
void print_all_tree(int tabnum=0)
|
||||
{
|
||||
for(int i=0;i<tabnum;++i)
|
||||
std::cout<<" ";
|
||||
if(type==__number)
|
||||
{
|
||||
std::cout<<"[ number ]"<<std::endl;
|
||||
std::cout<<"[ "<<num<<" ]"<<std::endl;
|
||||
return;
|
||||
}
|
||||
std::cout<<"{ ";
|
||||
|
@ -64,6 +65,32 @@ class ast_tree_node
|
|||
std::cout<<"}"<<std::endl;
|
||||
return;
|
||||
}
|
||||
double run()
|
||||
{
|
||||
if(type==__number)
|
||||
return num;
|
||||
if(type==__add_operator)
|
||||
{
|
||||
double sum=0;
|
||||
std::list<ast_tree_node>::iterator i=children.begin();
|
||||
sum=i->run()+(++i)->run();
|
||||
return sum;
|
||||
}
|
||||
else if(type==__sub_operator)
|
||||
{
|
||||
double sum=0;
|
||||
std::list<ast_tree_node>::iterator i=children.begin();
|
||||
sum=i->run()-(++i)->run();
|
||||
return sum;
|
||||
}
|
||||
else
|
||||
{
|
||||
for(std::list<ast_tree_node>::iterator i=children.begin();i!=children.end();++i)
|
||||
std::cout<<i->run()<<std::endl;
|
||||
return 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
class ast_node:public ast_tree_node
|
||||
|
@ -96,21 +123,15 @@ class operator_expr:public ast_node
|
|||
|
||||
class number_node:public ast_leaf
|
||||
{
|
||||
private:
|
||||
long long int num;
|
||||
double fnum;
|
||||
bool is_float;
|
||||
public:
|
||||
number_node():ast_leaf()
|
||||
{
|
||||
num=0;
|
||||
fnum=0;
|
||||
is_float=false;
|
||||
}
|
||||
void set_number(std::string& str)
|
||||
{
|
||||
type=__number;
|
||||
is_float=false;
|
||||
double is_float=false;
|
||||
int DotPlace=0;
|
||||
for(int i=0;i<(int)str.length();++i)
|
||||
if(str[i]=='.')
|
||||
|
@ -122,26 +143,26 @@ class number_node:public ast_leaf
|
|||
if(!is_float)
|
||||
{
|
||||
num=0;
|
||||
long long int acc=1;
|
||||
double acc=1;
|
||||
for(int i=(int)str.length()-1;i>=0;--i)
|
||||
{
|
||||
num+=acc*((long long int)(str[i]-'0'));
|
||||
num+=acc*((double)(str[i]-'0'));
|
||||
acc*=10;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
fnum=0;
|
||||
num=0;
|
||||
double acc=1;
|
||||
double aff=0.1;
|
||||
for(int i=DotPlace+1;i<(int)str.length();++i)
|
||||
{
|
||||
fnum+=aff*((double)(str[i]-'0'));
|
||||
num+=aff*((double)(str[i]-'0'));
|
||||
aff*=0.1;
|
||||
}
|
||||
for(int i=DotPlace-1;i>=0;--i)
|
||||
{
|
||||
fnum+=acc*((double)(str[i]-'0'));
|
||||
num+=acc*((double)(str[i]-'0'));
|
||||
acc*=10;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -60,7 +60,17 @@ class code_generator
|
|||
parse=temp;
|
||||
return;
|
||||
}
|
||||
void run()
|
||||
void print_gen_tree()
|
||||
{
|
||||
root.print_all_tree();
|
||||
return;
|
||||
}
|
||||
void run_gen_tree()
|
||||
{
|
||||
root.run();
|
||||
return;
|
||||
}
|
||||
void gen_ast()
|
||||
{
|
||||
root.return_list().clear();
|
||||
bool iserror=false;
|
||||
|
@ -75,17 +85,21 @@ class code_generator
|
|||
}
|
||||
parse.pop();
|
||||
}
|
||||
std::stack<ast_tree_node> temp_ast;
|
||||
while(!node_stack.empty())
|
||||
{
|
||||
root.return_list().push_back(node_stack.top());
|
||||
temp_ast.push(node_stack.top());
|
||||
node_stack.pop();
|
||||
}
|
||||
while(!temp_ast.empty())
|
||||
{
|
||||
root.return_list().push_back(temp_ast.top());
|
||||
temp_ast.pop();
|
||||
}
|
||||
if(iserror)
|
||||
return;
|
||||
std::cout<<">>[Parse] 0 error(s)."<<std::endl;
|
||||
std::cout<<">>[Parse] Complete checking."<<std::endl;
|
||||
root.print_all_tree();
|
||||
std::cout<<std::endl;
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
|
|
@ -20,9 +20,10 @@ int main()
|
|||
std::cout<<">> 3. [exit ] |shut down the interpreter."<<std::endl;
|
||||
std::cout<<">> 4. [lexer ] |run and show the lexer. (-lexer)"<<std::endl;
|
||||
std::cout<<">> 5. [parser] |run parser. (-parser)"<<std::endl;
|
||||
std::cout<<">> 6. [del ] |delete program in memory."<<std::endl;
|
||||
std::cout<<">> 7. [run ] |run the programme in stack. (-lexer -parser)"<<std::endl;
|
||||
std::cout<<">> 8. [rs ] |check the source program."<<std::endl;
|
||||
std::cout<<">> 6. [ast ] |print the abstract syntax tree."<<std::endl;
|
||||
std::cout<<">> 7. [del ] |delete program in memory."<<std::endl;
|
||||
std::cout<<">> 8. [run ] |run the programme in stack. (-lexer -parser)"<<std::endl;
|
||||
std::cout<<">> 9. [rs ] |check the source program."<<std::endl;
|
||||
}
|
||||
else if(command=="cls")
|
||||
{
|
||||
|
@ -49,15 +50,25 @@ int main()
|
|||
}
|
||||
else if(command=="parser")
|
||||
{
|
||||
lex.lexer_process(prog.use_file());
|
||||
pas.parse_process(lex.return_list());
|
||||
pas.print_parser_stack();
|
||||
}
|
||||
else if(command=="ast")
|
||||
{
|
||||
lex.lexer_process(prog.use_file());
|
||||
pas.parse_process(lex.return_list());
|
||||
cod.input_stack(pas.return_stack());
|
||||
cod.gen_ast();
|
||||
cod.print_gen_tree();
|
||||
}
|
||||
else if(command=="run")
|
||||
{
|
||||
lex.lexer_process(prog.use_file());
|
||||
pas.parse_process(lex.return_list());
|
||||
cod.input_stack(pas.return_stack());
|
||||
cod.run();
|
||||
cod.gen_ast();
|
||||
cod.run_gen_tree();
|
||||
}
|
||||
else
|
||||
prog.input_file(command);
|
||||
|
|
Loading…
Reference in New Issue