lua_term.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. #include <lua.h>
  2. //#include <lualib.h>
  3. #include <lauxlib.h>
  4. #include <term.h>
  5. #define check_term(L) \
  6. *(LSUP_Term **)luaL_checkudata(L, 1, "LSUP.Term")
  7. lua_State *L;
  8. static LSUP_Term **allocate_term (lua_State *L)
  9. {
  10. LSUP_Term **tp = lua_newuserdatauv (L, sizeof (*tp), 1);
  11. luaL_getmetatable (L, "LSUP.Term");
  12. lua_setmetatable (L, -2);
  13. return tp;
  14. }
  15. /*
  16. * Factory methods.
  17. */
  18. static int l_iriref_new (lua_State *L)
  19. {
  20. const char *data = luaL_checkstring (L, 1);
  21. // TODO handle nsm.
  22. LSUP_NSMap *nsm = lua_touserdata (L, 2);
  23. LSUP_Term **tp = allocate_term (L);
  24. *tp = LSUP_iriref_new (data, nsm);
  25. if (!*tp) luaL_error (L, "Error while creating a term!");
  26. return 1;
  27. }
  28. static int l_iriref_new_abs (lua_State *L)
  29. {
  30. LSUP_Term
  31. *root = check_term (L),
  32. *iri = *(LSUP_Term **)luaL_checkudata (L, 2, "LSUP.Term");
  33. LSUP_Term **tp = allocate_term (L);
  34. *tp = LSUP_iriref_absolute (root, iri);
  35. if (!*tp) luaL_error (L, "Error while creating a term!");
  36. return 1;
  37. }
  38. static int l_iriref_new_rel (lua_State *L)
  39. {
  40. LSUP_Term
  41. *root = check_term (L),
  42. *iri = *(LSUP_Term **)luaL_checkudata (L, 2, "LSUP.Term");
  43. LSUP_Term **tp = allocate_term (L);
  44. *tp = LSUP_iriref_relative (root, iri);
  45. if (!*tp) luaL_error (L, "Error while creating a term!");
  46. return 1;
  47. }
  48. static int l_lit_new (lua_State *L)
  49. {
  50. const char
  51. *data = luaL_checkstring (L, 1),
  52. *dtype_str = luaL_checkstring (L, 2);
  53. const char *lang = luaL_checkstring (L, 3);
  54. LSUP_Term **tp = allocate_term (L);
  55. if (lang) *tp = LSUP_lt_literal_new (data, (char *)lang);
  56. else {
  57. LSUP_Term *dtype = (dtype) ? LSUP_iriref_new (dtype_str, NULL) : NULL;
  58. *tp = LSUP_literal_new (data, dtype);
  59. }
  60. if (!*tp) luaL_error (L, "Error while creating a term!");
  61. return 1;
  62. }
  63. static int l_bnode_new (lua_State *L)
  64. {
  65. const char *data = luaL_checkstring (L, 1);
  66. LSUP_Term **tp = allocate_term (L);
  67. *tp = LSUP_bnode_new (data);
  68. if (!*tp) luaL_error (L, "Error while creating a term!");
  69. return 1;
  70. }
  71. static int l_term_new_copy (lua_State *L)
  72. {
  73. LSUP_Term *src = check_term (L);
  74. LSUP_Term **tp = allocate_term (L);
  75. *tp = LSUP_term_copy (src);
  76. if (!*tp) luaL_error (L, "Error while creating a term!");
  77. return 1;
  78. }
  79. static int l_term_new_from_buffer (lua_State *L)
  80. {
  81. const LSUP_Buffer *sterm = *(LSUP_Buffer **)luaL_checkudata (
  82. L, 1, "LSUP.Buffer");
  83. LSUP_Term **tp = allocate_term (L);
  84. *tp = LSUP_term_new_from_buffer (sterm);
  85. if (!*tp) luaL_error (L, "Error while creating a term!");
  86. return 1;
  87. }
  88. /*
  89. * Class methods.
  90. */
  91. static int l_term_equals (lua_State *L)
  92. {
  93. LSUP_Term
  94. *t1 = check_term (L),
  95. *t2 = *(LSUP_Term **)luaL_checkudata (L, 2, "LSUP.Term");
  96. lua_pushboolean (L, LSUP_term_equals (t1, t2));
  97. return 1;
  98. }
  99. static int l_term_gc (lua_State *L)
  100. {
  101. LSUP_Term *t = check_term (L);
  102. if (t) LSUP_term_free (t);
  103. return 0;
  104. }
  105. static int l_term_get_hash (lua_State *L)
  106. {
  107. LSUP_Term *t = check_term (L);
  108. lua_pushinteger (L, LSUP_term_hash (t));
  109. return 1;
  110. }
  111. static int l_term_serialize (lua_State *L)
  112. {
  113. LSUP_Term *t = check_term (L);
  114. LSUP_Buffer *buf = LSUP_term_serialize (t);
  115. if (!buf) luaL_error (L, "Error while serializing a term!");
  116. lua_pushfstring (L, buf->addr, buf->size);
  117. LSUP_buffer_free (buf);
  118. return 1;
  119. }
  120. /*
  121. * Getters.
  122. */
  123. static int l_term_get_attr (lua_State *L)
  124. {
  125. const char *attr = luaL_checkstring (L, 1);
  126. // TODO generic getter.
  127. return 0;
  128. }
  129. static int l_term_get_data (lua_State *L)
  130. {
  131. LSUP_Term *t = check_term (L);
  132. lua_pushstring (L, t->data);
  133. return 1;
  134. }
  135. static int l_term_get_iriref_nsm (lua_State *L)
  136. {
  137. LSUP_Term *t = check_term (L);
  138. // TODO
  139. lua_pushlightuserdata (L, LSUP_iriref_nsm (t));
  140. return 1;
  141. }
  142. static int l_term_get_iriref_prefix (lua_State *L)
  143. {
  144. LSUP_Term *t = check_term (L);
  145. lua_pushstring (L, LSUP_iriref_prefix (t));
  146. return 1;
  147. }
  148. static int l_term_get_iriref_path (lua_State *L)
  149. {
  150. LSUP_Term *t = check_term (L);
  151. lua_pushstring (L, LSUP_iriref_path (t));
  152. return 1;
  153. }
  154. static int l_term_get_iriref_frag (lua_State *L)
  155. {
  156. LSUP_Term *t = check_term (L);
  157. lua_pushstring (L, LSUP_iriref_frag (t));
  158. return 1;
  159. }
  160. static int l_term_get_lit_datatype (lua_State *L)
  161. {
  162. LSUP_Term *t = check_term (L);
  163. if (!LSUP_IS_LITERAL (t)) luaL_error (L, "Term is not a literal.");
  164. lua_pushstring (L, t->datatype->data);
  165. return 1;
  166. }
  167. static int l_term_get_lit_lang (lua_State *L)
  168. {
  169. LSUP_Term *t = check_term (L);
  170. if (!LSUP_IS_LITERAL (t)) luaL_error (L, "Term is not a literal.");
  171. lua_pushstring (L, t->lang);
  172. return 1;
  173. }
  174. /*
  175. * Library setup.
  176. */
  177. static const struct luaL_Reg term_lib_fn [] = {
  178. {"iriref_new", l_iriref_new},
  179. {"iriref_new_abs", l_iriref_new_abs},
  180. {"iriref_new_rel", l_iriref_new_rel},
  181. {"lit_new", l_lit_new},
  182. {"bnode_new", l_bnode_new},
  183. {"term_new_copy", l_term_new_copy},
  184. {NULL}
  185. };
  186. static const struct luaL_Reg term_lib_meth [] = {
  187. {"__eq", l_term_equals},
  188. {"__gc", l_term_gc},
  189. {"hash", l_term_get_hash},
  190. {"serialize", l_term_serialize},
  191. {"get_data", l_term_get_data},
  192. {"get_type", l_term_get_data},
  193. {"get_iriref_nsm", l_term_get_iriref_nsm},
  194. {"get_iriref_prefix", l_term_get_iriref_prefix},
  195. {"get_iriref_path", l_term_get_iriref_path},
  196. {"get_iriref_frag", l_term_get_iriref_frag},
  197. {"get_lit_datatype", l_term_get_lit_datatype},
  198. {"get_lit_lang", l_term_get_lit_lang},
  199. {NULL}
  200. };
  201. int luaopen_term (lua_State *L)
  202. {
  203. luaL_newmetatable (L, "LSUP.Term");
  204. lua_pushvalue (L, -1);
  205. lua_setfield (L, -2, "__index");
  206. luaL_setfuncs (L, term_lib_meth, 0);
  207. luaL_newlib (L, term_lib_fn);
  208. return 1;
  209. }