Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members

test_factorial_recursive.cc

Go to the documentation of this file.
00001 
00004 #include "ifprintf.h"
00005 #include "ast.h"
00006 int main(int argc, char** argv){
00007     printf("-------------------------------------------------------------------------------\n");
00008     printf("-------------------------------------------------------------------------------\n");
00009     printf("-------------------------------------------------------------------------------\n");
00010     set_indent("  ");
00011 
00012     AST::Identifier* id_x = new AST::Identifier();
00013     id_x->handle = "x";
00014 
00015     AST::Identifier* id_y = new AST::Identifier();
00016     id_y->handle = "y";
00017 
00018     AST::Constant* const_1 = new AST::Constant();
00019         const_1->type = "int";
00020         const_1->value = "1";
00021 
00022     AST::Constant* const_10 = new AST::Constant();
00023         const_1->type = "int";
00024         const_1->value = "10";
00025 
00026     AST::Program* program = new AST::Program();
00027         vector<AST::Node*> program_statements;
00028         AST::Def_Function* func_factorial = new AST::Def_Function();
00029             func_factorial->handle = "factorial_recursive";
00030 
00031             vector<AST::Node*> fact_args;
00032                 fact_args.push_back(id_x);
00033             func_factorial->arguments = fact_args;
00034 
00035             vector<AST::Node*> fact_statements;
00036             AST::If* fact_if = new AST::If();
00037                 //condition
00038                 AST::Op_Binary* fact_eqeq = new AST::Op_Binary();
00039                     fact_eqeq->lhs = id_x;
00040                     fact_eqeq->op = "==";
00041                     fact_eqeq->rhs = const_1;
00042                 fact_if->condition = fact_eqeq;
00043 
00044                 //then
00045                 vector<AST::Node*> statements_then;
00046                 AST::Return* fact_return1 = new AST::Return();
00047                     fact_return1->value = const_1;
00048                 statements_then.push_back(fact_return1);
00049                 fact_if->statements_then = statements_then;
00050 
00051                 //else
00052                 vector<AST::Node*> statements_else;
00053                 AST::Return* fact_return_recursive = new AST::Return();
00054                     AST::FunctionCall* recursiveCall = new AST::FunctionCall();
00055                         //arguments
00056                         vector<AST::Node*> recursiveCall_arguments;
00057                             recursiveCall_arguments.push_back(id_x);
00058                         recursiveCall->arguments = recursiveCall_arguments;
00059                     fact_return_recursive->value = recursiveCall;
00060                 statements_else.push_back(fact_return_recursive);
00061                 fact_if->statements_else = statements_else;
00062         program_statements.push_back(func_factorial);
00063 
00064         AST::Def_Function* func_main = new AST::Def_Function();
00065             func_main->handle = "main";
00066             //args
00067             vector<AST::Node*> main_args;
00068             func_main->arguments = main_args;
00069             //statements
00070             vector<AST::Node*> main_statements;
00071                 AST::Def_Var* main_defs = new AST::Def_Var();
00072                     main_defs->type = "int";
00073                     vector<AST::Node*> list_handles;
00074                         list_handles.push_back(id_x);
00075                         list_handles.push_back(id_y);
00076                     main_defs->handles = list_handles;
00077                 main_statements.push_back(main_defs);
00078                 AST::Assignment* assign_x = new AST::Assignment();
00079                     assign_x->lhs = id_x;
00080                     assign_x->op  = "=";
00081                     assign_x->rhs = const_10;
00082                 main_statements.push_back(assign_x);
00083                 AST::Assignment*  assign_y = new AST::Assignment();
00084                     assign_y->lhs = id_y;
00085                     assign_y->op = "=";
00086                     //rhs
00087                     AST::FunctionCall* funcCall = new AST::FunctionCall();
00088                         vector<AST::Node*> funcCall_args;
00089                         funcCall_args.push_back(id_x);
00090                     assign_y->rhs = funcCall;
00091                 main_statements.push_back(assign_y);
00092             func_main->statements = main_statements;
00093         program_statements.push_back(func_main);
00094     program->statements = program_statements;
00095 
00096     program->toASTFile(stdout);
00097 }

Generated on Thu Oct 20 12:00:04 2005 for ASTree by  doxygen 1.4.2-20050421