Browse Source

Main: Add CLI arguments to enable pretty printing / dumping

master
Riyyi 2 years ago
parent
commit
eedfe53b96
  1. 4
      CMakeLists.txt
  2. 17
      src/settings.cpp
  3. 30
      src/settings.h
  4. 61
      src/step1_read_print.cpp

4
CMakeLists.txt

@ -74,8 +74,8 @@ target_link_libraries(${PROJECT} ruc)
# Execute target
add_custom_target(run
COMMAND ${PROJECT})
add_dependencies(run ${PROJECT})
COMMAND ${PROJECT}
DEPENDS ${PROJECT})
# ------------------------------------------
# Test targets

17
src/settings.cpp

@ -0,0 +1,17 @@
/*
* Copyright (C) 2023 Riyyi
*
* SPDX-License-Identifier: MIT
*/
#include "settings.h"
namespace blaze {
std::string_view Settings::get(std::string_view key) const
{
VERIFY(m_settings.find(key) != m_settings.end(), "setting does not exist");
return m_settings.at(key);
};
} // namespace blaze

30
src/settings.h

@ -0,0 +1,30 @@
/*
* Copyright (C) 2023 Riyyi
*
* SPDX-License-Identifier: MIT
*/
#pragma once
#include "ruc/meta/assert.h"
#include "ruc/singleton.h"
#include <string>
#include <string_view>
#include <unordered_map>
#include <vector>
namespace blaze {
class Settings final : public ruc::Singleton<Settings> {
public:
Settings(s) {}
virtual ~Settings() = default;
std::string_view get(std::string_view key) const;
void set(std::string_view key, std::string_view value) { m_settings[key] = value; };
private:
std::unordered_map<std::string_view, std::string_view> m_settings;
};
} // namespace blaze

61
src/step1_read_print.cpp

@ -4,37 +4,42 @@
#include <string> // std::getline
#include <string_view>
#include "error.h"
#include "ruc/argparser.h"
#include "ruc/format/color.h"
#include "ast.h"
#include "error.h"
#include "lexer.h"
#include "printer.h"
#include "reader.h"
#define PRETTY_PRINT 0
#include "settings.h"
#if 1
auto read(std::string_view input) -> blaze::ASTNode*
{
blaze::Lexer lexer(input);
lexer.tokenize();
// lexer.dump();
if (blaze::Settings::the().get("dump-lexer") == "1") {
lexer.dump();
}
blaze::Reader reader(std::move(lexer.tokens()));
reader.read();
// reader.dump();
if (blaze::Settings::the().get("dump-reader") == "1") {
reader.dump();
}
return reader.node();
}
auto eval(blaze::ASTNode* node) -> blaze::ASTNode*
auto eval(blaze::ASTNode* ast) -> blaze::ASTNode*
{
return node;
return ast;
}
auto print(blaze::ASTNode* node) -> void
auto print(blaze::ASTNode* exp) -> void
{
blaze::Printer printer(node);
blaze::Printer printer(exp);
printer.dump();
}
@ -52,25 +57,41 @@ static auto cleanup(int signal) -> void
std::exit(signal);
}
auto main() -> int
auto main(int argc, char* argv[]) -> int
{
bool dump_lexer = false;
bool dump_reader = false;
bool pretty_print = false;
// CLI arguments
ruc::ArgParser arg_parser;
arg_parser.addOption(dump_lexer, 'l', "dump-lexer", nullptr, nullptr);
arg_parser.addOption(dump_reader, 'r', "dump-reader", nullptr, nullptr);
arg_parser.addOption(pretty_print, 'c', "color", nullptr, nullptr);
arg_parser.parse(argc, argv);
// Set settings
blaze::Settings::the().set("dump-lexer", dump_lexer ? "1" : "0");
blaze::Settings::the().set("dump-reader", dump_reader ? "1" : "0");
blaze::Settings::the().set("pretty-print", pretty_print ? "1" : "0");
// Signal callbacks
std::signal(SIGINT, cleanup);
std::signal(SIGTERM, cleanup);
while (true) {
#if PRETTY_PRINT
print(fg(ruc::format::TerminalColor::Blue), "user>");
print(" ");
print("\033[1m");
#else
print("user> ");
#endif
if (!pretty_print) {
print("user> ");
}
else {
print(fg(ruc::format::TerminalColor::Blue), "user>");
print(" \033[1m");
}
std::string line;
std::getline(std::cin, line);
#if PRETTY_PRINT
print("\033[0m");
#endif
if (pretty_print) {
print("\033[0m");
}
// Exit with Ctrl-D
if (std::cin.eof() || std::cin.fail()) {

Loading…
Cancel
Save