Browse Source

Move common function declarations to header file.

scossu 2 days ago
parent
commit
32e19ee6ec
4 changed files with 43 additions and 47 deletions
  1. 4 25
      lua/src/lua_lsup.h
  2. 1 1
      lua/src/namespace.c
  3. 28 20
      lua/src/term.c
  4. 10 1
      lua/src/triple.c

+ 4 - 25
lua/src/lua_lsup.h

@@ -12,31 +12,10 @@
         return luaL_error (L, "%s: %s", message, LSUP_strerror (exp)); \
 } while (0)
 
-// Size of printf("%p", <var>)
-#define ADDR_BUF_SIZE sizeof (size_t) * 2 + 2
-typedef char LSUP_AddrBuffer[ADDR_BUF_SIZE];
+/// Create new namespace map.
+int l_nsmap_new (lua_State *L);
 
+/// Allocate space for a term object.
+LSUP_Term **allocate_term (lua_State *L);
 
-int term_to_string (lua_State *L, LSUP_Term *t)
-{
-    char *nt_term = NULL;
-    CHECK (nt_codec.encode_term (t, NULL, &nt_term), fail);
-    lua_pushfstring (L, "LSUP.Term @ %p %s", t, nt_term);
-
-    return 1;
-
-fail:
-    if (nt_term) free (nt_term);
-    return luaL_error (L, "Error encoding term for display!");
-}
-
-
-LSUP_Term **allocate_term (lua_State *L)
-{
-    LSUP_Term **tp = lua_newuserdatauv (L, sizeof (*tp), 1);
-    luaL_getmetatable (L, "LSUP.Term");
-    lua_setmetatable (L, -2);
-
-    return tp;
-}
 #endif  // _LUA_LSUP_H

+ 1 - 1
lua/src/namespace.c

@@ -8,7 +8,7 @@
  * Factory methods.
  */
 
-static int l_nsmap_new (lua_State *L)
+int l_nsmap_new (lua_State *L)
 {
     LSUP_NSMap **nsm_p = lua_newuserdatauv (L, sizeof (*nsm_p), 1);
     luaL_getmetatable (L, "LSUP.NSMap");

+ 28 - 20
lua/src/term.c

@@ -4,6 +4,16 @@
     *(LSUP_Term **)luaL_checkudata(L, 1, "LSUP.Term")
 
 
+LSUP_Term **allocate_term (lua_State *L)
+{
+    LSUP_Term **tp = lua_newuserdatauv (L, sizeof (*tp), 1);
+    luaL_getmetatable (L, "LSUP.Term");
+    lua_setmetatable (L, -2);
+
+    return tp;
+}
+
+
 /*
  * Factory methods.
  */
@@ -125,37 +135,35 @@ static int gc (lua_State *L)
 }
 
 
-/*
-static int serialize (lua_State *L)
+static int to_string (lua_State *L)
 {
     LSUP_Term *t = check_term (L);
+    char *nt_term = NULL;
 
-    LSUP_Buffer *buf = LSUP_term_serialize (t);
-    if (!buf) return luaL_error (L, "Error while serializing a term!");
-
-    lua_pushfstring (L, buf->addr, buf->size);
-    LSUP_buffer_free (buf);
+    CHECK (nt_codec.encode_term (t, NULL, &nt_term), fail);
+    lua_pushfstring (L, "LSUP.Term @ %p %s", t, nt_term);
 
     return 1;
+
+fail:
+    if (nt_term) free (nt_term);
+    return luaL_error (L, "Error encoding term for display!");
 }
-*/
 
 
-static int to_string (lua_State *L)
+static int to_n3 (lua_State *L)
 {
     LSUP_Term *t = check_term (L);
+    char *nt_term = NULL;
 
-    return term_to_string(L, t);
-}
-
+    CHECK (nt_codec.encode_term (t, NULL, &nt_term), fail);
+    lua_pushstring (L, nt_term);
 
-static int echo (lua_State *L)
-{
-    LSUP_Term *t = check_term (L);
-    const char *s = luaL_checkstring (L, 2);
-    printf("Echo: %s\n", s);
+    return 1;
 
-    return 0;
+fail:
+    if (nt_term) free (nt_term);
+    return luaL_error (L, "Error encoding term for display!");
 }
 
 
@@ -281,6 +289,7 @@ static int get_hash (lua_State *L)
  */
 
 // Very simple for now.
+//static const luaL_Reg term_setters [];
 static int set_attr (lua_State *L)
 { return luaL_error (L, "Direct setting is not allowed for this type.", 2); }
 
@@ -336,8 +345,7 @@ static const luaL_Reg term_lib_meth [] = {
     {"__tostring", to_string},
     {"__newindex", set_attr},
 
-    {"echo", echo},
-    //{"serialize", serialize},
+    {"to_n3", to_n3},
     {NULL}
 };
 

+ 10 - 1
lua/src/triple.c

@@ -59,7 +59,16 @@ static int get_term (lua_State *L)
     else if (pos == 'o') pn = TRP_POS_O;
     else return luaL_error(L, "Out of range position: %c.", pos);
 
-    return term_to_string (L, LSUP_triple_pos (trp, pn));
+    LSUP_Term *t = LSUP_triple_pos (trp, pn);
+    char *nt_term = NULL;
+    CHECK (nt_codec.encode_term (t, NULL, &nt_term), fail);
+    lua_pushfstring (L, "LSUP.Term @ %p %s", t, nt_term);
+
+    return 1;
+
+fail:
+    if (nt_term) free (nt_term);
+    return luaL_error (L, "Error encoding triple term for display!");
 }