cln_test.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include <cln/cln.h>
  2. extern "C"
  3. {
  4. #include "lua.h"
  5. #include "lualib.h"
  6. #include "lauxlib.h"
  7. }
  8. #include <luabind/luabind.hpp>
  9. void bind_cln(lua_State* L)
  10. {
  11. using namespace luabind;
  12. using namespace cln;
  13. module(L)
  14. [
  15. // real numbers
  16. class_<cl_R>("cl_R")
  17. .def(constructor<>())
  18. .def(constructor<const cl_I&>())
  19. .def(constructor<float>())
  20. .def(constructor<const char*>())
  21. .def(tostring(const_self))
  22. .def(-self)
  23. .def(const_self + const_self)
  24. .def(const_self - const_self)
  25. .def(const_self * const_self)
  26. .def(const_self / const_self)
  27. .def(const_self <= const_self)
  28. .def(const_self < const_self)
  29. .def(const_self == const_self)
  30. .def(other<int>() + const_self)
  31. .def(other<int>() - const_self)
  32. .def(other<int>() * const_self)
  33. .def(other<int>() / const_self)
  34. .def(other<int>() <= const_self)
  35. .def(other<int>() < const_self)
  36. .def(const_self + other<int>())
  37. .def(const_self - other<int>())
  38. .def(const_self * other<int>())
  39. .def(const_self / other<int>())
  40. .def(const_self <= other<int>())
  41. .def(const_self < other<int>())
  42. ,
  43. // rational numbers
  44. class_<cl_RA, cl_R>("cl_RA")
  45. .def(constructor<>())
  46. .def(constructor<const cl_I&>())
  47. .def(constructor<int>())
  48. .def(constructor<const char*>())
  49. .def(tostring(const_self))
  50. .def(-self)
  51. .def(const_self + const_self)
  52. .def(const_self - const_self)
  53. .def(const_self * const_self)
  54. .def(const_self / const_self)
  55. .def(const_self <= const_self)
  56. .def(const_self < const_self)
  57. .def(const_self == const_self)
  58. .def(other<int>() + const_self)
  59. .def(other<int>() - const_self)
  60. .def(other<int>() * const_self)
  61. .def(other<int>() / const_self)
  62. .def(other<int>() <= const_self)
  63. .def(other<int>() < const_self)
  64. .def(const_self + other<int>())
  65. .def(const_self - other<int>())
  66. .def(const_self * other<int>())
  67. .def(const_self / other<int>())
  68. .def(const_self <= other<int>())
  69. .def(const_self < other<int>())
  70. ,
  71. // integers
  72. class_<cl_I, cl_RA>("cl_I")
  73. .def(constructor<>())
  74. .def(constructor<const cl_I&>())
  75. .def(constructor<int>())
  76. .def(constructor<const char*>())
  77. .def(tostring(const_self))
  78. .def(-self)
  79. .def(const_self + const_self)
  80. .def(const_self - const_self)
  81. .def(const_self * const_self)
  82. .def(const_self <= const_self)
  83. .def(const_self < const_self)
  84. .def(const_self == const_self)
  85. .def(other<int>() + const_self)
  86. .def(other<int>() - const_self)
  87. .def(other<int>() * const_self)
  88. .def(other<int>() <= const_self)
  89. .def(other<int>() < const_self)
  90. .def(const_self + other<int>())
  91. .def(const_self - other<int>())
  92. .def(const_self * other<int>())
  93. .def(const_self <= other<int>())
  94. .def(const_self < other<int>())
  95. ,
  96. def("factorial", &cln::factorial),
  97. def("sqrt", (const cl_R(*)(const cl_R&))&cln::sqrt)
  98. ];
  99. }
  100. int main()
  101. {
  102. lua_State* L = lua_open();
  103. lua_baselibopen(L);
  104. lua_mathlibopen(L);
  105. luabind::open(L);
  106. bind_cln(L);
  107. lua_dofile(L, "cln_test.lua");
  108. lua_close(L);
  109. return 0;
  110. }