ソースを参照

Implement term getters.

scossu 5 日 前
コミット
efcbcbbd01
2 ファイル変更75 行追加33 行削除
  1. 1 1
      include/term.h
  2. 74 32
      lua/lua_term.c

+ 1 - 1
include/term.h

@@ -455,7 +455,7 @@ LSUP_triple_free (LSUP_Triple *spo);
  * @return Corresponding triple term or NULL if n is out of range.
  */
 inline LSUP_Term *
-LSUP_triple_pos (const LSUP_Triple *trp, LSUP_TriplePos n)
+LSUP_triple_pos (const LSUP_Triple *trp, const LSUP_TriplePos n)
 {
     if (n == TRP_POS_S) return trp->s;
     if (n == TRP_POS_P) return trp->p;

+ 74 - 32
lua/lua_term.c

@@ -97,7 +97,7 @@ static int l_bnode_new (lua_State *L)
 }
 
 
-static int l_term_new_copy (lua_State *L)
+static int new_copy (lua_State *L)
 {
     LSUP_Term *src = check_term (L);
 
@@ -110,7 +110,7 @@ static int l_term_new_copy (lua_State *L)
 }
 
 
-static int l_term_new_from_buffer (lua_State *L)
+static int new_from_buffer (lua_State *L)
 {
     const LSUP_Buffer *sterm = *(LSUP_Buffer **)luaL_checkudata (
             L, 1, "LSUP.Buffer");
@@ -127,8 +127,7 @@ static int l_term_new_from_buffer (lua_State *L)
 /*
  * Class methods.
  */
-
-static int l_term_equals (lua_State *L)
+static int equals (lua_State *L)
 {
     LSUP_Term
         *t1 = check_term (L),
@@ -140,7 +139,7 @@ static int l_term_equals (lua_State *L)
 }
 
 
-static int l_term_gc (lua_State *L)
+static int gc (lua_State *L)
 {
     LSUP_Term *t = check_term (L);
     if (t) LSUP_term_free (t);
@@ -149,7 +148,7 @@ static int l_term_gc (lua_State *L)
 }
 
 
-static int l_term_get_hash (lua_State *L)
+static int get_hash (lua_State *L)
 {
     LSUP_Term *t = check_term (L);
 
@@ -159,7 +158,7 @@ static int l_term_get_hash (lua_State *L)
 }
 
 
-static int l_term_serialize (lua_State *L)
+static int serialize (lua_State *L)
 {
     LSUP_Term *t = check_term (L);
 
@@ -177,16 +176,24 @@ static int l_term_serialize (lua_State *L)
  * Getters.
  */
 
+// Forward declaration.
+static const struct luaL_Reg term_getters [];
+
 
-static int l_term_get_attr (lua_State *L)
+static int get_attr (lua_State *L)
 {
-    const char *attr = luaL_checkstring (L, 1);
-    // TODO generic getter.
+    const char *attr = luaL_checkstring (L, 2);
+
+    for (int i = 0; term_getters[i].name != NULL; i++) {
+        if (strcmp(term_getters[i].name, attr) == 0)
+            return term_getters[i].func (L);
+    }
 
+    // If the attribute doesn't exist, return nil.
     return 0;
 }
 
-static int l_term_get_data (lua_State *L)
+static int get_data (lua_State *L)
 {
     LSUP_Term *t = check_term (L);
     lua_pushstring (L, t->data);
@@ -195,7 +202,16 @@ static int l_term_get_data (lua_State *L)
 }
 
 
-static int l_term_get_iriref_nsm (lua_State *L)
+static int get_type (lua_State *L)
+{
+    LSUP_Term *t = check_term (L);
+    lua_pushinteger (L, t->type);
+
+    return 1;
+}
+
+
+static int get_iriref_nsm (lua_State *L)
 {
     LSUP_Term *t = check_term (L);
     // TODO
@@ -205,7 +221,7 @@ static int l_term_get_iriref_nsm (lua_State *L)
 }
 
 
-static int l_term_get_iriref_prefix (lua_State *L)
+static int get_iriref_prefix (lua_State *L)
 {
     LSUP_Term *t = check_term (L);
     lua_pushstring (L, LSUP_iriref_prefix (t));
@@ -214,7 +230,7 @@ static int l_term_get_iriref_prefix (lua_State *L)
 }
 
 
-static int l_term_get_iriref_path (lua_State *L)
+static int get_iriref_path (lua_State *L)
 {
     LSUP_Term *t = check_term (L);
     lua_pushstring (L, LSUP_iriref_path (t));
@@ -223,7 +239,7 @@ static int l_term_get_iriref_path (lua_State *L)
 }
 
 
-static int l_term_get_iriref_frag (lua_State *L)
+static int get_iriref_frag (lua_State *L)
 {
     LSUP_Term *t = check_term (L);
     lua_pushstring (L, LSUP_iriref_frag (t));
@@ -232,7 +248,7 @@ static int l_term_get_iriref_frag (lua_State *L)
 }
 
 
-static int l_term_get_lit_datatype (lua_State *L)
+static int get_lit_datatype (lua_State *L)
 {
     LSUP_Term *t = check_term (L);
     if (!LSUP_IS_LITERAL (t)) luaL_error (L, "Term is not a literal.");
@@ -243,7 +259,7 @@ static int l_term_get_lit_datatype (lua_State *L)
 }
 
 
-static int l_term_get_lit_lang (lua_State *L)
+static int get_lit_lang (lua_State *L)
 {
     LSUP_Term *t = check_term (L);
     if (!LSUP_IS_LITERAL (t)) luaL_error (L, "Term is not a literal.");
@@ -254,36 +270,62 @@ static int l_term_get_lit_lang (lua_State *L)
 }
 
 
+/*
+ * Setters.
+ */
+
+// Very simple for now.
+static int set_attr (lua_State *L)
+{ luaL_error (L, "Direct attribute setting is not allowed for this type."); }
+
+
 /*
  * Library setup.
  */
 
-static const struct luaL_Reg term_lib_fn [] = {
+static const luaL_Reg term_lib_fn [] = {
     {"iriref_new", l_iriref_new},
     {"iriref_new_abs", l_iriref_new_abs},
     {"iriref_new_rel", l_iriref_new_rel},
     {"lit_new", l_lit_new},
     {"bnode_new", l_bnode_new},
-    {"term_new_copy", l_term_new_copy},
+    {"term_new_copy", new_copy},
     {NULL}
 };
 
 
-static const struct luaL_Reg term_lib_meth [] = {
-    {"__eq", l_term_equals},
-    {"__gc", l_term_gc},
+static const luaL_Reg term_getters [] = {
+    // General getters.
+    {"data", get_data},
+    {"type", get_type},
+
+    // IRIRef getters.
+    {"nsm", get_iriref_nsm},
+    {"prefix", get_iriref_prefix},
+    {"path", get_iriref_path},
+    {"frag", get_iriref_frag},
+
+    // Literal getters.
+    {"datatype", get_lit_datatype},
+    {"lang", get_lit_lang},
+
+    {NULL}
+};
+
+
+static const luaL_Reg term_setters [] = {
+    {NULL}
+};
+
+static const luaL_Reg term_lib_meth [] = {
+    {"__eq", equals},
+    {"__gc", gc},
+    {"__index", get_attr},
+    {"__newindex", set_attr},
 
-    {"hash", l_term_get_hash},
-    {"serialize", l_term_serialize},
+    {"hash", get_hash},
+    {"serialize", serialize},
 
-    {"get_data", l_term_get_data},
-    {"get_type", l_term_get_data},
-    {"get_iriref_nsm", l_term_get_iriref_nsm},
-    {"get_iriref_prefix", l_term_get_iriref_prefix},
-    {"get_iriref_path", l_term_get_iriref_path},
-    {"get_iriref_frag", l_term_get_iriref_frag},
-    {"get_lit_datatype", l_term_get_lit_datatype},
-    {"get_lit_lang", l_term_get_lit_lang},
     {NULL}
 };