mutable
A Database System for Research and Fast Prototyping
Loading...
Searching...
No Matches
check.cpp
Go to the documentation of this file.
1#include "lex/Lexer.hpp"
2#include "parse/ASTDumper.hpp"
3#include "parse/Parser.hpp"
4#include "parse/Sema.hpp"
5#include <cerrno>
6#include <cstdlib>
7#include <cstring>
8#include <fstream>
9#include <iostream>
10#include <mutable/Options.hpp>
13#include <mutable/util/fn.hpp>
14
15
16using namespace m;
17using namespace m::ast;
18
19
20void usage(std::ostream &out, const char *name)
21{
22 out << "Performs semantic analysis of the input.\n"
23 << "USAGE:\n\t" << name << " [<FILE>...]"
24 << std::endl;
25}
26
27int main(int argc, const char **argv)
28{
29 /*----- Parse command line arguments. ----------------------------------------------------------------------------*/
30 ArgParser AP;
31#define ADD(TYPE, VAR, INIT, SHORT, LONG, DESCR, CALLBACK)\
32 VAR = INIT;\
33 {\
34 AP.add<TYPE>(SHORT, LONG, DESCR, CALLBACK);\
35 }
36 ADD(bool, Options::Get().show_help, false, /* Type, Var, Init */
37 "-h", "--help", /* Short, Long */
38 "prints this help message", /* Description */
39 [&](bool) { Options::Get().show_help = true; }); /* Callback */
40 ADD(bool, Options::Get().has_color, false, /* Type, Var, Init */
41 nullptr, "--color", /* Short, Long */
42 "use colors", /* Description */
43 [&](bool) { Options::Get().has_color = true; }); /* Callback */
44 ADD(bool, Options::Get().ast, false, /* Type, Var, Init */
45 nullptr, "--ast", /* Short, Long */
46 "print AST", /* Description */
47 [&](bool) { Options::Get().ast = true; }); /* Callback */
48 ADD(bool, Options::Get().quiet, false, /* Type, Var, Init */
49 "-q", "--quiet", /* Short, Long */
50 "work in quiet mode", /* Description */
51 [&](bool) { Options::Get().quiet = true; }); /* Callback */
52#undef ADD
53 AP.parse_args(argc, argv);
54
55 if (Options::Get().show_help) {
56 usage(std::cout, argv[0]);
57 std::cout << "WHERE\n" << AP;
58 std::exit(EXIT_SUCCESS);
59 }
60
61 auto args = AP.args();
62 if (args.empty())
63 args.push_back("-"); // start in interactive mode
64
65 std::istream *in;
66
67 /* Create the diagnostics object. */
68 Diagnostic diag(Options::Get().has_color, std::cout, std::cerr);
69
70 bool sema_error = false;
71 Catalog &C = Catalog::Get();
72
73 /* Process all the inputs. */
74 for (auto filename : args) {
75 if (streq(filename, "-")) {
76 /* read from stdin */
77 in = &std::cin;
78 } else {
79 /* read from file */
80 in = new std::ifstream(filename, std::ios_base::in);
81 }
82
83 if (in->fail()) {
84 if (in == &std::cin)
85 std::cerr << "Failed to open stdin: ";
86 else
87 std::cerr << "Failed to open the file '" << filename << "': ";
88 std::cerr << strerror(errno) << std::endl;
89 }
90
91 Lexer lexer(diag, C.get_pool(), filename, *in);
92 Parser parser(lexer);
93 Sema sema(diag);
94
95 while (parser.token()) {
96 auto stmt = parser.parse_Stmt();
97 if (diag.num_errors()) {
98 diag.clear();
99 continue;
100 }
101 auto cmd = sema.analyze(std::move(stmt));
102 sema_error = sema_error or diag.num_errors();
103 if (diag.num_errors()) {
104 diag.clear();
105 continue;
106 }
107 if (Options::Get().ast) cmd->ast().dump(std::cout);
108 if (is<const DDLCommand>(cmd))
109 cmd->execute(diag);
110 }
111
112 if (in != &std::cin)
113 delete in;
114 }
115
116 std::exit(sema_error ? EXIT_FAILURE : EXIT_SUCCESS);
117}
int main(void)
bool show_help
‍whether to show a help message
struct @5 args
void usage(std::ostream &out, const char *name)
Definition: check.cpp:20
#define ADD(TYPE, VAR, INIT, SHORT, LONG, DESCR, CALLBACK)
A parser for command line arguments.
Definition: ArgParser.hpp:20
void parse_args(int argc, const char **argv)
Parses the arguments from argv.
Definition: ArgParser.cpp:186
const std::vector< const char * > & args() const
Returns all positional arguments.
Definition: ArgParser.hpp:143
‍mutable namespace
Definition: Backend.hpp:10
bool streq(const char *first, const char *second)
Definition: fn.hpp:29
The catalog contains all Databases and keeps track of all meta information of the database system.
Definition: Catalog.hpp:215
ThreadSafeStringPool & get_pool()
Returns a reference to the StringPool.
Definition: Catalog.hpp:259
static Catalog & Get()
Return a reference to the single Catalog instance.
void clear()
Resets the error counter.
Definition: Diagnostic.hpp:50
unsigned num_errors() const
Returns the number of errors emitted since the last call to clear().
Definition: Diagnostic.hpp:48
static Options & Get()
Return a reference to the single Options instance.
Definition: Options.cpp:9
bool show_help
Definition: Options.hpp:22
bool has_color
Definition: Options.hpp:33
bool quiet
Definition: Options.hpp:35
bool ast
Definition: Options.hpp:41
const Token & token()
Definition: Parser.hpp:38
std::unique_ptr< Stmt > parse_Stmt()
Definition: Parser.cpp:133
std::unique_ptr< DatabaseCommand > analyze(std::unique_ptr< ast::Command > ast)
Perform semantic analysis of an ast::Command.
Definition: Sema.cpp:15