00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include <fstream>
00011 #include <iostream>
00012
00013
00014
00015 #include "build/CkLexer.hpp"
00016 #include "build/CkParser.hpp"
00017 #include "build/CkChecker.hpp"
00018 #include "build/CkCodeGenerator.hpp"
00019
00020 #include "tools/errorhandler.hpp"
00021 #include "tools/exception.hpp"
00022 #include "tools/argumentparser.hpp"
00023
00024 #include "classencoder/class.hpp"
00025
00026 using namespace std;
00027
00029
00033 int main(int argc, char *argv[])
00034 {
00035 ArgumentParser args;
00036 if( !args.parseArguments( argc, argv ) || args.singleArgSet( 'h' ) || args.numArguments() == 0 )
00037 {
00038
00039 cerr << "Ck usage: [OPTIONS] -s source_file\n\n";
00040 cerr << "\t-o [class name] output has given classname with .class as extension\n";
00041 cerr << "\t-d turn on debug output\n";
00042 cerr << "\t-h shows this menu and then quits.\n";
00043 cerr << "\t-v shows version and then quits.\n";
00044 return -1;
00045 }
00046
00047 if( args.singleArgSet( 'v' ) )
00048 {
00049 cerr << "Ck version 1.0.0 by Raymond Bosman and Frederik Holljen\n";
00050 return -1;
00051 }
00052
00053 if( args.argValue( "s" ) == "" )
00054 {
00055 cerr << "You must supply a source file with -s\n";
00056 return -1;
00057 }
00058
00059 string outputClass = "output";
00060 if( args.argValue( "o" ) != "" )
00061 {
00062 outputClass = args.argValue( "o" );
00063 }
00064
00065 string inputFileName = args.argValue( "s" );
00066 try
00067 {
00068 std::ifstream inputFile( inputFileName.c_str() );
00069 if( !inputFile.is_open() )
00070 {
00071 std::cerr << "Input file " << inputFileName << " could not be found.\n";
00072 return -1;
00073 }
00074
00075 if( args.singleArgSet( 'd' ) )
00076 std::cout << "Parsing ... " << std::endl;
00077
00078 CkLexer lexer(inputFile);
00079 CkParser parser(lexer);
00080
00081 antlr::ASTFactory ast_factory( "CkASTNodeBase", CkASTNodeBase::factory );
00082
00083
00084 parser.initializeASTFactory(ast_factory);
00085 parser.setASTFactory(&ast_factory);
00086
00087 parser.setFilename(inputFileName.c_str());
00088
00089
00090 parser.program();
00091
00092
00093
00094 if( args.singleArgSet( 'd' ) )
00095 std::cout << "parser: " << parser.getAST()->toStringList() << std::endl;
00096
00097 if( args.singleArgSet( 'd' ) )
00098 std::cout << "Checking ... " << std::endl;
00099
00100
00101 CkChecker checker;
00102 checker.initializeASTFactory(ast_factory);
00103 checker.setASTFactory(&ast_factory);
00104
00105 checker.program(parser.getAST());
00106
00107
00108 if( ErrorHandler::getNumErrors() != 0 )
00109 return -1;
00110
00111 if( args.singleArgSet( 'd' ) )
00112 std::cout << "Generating code ... " << std::endl;
00113
00114 CkCodeGenerator tparse;
00115 tparse.initializeASTFactory(ast_factory);
00116 tparse.setASTFactory(&ast_factory);
00117
00118
00119
00120 Class output( outputClass );
00121 tparse.setClass( &output );
00122
00123 tparse.program(parser.getAST());
00124
00125 output.write();
00126
00127 }
00128 catch(antlr::RecognitionException &e)
00129 {
00130 ErrorHandler::error (e.getFilename(), e.getLine (), e.getColumn(), e.getMessage());
00131 return -1;
00132 }
00133 catch(antlr::ANTLRException &e)
00134 {
00135 cerr << "ANTLR compiler made a booboo. Please file a bug report.\n";
00136 cerr << "Sorry...\n";
00137 return -1;
00138 }
00139 catch(std::exception& e)
00140 {
00141 std::cerr << "exception: " << e.what() << std::endl;
00142 return -1;
00143 }
00144 catch( Exception &e )
00145 {
00146 cerr << "The compiler made a booboo. Please file a bug report.\n";
00147 cerr << e.getError() << "\n";
00148 cerr << "Sorry...\n";
00149 return -1;
00150 }
00151
00152 return 0;
00153 }