#include "parser.hpp"
#include <iostream>
#include <fstream>

int main(int argc, char* argv[]) {
    auto args = read_arguments<args_t>(argc, argv);

    bool run_only = false;
    set_argument(run_only, "c", args);

    std::string line;
    math_parser_impl impl(args);
    math_parser parser(impl);

    for (auto& file : get_arguments(args)) {
        std::ifstream f(file.second);
        if (!f.is_open()) {
            std::cout << "warning: cannot find file '" << file.second << "'" << std::endl;
            continue;
        }

        int retcode = 0;
        while (getline(f, line)) {
            if (parser.parse(line, retcode)) break;
        }

        if (retcode != 0) return retcode;
    }

    int retcode = 0;
    if (!run_only) {
        std::cout << impl.prompt();
        while (getline(std::cin, line)) {
            if (parser.parse(line, retcode)) break;
            std::cout << impl.prompt();
        }
    }

    return retcode;
}
