diff --git a/README.md b/README.md index 73ea5197..daefbe06 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ $ cmake -B .build0 -S xo-cmake -DCMAKE_INSTALL_PREFIX=${PREFIX} $ cmake --build .build0 $ cmake --install .build0 # phase 2 -$ cmake -B .build -S . -DCMAKE_INSTALL_PREFIX=${PREFIX} +$ cmake -B .build -S . -DCMAKE_INSTALL_PREFIX=${PREFIX} -DXO_ENABLE_EXAMPLES=1 $ cmake --build .build $ cmake --install .build ``` diff --git a/xo-reader/examples/CMakeLists.txt b/xo-reader/examples/CMakeLists.txt new file mode 100644 index 00000000..220b7a89 --- /dev/null +++ b/xo-reader/examples/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(exprrepl) diff --git a/xo-reader/examples/exprrepl/CMakeLists.txt b/xo-reader/examples/exprrepl/CMakeLists.txt new file mode 100644 index 00000000..9296f119 --- /dev/null +++ b/xo-reader/examples/exprrepl/CMakeLists.txt @@ -0,0 +1,11 @@ +# xo-reader/example/exprrepl/CMakeLists.txt + +set(SELF_EXE xo_expression_repl) +set(SELF_SRCS exprrepl.cpp) + +if (XO_ENABLE_EXAMPLES) + xo_add_executable(${SELF_EXE} ${SELF_SRCS}) + xo_dependency(${SELF_EXE} xo_reader) +endif() + +# end CMakeLists.txt diff --git a/xo-reader/examples/exprrepl/exprrepl.cpp b/xo-reader/examples/exprrepl/exprrepl.cpp new file mode 100644 index 00000000..dbc9f587 --- /dev/null +++ b/xo-reader/examples/exprrepl/exprrepl.cpp @@ -0,0 +1,61 @@ +/** @file exprrepl.cpp **/ + +#include "xo/reader/reader.hpp" +#include +#include // for isatty + +bool repl_getline(bool interactive, std::istream& in, std::ostream& out, std::string& input) +{ + if (interactive) { + out << "> "; + std::flush(out); + } + + bool retval = static_cast(std::getline(in, input)); + + if (retval) { + // want reader to see newline, it's syntax + input.push_back('\n'); + } + + return retval; +} + +int +main() { + using namespace xo::scm; + using namespace std; + + using span_type = xo::scm::span; + + bool interactive = isatty(STDIN_FILENO); + + reader rdr; + rdr.begin_interactive_session(); + + string input_str; + + bool eof = false; + + span_type input; + + while (repl_getline(interactive, cin, cout, input_str)) { + input = span_type::from_string(input_str); + + while (!input.empty()) { + auto [expr, consumed] = rdr.read_expr(input, eof); + + if (expr) { + cout << expr << endl; + } + + input = input.after_prefix(consumed); + } + } + + auto [expr, _] = rdr.read_expr(input, true /*eof*/); + + if (expr) { + cout << expr << endl; + } +}