Add function to reload rc file.
This commit is contained in:
		
							parent
							
								
									7e329be922
								
							
						
					
					
						commit
						9565d99fec
					
				@ -15,6 +15,7 @@ class abacus {
 | 
			
		||||
    private:
 | 
			
		||||
        libab ab;
 | 
			
		||||
        ref scope;
 | 
			
		||||
        ref rc_scope;
 | 
			
		||||
        std::map<std::string, ref> 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 <typename ... Ts>
 | 
			
		||||
        ref call(const std::string& bane, Ts...params);
 | 
			
		||||
 | 
			
		||||
@ -1,5 +1,7 @@
 | 
			
		||||
#include "abacus.hpp"
 | 
			
		||||
#include "functions.hpp"
 | 
			
		||||
#include <fstream>
 | 
			
		||||
#include <sstream>
 | 
			
		||||
 | 
			
		||||
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);
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										44
									
								
								src/main.cpp
									
									
									
									
									
								
							
							
						
						
									
										44
									
								
								src/main.cpp
									
									
									
									
									
								
							@ -3,8 +3,6 @@
 | 
			
		||||
#include <mpfr.h>
 | 
			
		||||
#include <readline/readline.h>
 | 
			
		||||
#include <readline/history.h>
 | 
			
		||||
#include <fstream>
 | 
			
		||||
#include <sstream>
 | 
			
		||||
#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;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user