Add operator support.

This commit is contained in:
Danila Fedorin 2018-08-14 19:12:35 -07:00
parent 7825489b35
commit 9f34a3f5ac

View File

@ -222,6 +222,9 @@ class abacus {
public:
abacus();
void add_function(const std::string& name, libab_function_ptr ptr, const std::string& type);
void add_operator_infix(const std::string& op, const std::string& func, int assoc, int prec);
void add_operator_prefix(const std::string& op, const std::string& func);
void add_operator_postfix(const std::string& op, const std::string& func);
abacus_ref run(const std::string& code);
template <typename ... Ts>
abacus_ref call(const std::string& bane, Ts...params);
@ -251,6 +254,16 @@ void abacus::add_function(const std::string& name, libab_function_ptr ptr, const
}
}
void abacus::add_operator_infix(const std::string& op, const std::string& func, int assoc, int prec) {
libab_register_operator_infix(&ab, op.c_str(), prec, assoc, func.c_str());
}
void abacus::add_operator_prefix(const std::string& op, const std::string& func) {
libab_register_operator_prefix(&ab, op.c_str(), func.c_str());
}
void abacus::add_operator_postfix(const std::string& op, const std::string& func) {
libab_register_operator_postfix(&ab, op.c_str(), func.c_str());
}
abacus_ref abacus::run(const std::string& code) {
abacus_ref value;
libab_run_scoped(&ab, code.c_str(), scope, value);
@ -302,6 +315,12 @@ int main() {
ab.add_function("divide", function_divide, "(num, num)->num");
ab.add_function("negate", function_negate, "(num)->num");
ab.add_operator_infix("+", "plus", 1, -1);
ab.add_operator_infix("-", "minus", 1, -1);
ab.add_operator_infix("*", "times", 2, -1);
ab.add_operator_infix("/", "divide", 2, -1);
ab.add_operator_prefix("-", "negate");
while(!close_requested) {
std::cout << "> ";
std::getline(std::cin, buffer);