summaryrefslogtreecommitdiff
path: root/src/cli.cc
blob: d74e327f6162a801928f588c0d64a0becfd9618f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// This file is part of Cait <https://github.com/Fuwn/cait>.
// Copyright (C) 2022-2022 Fuwn <[email protected]>
//
// 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 <http://www.gnu.org/licenses/>.
//
// Copyright (C) 2022-2022 Fuwn <[email protected]>
// SPDX-License-Identifier: GPL-3.0-only

#include <fstream>

#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<std::string> {
  if (i < this->_argc) {
    return std::string(this->_argv[i]);
  } else {
    return std::nullopt;
  }
}

auto cli::option(const std::string &key)
    -> std::optional<std::optional<std::string>> {
  if (this->options.contains(key)) {
    return this->options[key];
  } else {
    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 << '\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 (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