A binding to libqalculate for lua
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.2 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. #include <lua.hpp>
  2. #include <libqalculate/qalculate.h>
  3. #include <string>
  4. EvaluationOptions user_evaluation_options = default_evaluation_options;
  5. const char* qalculate(const char* input, bool exact_mode) {
  6. Calculator* c = new Calculator();
  7. c->loadExchangeRates();
  8. c->loadGlobalDefinitions();
  9. c->loadLocalDefinitions();
  10. std::string Input = input;
  11. std::string Output;
  12. if (exact_mode) {
  13. user_evaluation_options.approximation = APPROXIMATION_EXACT;
  14. } else {
  15. user_evaluation_options.approximation = APPROXIMATION_APPROXIMATE;
  16. }
  17. Output = c->calculateAndPrint(Input,2000,user_evaluation_options);
  18. delete c;
  19. char * output = new char [Output.length()+1];
  20. Output.copy(output,Output.length());
  21. output[Output.length()] = "\0"[0];
  22. return output;
  23. }
  24. extern "C"
  25. int l_qalc(lua_State* L) {
  26. const char * input = luaL_checkstring(L, 1);
  27. bool exact_mode = lua_toboolean(L, 2);
  28. const char * output = qalculate(input, exact_mode);
  29. lua_pushstring(L, output);
  30. return 1;
  31. }
  32. extern "C"
  33. int luaopen_libqalculator(lua_State* L) {
  34. static const struct luaL_Reg library [] =
  35. {
  36. {"qalc", l_qalc},
  37. {NULL, NULL}
  38. };
  39. luaL_register(L, "qalc", library);
  40. return 1;
  41. }