From 9565d99fec2d5d02d6af390fa9ebf0ff9a5610e7 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Thu, 27 Sep 2018 20:58:51 -0700 Subject: [PATCH] Add function to reload rc file. --- include/abacus.hpp | 2 ++ src/abacus.cpp | 16 +++++++++++++++- src/main.cpp | 44 ++++++++++++++++++++------------------------ 3 files changed, 37 insertions(+), 25 deletions(-) diff --git a/include/abacus.hpp b/include/abacus.hpp index ae8985a..925fe6e 100644 --- a/include/abacus.hpp +++ b/include/abacus.hpp @@ -15,6 +15,7 @@ class abacus { private: libab ab; ref scope; + ref rc_scope; std::map compiled_types; libab_basetype basetype_string = { [](void* s) { delete ((string*) s); }, NULL, 0 }; public: @@ -26,6 +27,7 @@ class abacus { void add_operator_prefix(const std::string& op, const std::string& func); void add_operator_postfix(const std::string& op, const std::string& func); void add_standard(); + void add_rc(const std::string& file); ref run(const std::string& code); template ref call(const std::string& bane, Ts...params); diff --git a/src/abacus.cpp b/src/abacus.cpp index a07075e..5d4161d 100644 --- a/src/abacus.cpp +++ b/src/abacus.cpp @@ -1,5 +1,7 @@ #include "abacus.hpp" #include "functions.hpp" +#include +#include abacus::abacus() { auto parse_function = [](const char* s) { @@ -10,7 +12,8 @@ abacus::abacus() { }; libab_init(&ab, parse_function, free_function); libab_register_basetype(&ab, "str", &basetype_string); - libab_create_table(&ab, scope, &ab.table); + libab_create_table(&ab, rc_scope, &ab.table); + libab_create_table(&ab, scope, rc_scope); } const libab_basetype* abacus::get_basetype_string() { @@ -81,6 +84,17 @@ void abacus::add_standard() { add_operator_postfix("!", "factorial"); } +void abacus::add_rc(const std::string& file) { + std::ifstream rcfile(file); + std::ostringstream str; + ref value; + if(rcfile.good()) { + str << rcfile.rdbuf(); + libab_table_clear((libab_table*) libab_ref_get(rc_scope)); + libab_run_scoped(&ab, str.str().c_str(), rc_scope, value); + } +} + ref abacus::run(const std::string& code) { ref value; libab_run_scoped(&ab, code.c_str(), scope, value); diff --git a/src/main.cpp b/src/main.cpp index 9dc975c..60c2078 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,8 +3,6 @@ #include #include #include -#include -#include #include "types.hpp" #include "ref.hpp" #include "repl_functions.hpp" @@ -20,6 +18,7 @@ extern "C" { // == Global State (uh-oh) bool close_requested = false; long requested_precision = 3; +abacus abcs; FUNCTION(quit) { close_requested = true; @@ -27,6 +26,12 @@ FUNCTION(quit) { return LIBAB_SUCCESS; } +FUNCTION(reload_rc) { + abcs.add_rc("./.abcsrc"); + libab_get_unit_value(ab, into); + return LIBAB_SUCCESS; +} + FUNCTION(request_precision) { number* value = (number*) libab_unwrap_param(params, 0); requested_precision = std::min(PRECISION / 4, std::max(2, value->to_int())); @@ -34,47 +39,38 @@ FUNCTION(request_precision) { return LIBAB_SUCCESS; } -void run_rc(abacus& ab) { - std::ifstream rcfile("./.abcsrc"); - std::ostringstream str; - if(rcfile.good()) { - str << rcfile.rdbuf(); - ab.run(str.str()); - } -} - int main() { - abacus ab; rl_bind_key('\t', rl_insert); size_t index = 0; - ab.add_standard(); - ab.add_function("quit", function_quit, "()->unit"); - ab.add_function("request_precision", function_request_precision, "(num)->unit"); + abcs.add_standard(); + abcs.add_function("quit", function_quit, "()->unit"); + abcs.add_function("request_precision", function_request_precision, "(num)->unit"); + abcs.add_function("reload_rc", function_reload_rc, "()->unit"); - ab.add_function("print", function_print_string, "(str)->unit"); - ab.add_function("to_string", function_to_string_num, "(num)->str"); - ab.add_function("to_string", function_to_string_bool, "(bool)->str"); - ab.add_function("to_string", function_to_string_unit, "(unit)->str"); + abcs.add_function("print", function_print_string, "(str)->unit"); + abcs.add_function("to_string", function_to_string_num, "(num)->str"); + abcs.add_function("to_string", function_to_string_bool, "(bool)->str"); + abcs.add_function("to_string", function_to_string_unit, "(unit)->str"); - run_rc(ab); + abcs.add_rc("./.abcsrc"); while(!close_requested) { char* data = readline(" > "); std::string buffer(data); add_history(data); free(data); - ref value = ab.run(buffer); + ref value = abcs.run(buffer); if(value == nullptr) { std::cout << "Invalid expression." << std::endl; } else { std::string name = "r" + std::to_string(index); std::string ans = "ans"; - ab.add_variable(name, value); - ab.add_variable(ans, value); + abcs.add_variable(name, value); + abcs.add_variable(ans, value); index++; - std::cout << name << " = " << ab.to_string(value) << std::endl; + std::cout << name << " = " << abcs.to_string(value) << std::endl; } } }