From c35170a83d3c507e2129df1a54728ff9f305e098 Mon Sep 17 00:00:00 2001 From: Jhon Pedroza Date: Mon, 19 Aug 2019 00:25:12 -0500 Subject: [PATCH] Added flags to lispy class. Properly clear screen --- builtin.cpp | 3 ++- lenv.hpp | 2 ++ lispy.cpp | 18 ++++++++++++++++-- lispy.hpp | 8 +++++++- 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/builtin.cpp b/builtin.cpp index 6b48a58..6448153 100644 --- a/builtin.cpp +++ b/builtin.cpp @@ -721,7 +721,8 @@ namespace repl { lval *clear(lenv *e, lval *a) { LASSERT_NUM_ARGS("clear", a, 0) - linenoiseClearScreen(); + auto lspy = lispy::instance(); + lspy->flags |= LISPY_FLAG_CLEAR_OUTPUT; return lval::sexpr(); } diff --git a/lenv.hpp b/lenv.hpp index 6d8211c..54d4426 100644 --- a/lenv.hpp +++ b/lenv.hpp @@ -21,9 +21,11 @@ struct lenv { std::vector keys() const; std::vector keys(const std::string &prefix) const; + lval *get(const std::string &sym) const; void put(const std::string &sym, const lval *const val); void def(const std::string &sym, const lval *const val); + void add_builtin_function(const std::string &name, lbuiltin func); void add_builtin_macro(const std::string &name, lbuiltin func); void add_builtin_command(const std::string &name, lbuiltin func); diff --git a/lispy.cpp b/lispy.cpp index 5fb7876..42f0ca2 100644 --- a/lispy.cpp +++ b/lispy.cpp @@ -13,7 +13,8 @@ using std::vector; lispy *lispy::_instance = nullptr; lispy::lispy() - : cmd_line("The Lispy interpreter", ' ', LISPY_VERSION), + : flags(LISPY_NO_FLAGS), + cmd_line("The Lispy interpreter", ' ', LISPY_VERSION), interactive_arg("i", "interactive", "Run REPL, even when -e is present or files are given", false), @@ -71,11 +72,13 @@ int lispy::run(int argc, char *argv[]) { } if ((evals.empty() && files.empty()) || interactive) { + flags |= LISPY_FLAG_INTERACTIVE; builtin::add_builtin_commands(&env); run_interactive(); } } catch (TCLAP::ArgException &e) { cerr << "Error: " << e.error() << " for arg " << e.argId() << endl; + return 1; } return 0; @@ -142,9 +145,10 @@ void lispy::run_interactive() { if (mpc_parse("", input, lispy_parser, &r)) { lval *result = lval::read((mpc_ast_t *)r.output); result = lval::eval(&env, result); - cout << *result << endl; + bool break_loop = process_interactive_result(result); delete result; mpc_ast_delete((mpc_ast_t *)r.output); + if (break_loop) break; } else { /* Otherwise Print the Error */ mpc_err_print(r.error); @@ -158,6 +162,16 @@ void lispy::run_interactive() { linenoiseHistoryFree(); } +bool lispy::process_interactive_result(lval *result) { + if (flags & LISPY_FLAG_CLEAR_OUTPUT) { + linenoiseClearScreen(); + return false; + } + + cout << *result << endl; + return false; +} + bool lispy::load_files(const vector &files) { for (auto file: files) { lval *args = lval::sexpr({new lval(file)}); diff --git a/lispy.hpp b/lispy.hpp index c47cac1..395a93a 100644 --- a/lispy.hpp +++ b/lispy.hpp @@ -6,6 +6,10 @@ #include "lenv.hpp" #include "mpc.h" +#define LISPY_NO_FLAGS 0x0 +#define LISPY_FLAG_INTERACTIVE 0x1 +#define LISPY_FLAG_CLEAR_OUTPUT 0x2 + class lispy { public: lispy(); @@ -16,15 +20,17 @@ class lispy { int run(int argc, char *argv[]); mpc_parser_t *parser(); + uint flags; + private: lenv env; static lispy *_instance; bool load_prelude(); void run_interactive(); + bool process_interactive_result(lval *result); bool load_files(const std::vector &files); bool eval_strings(const std::vector &strings); - void register_completion_hook(); friend void completion_hook(char const *prefix, linenoiseCompletions *lc);