|
@@ -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}
|
|
|
};
|
|
|
|