-
Notifications
You must be signed in to change notification settings - Fork 0
Add simple REPL functionality #5
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e07f209
Add simple REPL functionality
NikolaJelic 32cb495
Fix: add return statement to start()
NikolaJelic daa8d4b
Add -help and use instructions
NikolaJelic 157ee54
Refactor code and resolve PR change requests
NikolaJelic e40eb1b
fix: crash on exit bug solved
NikolaJelic fd40057
refactor help message and input options
NikolaJelic aaa57fa
Merge branch 'main' into repl
NikolaJelic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
add_subdirectory(quickstart) | ||
add_subdirectory(repl) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
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"; } | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.