123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- #include "lua_lsup.h"
- #define check_triple(L) \
- *(LSUP_Triple **)luaL_checkudata(L, 1, "LSUP.Triple")
- /*
- * Factory methods.
- */
- static int new (lua_State *L)
- {
- LSUP_Triple **trp_p = lua_newuserdatauv (L, sizeof (*trp_p), 1);
- luaL_getmetatable (L, "LSUP.Triple");
- LSUP_Term
- *s = *(LSUP_Term **)luaL_checkudata(L, 2, "LSUP.Term"),
- *p = *(LSUP_Term **)luaL_checkudata(L, 3, "LSUP.Term"),
- *o = *(LSUP_Term **)luaL_checkudata(L, 4, "LSUP.Term");
- lua_setmetatable (L, -4);
- *trp_p = LSUP_triple_new (s, p, o);
- if (!*trp_p) luaL_error (L, "Error while creating a triple!");
- return 1;
- }
- /*
- * Class methods.
- */
- static int gc (lua_State *L)
- {
- LSUP_Triple *trp = check_triple (L);
- if (trp) LSUP_triple_free (trp);
- return 0;
- }
- static int get_term (lua_State *L)
- {
- const LSUP_Triple *trp = check_triple (L);
- const int pos = luaL_checkinteger (L, 2);
- if (pos < TRP_POS_S || pos > TRP_POS_O)
- luaL_error(L, "Out of range position: %d.", pos);
- lua_pushlightuserdata (L, LSUP_triple_pos (trp, (LSUP_TriplePos)pos));
- return 1;
- }
- static int set_term (lua_State *L)
- {
- LSUP_Triple *trp = check_triple (L);
- const int pos = luaL_checkinteger (L, 2);
- const LSUP_Term *t = *(LSUP_Term **)luaL_checkudata(L, 3, "LSUP.Term");
- LSUP_Term *new_t = LSUP_term_copy (t);
- if (pos == TRP_POS_S) {
- LSUP_term_free (trp->s);
- trp->s = new_t;
- } else if (pos == TRP_POS_P) {
- LSUP_term_free (trp->p);
- trp->p = new_t;
- } else if (pos == TRP_POS_O) {
- LSUP_term_free (trp->o);
- trp->o = new_t;
- } else return luaL_error(L, "Out of range position: %d.", pos);
- return 1;
- }
- /*
- * Library setup.
- */
- static const struct luaL_Reg triple_lib_fn [] = {
- {"new", new},
- {NULL}
- };
- static const struct luaL_Reg triple_lib_meth [] = {
- {"__gc", gc},
- {NULL}
- };
- int luaopen_triple (lua_State *L)
- {
- luaL_newmetatable (L, "LSUP.Triple");
- lua_pushvalue (L, -1);
- lua_setfield (L, -2, "__index");
- luaL_setfuncs (L, triple_lib_meth, 0);
- // Enums
- lua_pushvalue (L, TRP_POS_S);
- lua_setfield (L, -1, "TRP_POS_S");
- lua_pushvalue (L, TRP_POS_P);
- lua_setfield (L, -1, "TRP_POS_P");
- lua_pushvalue (L, TRP_POS_O);
- lua_setfield (L, -1, "TRP_POS_O");
- luaL_newlib (L, triple_lib_fn);
- return 1;
- }
|