Main Page | Class Hierarchy | Compound List | File List | Compound Members | File Members | Related Pages

main.cpp

00001 /*
00002  * Copyright (c) 2003, Raymond Bosman
00003  * Copyright (c) 2003, Frederik Holljen
00004  * All Rights Reserved.
00005  *
00006  * See COPYING for licensing.
00007  */
00008 
00009 
00010 #include <fstream>
00011 #include <iostream>
00012 
00013 //#include <antlr/CommonAST.hpp>
00014 //#include <antlr/AST.hpp>
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         // print usage list
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"; // default
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                 // initialize and put it in the parser...
00084                 parser.initializeASTFactory(ast_factory);
00085                 parser.setASTFactory(&ast_factory);
00086 
00087                 parser.setFilename(inputFileName.c_str());
00088 
00089                 // start parsing at the program.
00090                 parser.program();
00091 
00092                 // Damn, this line took me a 2 hours.
00093                 // Somehow, when debugging this line causes a segment fault.
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                 // Checker.
00100                 
00101                 CkChecker checker;
00102                 checker.initializeASTFactory(ast_factory);
00103                 checker.setASTFactory(&ast_factory);
00104 
00105                 checker.program(parser.getAST());
00106 
00107         // silently quit if there where errors while checking
00108         if( ErrorHandler::getNumErrors() != 0 )
00109             return -1;
00110 
00111         if( args.singleArgSet( 'd' ) )
00112             std::cout << "Generating code ... " << std::endl;
00113                 // Now the code generation
00114                 CkCodeGenerator tparse;
00115                 tparse.initializeASTFactory(ast_factory);
00116                 tparse.setASTFactory(&ast_factory);
00117 
00118 
00119         // all is fine, create and set the class we will be writing to.
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 }

Generated on Mon Dec 1 14:26:27 2003 for Ck by doxygen 1.3.3