Česky
Kamil Dudka

Flex/Bison based compiler and interpreter written in C++ (using Boost)

File detail

Name:Downloadvyp08.cc [Download]
Location: vyp08 > vyp08-1.0pre1 > src
Size:2.9 KB
Last modification:2022-09-09 13:06

Source code

/*
 * Copyright (C) 2008 Kamil Dudka <xdudka00@stud.fit.vutbr.cz>
 *
 * This file is part of vyp08 (compiler and interpreter of VYP08 language).
 *
 * vyp08 is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * vyp08 is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with vyp08.  If not, see <http://www.gnu.org/licenses/>.
 */
 
#include "config.h"
 
#include "builder.h"
#include "parser.h"
#include "scanner.h"
#include "vm.h"
#include "vypIO.h"
 
#ifndef BUILDING_DOX
#   include <string>
#   include <fstream>
#endif
 
using namespace StreamDecorator;
using std::string;
 
/**
 * print application usage in usual way
 * @param path path to application
 */
void printUsage(string path) {
    std::cout << "Usage: " << path << " INPUT_FILE_NAME" << std::endl;
}
 
int main(int argc, char *argv[]) {
#if CONSOLE_COLOR_OUTPUT
    Color::enable(true);
#endif
    if (argc != 2) {
        // invalid count of arguments
        if (argc > 0)
            printUsage(argv[0]);
        return -1;
    }
    const string fileName(argv[1]);
    if (fileName == string("--help")) {
        // --help
        printUsage(argv[0]);
        return 0;
    }
 
    // open input file
    std::fstream input(fileName.c_str(), std::ios::in);
    if (!input) {
        std::cerr << Error(E_ERROR, fileName, "can't open file") << std::endl;
        return -1;
    }
 
    // create scanner
    IScanner *scanner = ScannerFactory::createScanner(input, fileName);
 
    // create and initialize virtual machine
    Vm *vm = new Vm(fileName);
    bool bOk = FncFactory::initVm(vm);
 
    // create builder for virtual machine
    IBuilder *builder = BuilderFactory::createBuilder(vm);
 
    // parse input, build virtual machine
    bOk &= (0 == Parser::parse(scanner, builder, fileName));
    bOk &= !(scanner -> hasError());
    bOk &= !(builder -> hasError());
 
    // create virtual machine runner if all ok yet
    VmRunner *vmRunner = 0;
    if (bOk) {
        vmRunner = new VmRunner(vm);
        bOk &= !(vmRunner -> hasError());
    }
 
    if (!bOk)
        std::cerr << Error(E_NOTE, fileName, "unable to run program, fix the error(s) above") << std::endl;
    else {
        // run program
        if (!vmRunner -> run()) {
            bOk = false;
            std::cerr << Error(E_NOTE, fileName, "runtime error(s) detected") << std::endl;
        }
    }
 
    // destroy objects in reverse order
    delete vmRunner;
    delete builder;
    delete vm;
    delete scanner;
 
    // close input file
    input.close();
 
    // final application exit code
    return bOk ? 0 : -1;
}