Compare commits

..
2 Commits
Author SHA1 Message Date
Riyyi f6f8207e1a Meta: Add license file 2023-03-23 20:33:50 +01:00
Riyyi eedfe53b96 Main: Add CLI arguments to enable pretty printing / dumping 2023-03-20 21:38:19 +01:00
5 changed files with 110 additions and 22 deletions
+2 -2
View File
@@ -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
+20
View File
@@ -0,0 +1,20 @@
MIT License
Copyright (C) 2023 Riyyi
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+17
View File
@@ -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
View File
@@ -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
+41 -20
View File
@@ -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()) {