Skip to content

Commit 7994773

Browse files
committed
Refactor code and resolve PR change requests
1 parent 6bbca1a commit 7994773

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

examples/repl/repl.cpp

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,51 +10,58 @@ struct Repl {
1010
const std::string_view EXIT_TOKEN{"exit"};
1111
bool verbose{}; // toggled on with '-v' and off with '-q'
1212
bool is_running{true};
13+
kalcy::Parser parser{};
1314

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

2223
auto start() -> bool {
2324
while (is_running) {
24-
std::string text{};
25+
auto text = std::string{};
2526
std::cout << std::format("{} > ", verbose ? "[verbose]" : "");
2627
std::getline(std::cin, text);
2728
// run kalcy on input expression
2829
if (!text.empty()) {
29-
if (!run(text)) { return EXIT_FAILURE; }
30+
if (!run(text)) { return EXIT_FAILURE; }
3031
} else {
3132
print_help();
3233
}
3334
}
3435
// print epilogue
3536
std::cout << std::format("\n^^ kalcy v{}\n", kalcy::version_v);
36-
return EXIT_SUCCESS;
37+
return true;
3738
}
3839

3940
auto run(std::string_view const text) -> bool {
4041
try {
4142
if (text == "exit") {
4243
is_running = false;
43-
} else if (text == "-v") {
44-
verbose = !verbose;
45-
} else if (text == "-h" || text == "-help") {
44+
return true;
45+
}
46+
47+
if (text == "-h" || text == "-help") {
4648
print_help();
49+
return true;
50+
}
51+
52+
if (text == "-v") {
53+
verbose = !verbose;
4754
} else {
4855
// parse text into an expression
49-
auto expr = kalcy::Parser{}.parse(text);
56+
auto expr = parser.parse(text);
5057
assert(expr != nullptr);
5158
// evaluate parsed expression
5259
// a custom Env can be used and passed, if desired
5360
std::cout << kalcy::evaluate(*expr) << "\n";
5461
// print AST if verbose
5562
if (verbose) { std::cout << std::format("expression\t: {}\nAST\t\t: {}\n", text, kalcy::to_string(*expr)); }
63+
return true;
5664
}
57-
return true;
5865
} catch (kalcy::Error const& err) {
5966
// print error
6067
std::cerr << err.what() << "\n";
@@ -69,5 +76,7 @@ struct Repl {
6976
auto main() -> int {
7077

7178
Repl repl{};
72-
repl.start();
79+
try {
80+
repl.start();
81+
} catch (std::exception const& e) { std::cerr << e.what() << "\n"; }
7382
}

0 commit comments

Comments
 (0)