Ver código fonte

Prefix all Lua modules; add term set iteration.

scossu 2 semanas atrás
pai
commit
fdb0bbedb6
6 arquivos alterados com 103 adições e 14 exclusões
  1. 12 5
      lua/scratch.lua
  2. 0 0
      lua/src/lua_graph.c
  3. 0 0
      lua/src/lua_namespace.c
  4. 87 8
      lua/src/lua_term.c
  5. 0 0
      lua/src/lua_triple.c
  6. 4 1
      src/term.c

+ 12 - 5
lua/scratch.lua

@@ -2,27 +2,34 @@ term = require "lsup.term"
 triple = require "lsup.triple"
 graph = require "lsup.graph"
 
-t1 = term.new_bnode()
 ---[[
+t1 = term.new_bnode()
 t2 = term.new_iriref("urn:p:11")
 t3 = term.new_lit("123", "xsd:int")
+t4 = term.new_lit("Hola", nil, "es_ES")
+t5 = term.new_lit("مرحبا", nil, "ar_AR")
 
 trp1 = triple.new (
     term.new_iriref("urn:s:1"),
     term.new_iriref("urn:p:1"),
-    term.new_lit("hello", nil, "us-EN"))
+    term.new_lit("hello", nil, "en_US"))
 
-trp2 = triple.new (t1, t2, t3)
+triples = {
+    trp1,
+    triple.new (t1, t2, t3),
+    triple.new (t1, t2, t4),
+    triple.new (t1, t2, t5),
+}
 
 gr1 = graph.new()
-triples = {trp1, trp2}
 ct = gr1:add(triples)
 print("Triples added: " .. ct)
 gr2 = graph.new()
 for i in gr1:lookup() do print(i) end
 
 lm = gr1:connections(t1, term.LINK_OUTBOUND)
-for t1, ts in lm:iter() do
+print("Connections")
+for t1, ts in pairs(lm) do
     for t2 in pairs(ts) do print(t1, t2) end
 end
 --]]

+ 0 - 0
lua/src/graph.c → lua/src/lua_graph.c


+ 0 - 0
lua/src/namespace.c → lua/src/lua_namespace.c


+ 87 - 8
lua/src/term.c → lua/src/lua_term.c

@@ -15,6 +15,16 @@ LSUP_Term **allocate_term (lua_State *L)
 }
 
 
+LSUP_TermSet **allocate_tset (lua_State *L)
+{
+    LSUP_TermSet **ts_p = lua_newuserdatauv (L, sizeof (*ts_p), 1);
+    luaL_getmetatable (L, "LSUP.TermSet");
+    lua_setmetatable (L, -2);
+
+    return ts_p;
+}
+
+
 /*
  * Factory methods.
  */
@@ -310,7 +320,66 @@ static int l_term_set_attr (lua_State *L)
 
 
 /*
- * Ancillary types not directly created from Lua.
+ * Ancillary types
+ */
+
+/*
+ * Term set.
+ */
+
+static int term_set_iter_next (lua_State *L)
+{
+    LSUP_TermSet *ts =
+        *(LSUP_TermSet **)lua_touserdata (L, lua_upvalueindex (1));
+    size_t *ip = lua_touserdata (L, lua_upvalueindex (2));
+
+    LSUP_Term **tp = allocate_term (L);
+    LSUP_Term *tmp = NULL;
+    LSUP_rc rc = LSUP_term_set_next (ts, ip, &tmp);
+
+    LUA_PCHECK (rc, "Error iterating over term set");
+
+    if (rc == LSUP_END) {
+        free (ip);
+        return 0;
+    }
+
+    *tp = LSUP_term_copy (tmp);
+    if (!*tp) {
+        free (ip);
+        luaL_error (L, "Error allocating term.");
+    }
+
+    return 1;
+}
+
+
+static int l_term_set_iter_init (lua_State *L)
+{
+    size_t *ip = malloc (sizeof (*ip));
+    LUA_NLCHECK (ip, "Error allocating tset iterator.");
+    *ip = 0;
+
+    lua_pushlightuserdata (L, ip);
+    STACK_DUMP(L, "Before pushing tset next closure");
+    lua_pushcclosure (L, term_set_iter_next, 2);
+
+    return 1;
+}
+
+
+static int term_set_gc (lua_State *L)
+{
+    LSUP_TermSet **ts_p = lua_touserdata (L, 1);
+    LSUP_term_set_free (*ts_p);
+    *ts_p = NULL;
+
+    return 0;
+}
+
+
+/*
+ * Link map.
  */
 
 static int lmap_iter_next (lua_State *L)
@@ -328,14 +397,11 @@ static int lmap_iter_next (lua_State *L)
     LSUP_rc rc = LSUP_link_map_next (it, &tmp, &ts);
 
     if (rc != LSUP_OK) {
-        if (rc == LSUP_END) {
-            lua_pushnil (L);
-            lua_pushstring (L, "End of the loop.");
-            return 2;
-        }
+        if (rc == LSUP_END) return 0;
         else LUA_PCHECK (rc, "Error iterating over link map");
     }
     *link_p = LSUP_term_copy (tmp);
+    LUA_NLCHECK (*link_p, "Error allocating term.");
 
     size_t i = 0;
     tmp = NULL;
@@ -500,15 +566,28 @@ int luaopen_lsup_term (lua_State *L)
     // Set getters table as a value for the Term metatable.
     lua_setfield (L, -2, "getters");
 
-    // Metatables for ancillary types.
+    /*
+     * Metatables for ancillary types.
+     */
+    // Term set.
+    luaL_newmetatable (L, "LSUP.TermSet");
+    lua_pushvalue (L, -1);
+    lua_setfield (L, -2, "__index");
+    lua_pushcfunction (L, term_set_gc);
+    lua_setfield (L, -2, "__gc");
+    lua_pushcfunction (L, l_term_set_iter_init);
+    lua_setfield (L, -2, "__pairs");
+
+    // Link map.
     luaL_newmetatable (L, "LSUP.LinkMap");
     lua_pushvalue (L, -1);
     lua_setfield (L, -2, "__index");
     lua_pushcfunction (L, link_map_gc);
     lua_setfield (L, -2, "__gc");
     lua_pushcfunction (L, l_lmap_iter_init);
-    lua_setfield (L, -2, "iter");
+    lua_setfield (L, -2, "__pairs");
 
+    // Link map iterator.
     luaL_newmetatable (L, "LSUP.LMapIterator");
     lua_pushcfunction (L, lmap_iter_gc);
     lua_setfield (L, -2, "__gc");

+ 0 - 0
lua/src/triple.c → lua/src/lua_triple.c


+ 4 - 1
src/term.c

@@ -601,7 +601,10 @@ LSUP_term_set_next (LSUP_TermSet *ts, size_t *i, LSUP_Term **term)
 
 void
 LSUP_term_set_free (LSUP_TermSet *ts)
-{ hashmap_free (ts); }
+{
+    if (UNLIKELY (!ts)) return;
+    hashmap_free (ts);
+}
 
 
 LSUP_LinkMap *