|
|
|
@ -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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|