vyp08.cc

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2008 Kamil Dudka <xdudka00@stud.fit.vutbr.cz>
00003  *
00004  * This file is part of vyp08 (compiler and interpreter of VYP08 language).
00005  *
00006  * vyp08 is free software: you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation, either version 3 of the License, or
00009  * any later version.
00010  *
00011  * vyp08 is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with vyp08.  If not, see <http://www.gnu.org/licenses/>.
00018  */
00019 
00020 #include "config.h"
00021 
00022 #include "builder.h"
00023 #include "parser.h"
00024 #include "scanner.h"
00025 #include "vm.h"
00026 #include "vypIO.h"
00027 
00028 #ifndef BUILDING_DOX
00029 #   include <string>
00030 #   include <fstream>
00031 #endif
00032 
00033 using namespace StreamDecorator;
00034 using std::string;
00035 
00040 void printUsage(string path) {
00041     std::cout << "Usage: " << path << " INPUT_FILE_NAME" << std::endl;
00042 }
00043 
00044 int main(int argc, char *argv[]) {
00045 #if CONSOLE_COLOR_OUTPUT
00046     Color::enable(true);
00047 #endif
00048     if (argc != 2) {
00049         // invalid count of arguments
00050         if (argc > 0)
00051             printUsage(argv[0]);
00052         return -1;
00053     }
00054     const string fileName(argv[1]);
00055     if (fileName == string("--help")) {
00056         // --help
00057         printUsage(argv[0]);
00058         return 0;
00059     }
00060 
00061     // open input file
00062     std::fstream input(fileName.c_str(), std::ios::in);
00063     if (!input) {
00064         std::cerr << Error(E_ERROR, fileName, "can't open file") << std::endl;
00065         return -1;
00066     }
00067 
00068     // create scanner
00069     IScanner *scanner = ScannerFactory::createScanner(input, fileName);
00070 
00071     // create and initialize virtual machine
00072     Vm *vm = new Vm(fileName);
00073     bool bOk = FncFactory::initVm(vm);
00074 
00075     // create builder for virtual machine
00076     IBuilder *builder = BuilderFactory::createBuilder(vm);
00077 
00078     // parse input, build virtual machine
00079     bOk &= (0 == Parser::parse(scanner, builder, fileName));
00080     bOk &= !(scanner -> hasError());
00081     bOk &= !(builder -> hasError());
00082 
00083     // create virtual machine runner if all ok yet
00084     VmRunner *vmRunner = 0;
00085     if (bOk) {
00086         vmRunner = new VmRunner(vm);
00087         bOk &= !(vmRunner -> hasError());
00088     }
00089 
00090     if (!bOk)
00091         std::cerr << Error(E_NOTE, fileName, "unable to run program, fix the error(s) above") << std::endl;
00092     else {
00093         // run program
00094         if (!vmRunner -> run()) {
00095             bOk = false;
00096             std::cerr << Error(E_NOTE, fileName, "runtime error(s) detected") << std::endl;
00097         }
00098     }
00099 
00100     // destroy objects in reverse order
00101     delete vmRunner;
00102     delete builder;
00103     delete vm;
00104     delete scanner;
00105 
00106     // close input file
00107     input.close();
00108 
00109     // final application exit code
00110     return bOk ? 0 : -1;
00111 }
00112 

Generated on Sat Jul 4 18:32:59 2009 for vyp08 (compiler and interpreter of VYP08 language) by  doxygen 1.5.4