Browse Source

Initial commit

master
Yessiest 3 months ago
commit
79d59d63b3
  1. 16
      Makefile
  2. 45
      qalculator.cpp
  3. BIN
      qalculator.o
  4. 3
      test.lua

16
Makefile

@ -0,0 +1,16 @@
LUA_VERSION=5.2
INCLUDES=-I/usr/lib/lua${LUA_VERSION}
CXX=g++
CFLAGS=-fPIC
LIBS=-lqalculate
OUTPUT=libqalculate.so
all: qalculator.o
${CXX} ${LIBS} -shared $< -o libqalculator.so
qalculator.o: qalculator.cpp
${CXX} ${INCLUDES} -c $< -o $@ ${CFLAGS}
clean:
${RM} libqalculator.so qalculator.o

45
qalculator.cpp

@ -0,0 +1,45 @@
#include <lua.hpp>
#include <libqalculate/qalculate.h>
#include <string>
const char* qalculate(const char* input, bool exact_mode) {
Calculator* c = new Calculator();
c->loadExchangeRates();
c->loadGlobalDefinitions();
c->loadLocalDefinitions();
std::string Input = input;
std::string Output;
if (exact_mode) {
default_user_evaluation_options.approximation = APPROXIMATION_EXACT;
} else {
default_user_evaluation_options.approximation = APPROXIMATION_APPROXIMATE;
}
Output = c->calculateAndPrint(Input,2000,default_user_evaluation_options);
delete c;
std::cout << Output << std::endl;
char * output = new char [Output.length()+1];
Output.copy(output,Output.length());
output[Output.length()] = "\0"[0];
return output;
}
extern "C"
int l_qalc(lua_State* L) {
const char * input = luaL_checkstring(L, 1);
bool exact_mode = lua_toboolean(L, 2);
const char * output = qalculate(input, exact_mode);
lua_pushstring(L, output);
return 1;
}
extern "C"
int luaopen_libqalculator(lua_State* L) {
static const struct luaL_Reg library [] =
{
{"qalc", l_qalc},
{NULL, NULL}
};
luaL_register(L, "qalc", library);
return 1;
}

BIN
qalculator.o

3
test.lua

@ -0,0 +1,3 @@
qalculate = require("libqalculator")
print(qalculate.qalc("sin(3 rad)"))
print(qalculate.qalc("sin(3 rad)",true))
Loading…
Cancel
Save