lua_term.c 5.7 KB

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