Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add simple REPL functionality #5

Merged
merged 7 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
add_subdirectory(quickstart)
add_subdirectory(repl)
12 changes: 12 additions & 0 deletions examples/repl/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
project(kalcy-repl)

add_executable(${PROJECT_NAME})

target_link_libraries(${PROJECT_NAME} PRIVATE
kalcy::kalcy
kalcy::kalcy-compile-options
)

target_sources(${PROJECT_NAME} PRIVATE
repl.cpp
)
81 changes: 81 additions & 0 deletions examples/repl/repl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include <kalcy/kalcy.hpp>
#include <cassert>
#include <format>
#include <iomanip>
#include <iostream>
#include <sstream>

namespace {
struct Repl {
bool verbose{}; // toggled on with '-v' and off with '-q'
bool is_running{true};
kalcy::Parser parser{};

auto print_help() const -> void {
std::cout << "Usage: [OPTION] | [EXPRESSION]\n";
std::cout << "\nDescription:\n\n";
std::cout << std::format(" {:<15} {}\n", "-h, --help", "Display this help message, providing information about available options.");
std::cout << std::format(" {:<15} {}\n", "-v, --verbose", "Toggle verbose mode to control the level of detail in the output.");
std::cout << std::format(" {:<15} {}\n\n", "exit", "Terminate the REPL input loop.");
}

auto start() -> void {
while (is_running) {
auto text = std::string{};
std::cout << std::format("{} > ", verbose ? "[verbose]" : "");
std::getline(std::cin, text);
// run kalcy on input expression
if (!text.empty()) {
run(text);
} else {
print_help();
}
}
// print epilogue
std::cout << std::format("\n^^ kalcy v{}\n", kalcy::version_v);
}

auto run(std::string_view const text) -> bool {
NikolaJelic marked this conversation as resolved.
Show resolved Hide resolved
try {
if (text == "exit") {
is_running = false;
return true;
}

if (text == "-h" || text == "--help") {
print_help();
return true;
}

if (text == "-v" || text == "--verbose") {
verbose = !verbose;
return true;
}
// parse text into an expression
auto expr = parser.parse(text);
assert(expr != nullptr);
// evaluate parsed expression
// a custom Env can be used and passed, if desired
std::cout << kalcy::evaluate(*expr) << "\n";
// print AST if verbose
if (verbose) { std::cout << std::format("expression\t: {}\nAST\t\t: {}\n", text, kalcy::to_string(*expr)); }
return true;

} catch (kalcy::Error const& err) {
// print error
std::cerr << err.what() << "\n";
// highlight error location under text
std::cerr << " | " << text << "\n | " << err.build_highlight() << "\n";
return false;
}
}
};
} // namespace

auto main() -> int {

Repl repl{};
try {
repl.start();
NikolaJelic marked this conversation as resolved.
Show resolved Hide resolved
} catch (std::exception const& e) { std::cerr << e.what() << "\n"; }
}
Loading