lua_term.c 6.2 KB

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