2#include <vkdb/database.h>
4#include <vkdb/parser.h>
5#include <vkdb/interpreter.h>
8bool VQ::had_error_ =
false;
9bool VQ::had_runtime_error_ =
false;
10Database VQ::database_{INTERPRETER_DEFAULT_DATABASE};
14 if (path.extension() !=
".vq") {
15 std::cerr <<
"\033[1;32mVQ::runFile(): File extension cannot be "
16 << path.extension() <<
", must be .vq.\033[0m\n";
19 std::ifstream file{path};
20 if (!file.is_open()) {
21 std::cerr <<
"\033[1;32mVQ::runFile(): Unable to open file "
22 << path <<
".\033[0m\n";
25 std::stringstream buffer;
26 buffer << file.rdbuf();
31 std::cout <<
"\033[1;31mwelcome to the vq repl! :)\033[0m\n";
32 std::cout <<
"\033[1;31m(on default interpreter database)\033[0m\n";
35 std::cout <<
"\033[1;34m(vq) >> \033[0m";
36 if (!std::getline(std::cin, line) || line.empty()) {
44void VQ::run(
const std::string& source)
noexcept {
46 auto tokens{lexer.tokenize()};
48 Parser parser{tokens, error};
49 auto expr{parser.parse()};
55 interpreter_.interpret(expr.value());
59 if (token.type() == TokenType::END_OF_FILE) {
60 report(token.line(),
"at end", message);
62 report(token.line(),
"at '" + token.lexeme() +
"'", message);
67 std::cerr <<
"\033[1;32m[line " << error.token().line();
68 std::cerr <<
"] Runtime error: " << error.message() <<
"\033[0m\n";
69 had_runtime_error_ =
true;
74 const std::string& where,
75 const std::string& message
77 std::cerr <<
"\033[1;32m[line " << line <<
"] Parse error ";
78 std::cerr << where <<
": " << message <<
"\033[0m\n";
static void error(Token token, const std::string &message) noexcept
Handle an error.
static void run(const std::string &source) noexcept
Run a source string.
static void runFile(const std::filesystem::path path) noexcept
Run a file.
static void runPrompt() noexcept
Run the prompt.
static void runtimeError(const RuntimeError &error) noexcept
Handle a runtime error.