From 9f34a3f5acfde207c5c51b14aaba3e3302bc482c Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Tue, 14 Aug 2018 19:12:35 -0700 Subject: [PATCH] Add operator support. --- src/main.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/main.cpp b/src/main.cpp index 725382e..4b49efe 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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 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);