lua_term.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. LSUP_Term *dtype;
  52. if (lang) *tp = LSUP_lt_literal_new (data, (char *)lang);
  53. else {
  54. dtype = (dtype_str) ? LSUP_iriref_new (dtype_str, NULL) : NULL;
  55. *tp = LSUP_literal_new (data, dtype);
  56. }
  57. if (!*tp) luaL_error (L, "Error while creating a term!");
  58. return 1;
  59. }
  60. static int l_bnode_new (lua_State *L)
  61. {
  62. const char *data = luaL_checkstring (L, 1);
  63. LSUP_Term **tp = allocate_term (L);
  64. *tp = LSUP_bnode_new (data);
  65. if (!*tp) luaL_error (L, "Error while creating a term!");
  66. return 1;
  67. }
  68. static int new_copy (lua_State *L)
  69. {
  70. LSUP_Term *src = check_term (L);
  71. LSUP_Term **tp = allocate_term (L);
  72. *tp = LSUP_term_copy (src);
  73. if (!*tp) luaL_error (L, "Error while creating a term!");
  74. return 1;
  75. }
  76. /*
  77. * Class methods.
  78. */
  79. static int equals (lua_State *L)
  80. {
  81. LSUP_Term
  82. *t1 = check_term (L),
  83. *t2 = *(LSUP_Term **)luaL_checkudata (L, 2, "LSUP.Term");
  84. lua_pushboolean (L, LSUP_term_equals (t1, t2));
  85. return 1;
  86. }
  87. static int gc (lua_State *L)
  88. {
  89. LSUP_Term *t = check_term (L);
  90. if (t) LSUP_term_free (t);
  91. return 0;
  92. }
  93. static int get_hash (lua_State *L)
  94. {
  95. LSUP_Term *t = check_term (L);
  96. lua_pushinteger (L, LSUP_term_hash (t));
  97. return 1;
  98. }
  99. /*
  100. static int serialize (lua_State *L)
  101. {
  102. LSUP_Term *t = check_term (L);
  103. LSUP_Buffer *buf = LSUP_term_serialize (t);
  104. if (!buf) luaL_error (L, "Error while serializing a term!");
  105. lua_pushfstring (L, buf->addr, buf->size);
  106. LSUP_buffer_free (buf);
  107. return 1;
  108. }
  109. */
  110. static int to_string (lua_State *L)
  111. {
  112. LSUP_Term *t = check_term (L);
  113. lua_pushstring (L, t->data);
  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. { return luaL_error (L, "Direct setting is not allowed for this type.", 2); }
  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. /*
  215. static const luaL_Reg term_setters [] = {
  216. {NULL}
  217. };
  218. */
  219. static const luaL_Reg term_lib_meth [] = {
  220. {"__eq", equals},
  221. {"__gc", gc},
  222. {"__index", get_attr},
  223. {"__tostring", to_string},
  224. {"__newindex", set_attr},
  225. {"hash", get_hash},
  226. //{"serialize", serialize},
  227. {NULL}
  228. };
  229. int luaopen_term (lua_State *L)
  230. {
  231. luaL_newmetatable (L, "LSUP.Term");
  232. lua_pushvalue (L, -1);
  233. lua_setfield (L, -2, "__index");
  234. luaL_setfuncs (L, term_lib_meth, 0);
  235. luaL_newlib (L, term_lib_fn);
  236. return 1;
  237. }