lua_term.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 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 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 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 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 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 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. // Forward declaration.
  120. static const struct luaL_Reg term_getters [];
  121. static int get_attr (lua_State *L)
  122. {
  123. const char *attr = luaL_checkstring (L, 2);
  124. for (int i = 0; term_getters[i].name != NULL; i++) {
  125. if (strcmp(term_getters[i].name, attr) == 0)
  126. return term_getters[i].func (L);
  127. }
  128. // If the attribute doesn't exist, return nil.
  129. return 0;
  130. }
  131. static int get_data (lua_State *L)
  132. {
  133. LSUP_Term *t = check_term (L);
  134. lua_pushstring (L, t->data);
  135. return 1;
  136. }
  137. static int get_type (lua_State *L)
  138. {
  139. LSUP_Term *t = check_term (L);
  140. lua_pushinteger (L, t->type);
  141. return 1;
  142. }
  143. static int get_iriref_nsm (lua_State *L)
  144. {
  145. LSUP_Term *t = check_term (L);
  146. // TODO
  147. lua_pushlightuserdata (L, LSUP_iriref_nsm (t));
  148. return 1;
  149. }
  150. static int get_iriref_prefix (lua_State *L)
  151. {
  152. LSUP_Term *t = check_term (L);
  153. lua_pushstring (L, LSUP_iriref_prefix (t));
  154. return 1;
  155. }
  156. static int get_iriref_path (lua_State *L)
  157. {
  158. LSUP_Term *t = check_term (L);
  159. lua_pushstring (L, LSUP_iriref_path (t));
  160. return 1;
  161. }
  162. static int get_iriref_frag (lua_State *L)
  163. {
  164. LSUP_Term *t = check_term (L);
  165. lua_pushstring (L, LSUP_iriref_frag (t));
  166. return 1;
  167. }
  168. static int get_lit_datatype (lua_State *L)
  169. {
  170. LSUP_Term *t = check_term (L);
  171. if (!LSUP_IS_LITERAL (t)) luaL_error (L, "Term is not a literal.");
  172. lua_pushstring (L, t->datatype->data);
  173. return 1;
  174. }
  175. static int get_lit_lang (lua_State *L)
  176. {
  177. LSUP_Term *t = check_term (L);
  178. if (!LSUP_IS_LITERAL (t)) luaL_error (L, "Term is not a literal.");
  179. lua_pushstring (L, t->lang);
  180. return 1;
  181. }
  182. /*
  183. * Setters.
  184. */
  185. // Very simple for now.
  186. static int set_attr (lua_State *L)
  187. { luaL_error (L, "Direct attribute setting is not allowed for this type."); }
  188. /*
  189. * Library setup.
  190. */
  191. static const luaL_Reg term_lib_fn [] = {
  192. {"iriref_new", l_iriref_new},
  193. {"iriref_new_abs", l_iriref_new_abs},
  194. {"iriref_new_rel", l_iriref_new_rel},
  195. {"lit_new", l_lit_new},
  196. {"bnode_new", l_bnode_new},
  197. {"term_new_copy", new_copy},
  198. {NULL}
  199. };
  200. static const luaL_Reg term_getters [] = {
  201. // General getters.
  202. {"data", get_data},
  203. {"type", get_type},
  204. // IRIRef getters.
  205. {"nsm", get_iriref_nsm},
  206. {"prefix", get_iriref_prefix},
  207. {"path", get_iriref_path},
  208. {"frag", get_iriref_frag},
  209. // Literal getters.
  210. {"datatype", get_lit_datatype},
  211. {"lang", get_lit_lang},
  212. {NULL}
  213. };
  214. static const luaL_Reg term_setters [] = {
  215. {NULL}
  216. };
  217. static const luaL_Reg term_lib_meth [] = {
  218. {"__eq", equals},
  219. {"__gc", gc},
  220. {"__index", get_attr},
  221. {"__newindex", set_attr},
  222. {"hash", get_hash},
  223. {"serialize", serialize},
  224. {NULL}
  225. };
  226. int luaopen_term (lua_State *L)
  227. {
  228. luaL_newmetatable (L, "LSUP.Term");
  229. lua_pushvalue (L, -1);
  230. lua_setfield (L, -2, "__index");
  231. luaL_setfuncs (L, term_lib_meth, 0);
  232. luaL_newlib (L, term_lib_fn);
  233. return 1;
  234. }