// This file is part of Cait . // Copyright (C) 2022-2022 Fuwn // // This program 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, version 3. // // This program 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 this program. If not, see . // // Copyright (C) 2022-2022 Fuwn // SPDX-License-Identifier: GPL-3.0-only #include #include "cli.hh" #include "context.hh" #include "help.hh" #include "lexer.hh" #include "parser.hh" #include "utility.hh" namespace cait { cli::cli(int argc, char **argv) : _argc(argc), _argv(argv) { for (int i = 1; i < _argc; i++) { if (_argv[i][0] == '-') { std::string key = _argv[i]; std::string value; if (i + 1 < _argc) { value = _argv[i + 1]; i += 1; } options[key] = value.empty() ? std::nullopt : std::optional(value); } } } // auto cli::argc() const -> int { return this->_argc; } // auto cli::argv() const -> char ** { return this->_argv; } auto cli::arg(int i) -> std::optional { if (i < this->_argc) { return std::string(this->_argv[i]); } return std::nullopt; } auto cli::option(const std::string &key) -> std::optional> { if (this->options.contains(key)) { return this->options[key]; } return std::nullopt; } auto cli::look() -> int { switch (cait::utility::hash(this->arg(1).value_or("").c_str())) { case cait::utility::hash("--help"): case cait::utility::hash("-h"): { std::cout << cait::help_message() << '\n'; } break; case cait::utility::hash("--version"): { std::cout << version::whole() << '\n'; } break; default: { std::string cait_file_name = this->option("-f")->value_or("build.cait"); std::ifstream cait_file(cait_file_name, std::ios::in); if (cait_file.is_open()) { lexer lexer(cait_file); parser parser(lexer.tree()); try { parser.generate_nodes(cait_file_name, lexer.lines()); } catch (const parser_exception &e) { std::cout << e.what() << '\n'; return 1; } context context(parser); for (const auto &token_line : parser.tree()) { for (const auto &token : token_line) { std::cout << token.word << "(" << token.type.string() << ")"; } std::cout << "\n\n"; } for (const auto &token_line : parser.nodes()) { std::visit( [](const auto &node) -> void { std::cout << node.string() << '\n'; }, token_line); } // auto cc = context.rule("cc"); // if (cc.has_value()) { // std::cout << "cc: '" << cc.value().string() << "'\n"; // } else { // std::cout << "cc has no value\n"; // } return 0; } std::cout << "cait: error: loading '" << cait_file_name << "': The system cannot find " "the file specified.\n\n"; return 1; } // break; } // auto option = this->option("-h"); // if (option.has_value()) { // std::cout << "option exists "; // if (option->has_value()) { // std::cout << "with value " << option->value() << std::endl; // } else { // std::cout << "with no value\n"; // } // } else { // std::cout << "option does not exist\n"; // } return 0; } } // namespace cait