Skip to content

Commit

Permalink
Added flags to lispy class. Properly clear screen
Browse files Browse the repository at this point in the history
  • Loading branch information
jfpedroza committed Aug 19, 2019
1 parent 4004537 commit c35170a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
3 changes: 2 additions & 1 deletion builtin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
2 changes: 2 additions & 0 deletions lenv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ struct lenv {

std::vector<std::string> keys() const;
std::vector<const std::string *> 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);
Expand Down
18 changes: 16 additions & 2 deletions lispy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -142,9 +145,10 @@ void lispy::run_interactive() {
if (mpc_parse("<stdin>", 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);
Expand All @@ -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<string> &files) {
for (auto file: files) {
lval *args = lval::sexpr({new lval(file)});
Expand Down
8 changes: 7 additions & 1 deletion lispy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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<std::string> &files);
bool eval_strings(const std::vector<std::string> &strings);
void register_completion_hook();

friend void completion_hook(char const *prefix, linenoiseCompletions *lc);

Expand Down

0 comments on commit c35170a

Please sign in to comment.