ss2pi.test.cc

Go to the documentation of this file.
00001 /*
00002    The lestes compiler suite
00003    Copyright (C) 2002, 2003, 2004, 2005 Miroslav Tichy
00004    Copyright (C) 2002, 2003, 2004, 2005 Petr Zika
00005    Copyright (C) 2002, 2003, 2004, 2005 Vojtech Hala
00006    Copyright (C) 2002, 2003, 2004, 2005 Jiri Kosina
00007    Copyright (C) 2002, 2003, 2004, 2005 Pavel Sanda
00008    Copyright (C) 2002, 2003, 2004, 2005 Jan Zouhar
00009    Copyright (C) 2002, 2003, 2004, 2005 Rudolf Thomas
00010 
00011    This program is free software; you can redistribute it and/or modify
00012    it under the terms of the GNU General Public License as published by
00013    the Free Software Foundation; version 2 of the License.
00014 
00015    This program is distributed in the hope that it will be useful,
00016    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018    GNU General Public License for more details.
00019 
00020    See the full text of the GNU General Public License version 2, and
00021    the limitations in the file doc/LICENSE.
00022 
00023    By accepting the license the licensee waives any and all claims
00024    against the copyright holder(s) related in whole or in part to the
00025    work, its use, and/or the inability to use it.
00026  
00027  */
00028 #include <lestes/lang/cplus/lex/file_system.hh>
00029 #include <lestes/lang/cplus/lex/preprocessor.hh>
00030 #include <lestes/lang/cplus/sem/sa_statements.g.hh>
00031 #include <lestes/lang/cplus/sem/ss_misc.g.hh>
00032 #include <lestes/lang/cplus/sem/ss_ss2pi_base.hh>
00033 #include <lestes/lang/cplus/syn/hinter.hh>
00034 #include <lestes/lang/cplus/syn/manager.hh>
00035 #include <lestes/lang/cplus/syn/parse_result.hh>
00036 #include <lestes/lang/cplus/syn/parser.hh>
00037 #include <lestes/lang/cplus/syn/prefixer.hh>
00038 #include <lestes/msg/logger.hh>
00039 #include <lestes/package.hh>
00040 #include <lestes/std/data_types.hh>
00041 #include <lestes/std/dumper.hh>
00042 #include <lestes/backend/backend.g.hh>
00043 #include <vector>
00044 #include <iostream>
00045 #include <fstream>
00046 #include <cstdlib>
00047 
00048 
00049 package(lestes);
00050 package(lang);
00051 package(cplus);
00052 package(syn);
00053 
00054 
00055 using ::lestes::lang::cplus::sem::ss_decl_seq;
00056 using ::lestes::lang::cplus::sem::ss2pi_start;
00057 using ::lestes::lang::cplus::sem::ss_translation_unit;
00058 using ::lestes::lang::cplus::sem::sa_context_manager;
00059 
00060 
00061 int do_it( bool flat_dump, lstring filename, lstring ss_filename, bool hinter_on )
00062 {
00063         /* lex init */
00064         ptr<prefixer::preprocessor> pp =
00065                 prefixer::preprocessor::create(
00066                         ::lestes::lang::cplus::lex::file_system::create(), "" );
00067 
00068         /* syn init */
00069         prefixer::init( prefixer::PREFIX_ON, pp );
00070         manager::init();
00071         hinter::init( hinter_on ? hinter::HINTER_CHECK : hinter::HINTER_OFF );
00072         parser::init();
00073 	::lestes::lang::cplus::sem::sa_statements::instance();
00074 
00075 //F//   //FIXME: this should be done elsewhere
00076 //F//   hinter::forced_scope_set( ss_decl_seq::root_instance() );
00077 //F//   // end
00078 
00079         ptr<parse_result_type> parse_result = parser::parse();
00080         bool success = parse_result->success_get();
00081         ::std::cout << (success ? "succeeded" : "failed") << ::std::endl;
00082 
00083         ptr< ::lestes::intercode::as_base > as_result = parse_result->as_result_get();
00084         if (filename != "" && as_result) {
00085                 ::std::ofstream f(filename.c_str());
00086                 if (flat_dump)
00087 			::lestes::std::dumper::dump( f, as_result );
00088                 else
00089 			::lestes::std::readable_dumper::dump( f, as_result );
00090                 f.close();
00091         }
00092 
00093         if (ss_filename != "") {
00094                 ::std::ofstream f(ss_filename.c_str());
00095                 if (flat_dump)
00096 			::lestes::std::dumper::dump(f, sem::ss_decl_seq::root_instance());
00097                 else
00098 			::lestes::std::readable_dumper::dump(f, sem::ss_decl_seq::root_instance());
00099                 f.close();
00100         }
00101 
00102         ss2pi_start(ss_translation_unit::create(ss_decl_seq::root_instance()));
00103         lestes::backend::backend::create(::std::cout)->main();
00104         return success ? 0 : 1;
00105 }
00106 
00107 end_package(syn);
00108 end_package(cplus);
00109 end_package(lang);
00110 end_package(lestes);
00111 
00112 using ::std::vector;
00113 using ::std::cout;
00114 using ::std::endl;
00115 using ::lestes::std::lstring;
00116 
00117 struct usage_and_exit {
00118 };
00119 
00120 void operator << ( ::std::ostream & os, const usage_and_exit & )
00121 {
00122         os << "\n\n"
00123                 "Usage: parser_test [-h] [-l llc_file] [-s skel_file] [-o output_file] [-f filename] [dump_type]\n"
00124                 "\n"
00125                 "Logger configuration is read from llc_file, if specified.\n"
00126                 "Logger configuration skeleton is dumped to skel_file, if specified.\n"
00127                 "AS structures will be dumped into file specified by the -o option.\n"
00128                 "  When the option is missing, they will not be dumped at all.\n"
00129                 "SS structures will be dumped into file specified by the -f option.\n"
00130                 "  When the option is missing, they will not be dumped at all.\n"
00131                 "dump_type is either 'flat' or 'deep', the default is 'deep'.\n"
00132                 "Hinter is turned on by the -h option.\n"
00133                 << endl;
00134         return exit(1);
00135 }
00136 
00137 int main( int argc, char * argv[] )
00138 {
00139         vector<lstring> args( argv, argv+argc );
00140         vector<lstring>::iterator it = args.begin();
00141         ++it;
00142         lstring filename = "";
00143         lstring ss_filename = "";
00144         bool log_finish = false;
00145         bool o_seen = false;
00146         bool f_seen = false;
00147         bool l_seen = false;
00148         bool h_seen = false;
00149         while (it != args.end() && (*it)[0] == '-') {
00150                 // option found
00151                 if (*it == "-o") {
00152                         if (o_seen)
00153                                 cout << "Error: -o option used multiple times." << usage_and_exit();
00154                         o_seen = true;
00155                         ++it;
00156                         if (it == args.end())
00157                                 cout << "Argument to -o option is missing." << usage_and_exit();
00158                         filename = *it++;
00159                 } else if (*it == "-f") {
00160                         if (f_seen)
00161                                 cout << "Error: -f option used multiple times." << usage_and_exit();
00162                         f_seen = true;
00163                         ++it;
00164                         if (it == args.end())
00165                                 cout << "Argument to -f option is missing." << usage_and_exit();
00166                         ss_filename = *it++;
00167                 } else if (*it == "-l") {
00168                         if (l_seen)
00169                                 cout << "Error: -l option used multiple times." << usage_and_exit();
00170                         l_seen = true;
00171                         ++it;
00172                         if (it == args.end())
00173                                 cout << "Argument to -l option is missing." << usage_and_exit();
00174                         log_finish = ::lestes::msg::logger::init(*it++);
00175                         if (!log_finish)
00176                                 cout << "Error, loggers will be off." << endl;
00177                 } else if (*it == "-s") {
00178                         // can be used multiple times
00179                         ++it;
00180                         if (it == args.end())
00181                                 cout << "Argument to -s option is missing." << usage_and_exit();
00182                         ::std::ofstream of( it->c_str() );
00183                         ++it;
00184 			::lestes::msg::logger::dump_skeleton(of);
00185                         of.close();
00186                 } else if (*it == "-h") {
00187                         ++it;
00188                         h_seen = true;
00189                 } else
00190                         cout << "Unknown option '" << *it << "'." << usage_and_exit();
00191         }
00192         bool flat_dump = false;
00193         if (it != args.end()) {
00194                 if (*it == "flat")
00195                         flat_dump = true;
00196                 else if (*it != "deep")
00197                         cout << "Unknown dump type '" << *it << "'." << usage_and_exit();
00198                 ++it;
00199                 if (it != args.end())
00200                         cout << "Too many arguments." << usage_and_exit();
00201         }
00202         int result = ::lestes::lang::cplus::syn::do_it( flat_dump, filename, ss_filename, h_seen );
00203         if (log_finish)
00204 		::lestes::msg::logger::finish();
00205         return result;
00206 }

Generated on Mon Feb 12 18:23:16 2007 for lestes by doxygen 1.5.1-20070107