Skip to content

Commit

Permalink
Refactor code and resolve PR change requests
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolaJelic committed Oct 6, 2023
1 parent 6bbca1a commit 7994773
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions examples/repl/repl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,58 @@ struct Repl {
const std::string_view EXIT_TOKEN{"exit"};
bool verbose{}; // toggled on with '-v' and off with '-q'
bool is_running{true};
kalcy::Parser parser{};

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

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

auto run(std::string_view const text) -> bool {
try {
if (text == "exit") {
is_running = false;
} else if (text == "-v") {
verbose = !verbose;
} else if (text == "-h" || text == "-help") {
return true;
}

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

if (text == "-v") {
verbose = !verbose;
} else {
// parse text into an expression
auto expr = kalcy::Parser{}.parse(text);
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;
}
return true;
} catch (kalcy::Error const& err) {
// print error
std::cerr << err.what() << "\n";
Expand All @@ -69,5 +76,7 @@ struct Repl {
auto main() -> int {

Repl repl{};
repl.start();
try {
repl.start();
} catch (std::exception const& e) { std::cerr << e.what() << "\n"; }
}

0 comments on commit 7994773

Please sign in to comment.