about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2021-11-26 18:17:35 -0800
committerKartik K. Agaram <vc@akkartik.com>2021-11-26 18:17:35 -0800
commit720ff1935118cf6f7e1fe8310766066f4a77f7d7 (patch)
tree188a0eaf770a36d1e0d1a0d43210909483de3d12 /src
parent7295ce050d2bbc9eb0df8fee7e7ad0881631080b (diff)
downloadteliva-720ff1935118cf6f7e1fe8310766066f4a77f7d7.tar.gz
add support for metadata in Teliva programs
They don't have any semantics yet. We just ignore them for now.
Diffstat (limited to 'src')
-rw-r--r--src/lua.c38
1 files changed, 26 insertions, 12 deletions
diff --git a/src/lua.c b/src/lua.c
index 3d7945c..1adcb95 100644
--- a/src/lua.c
+++ b/src/lua.c
@@ -291,6 +291,22 @@ static int binding_exists (lua_State *L, const char *name) {
 }
 
 
+static const char *special_history_keys[] = {
+  "__teliva_timestamp",
+  "__teliva_undo",
+  "__teliva_note",
+  NULL,
+};
+
+int is_special_history_key(const char *key) {
+  for (const char **curr = special_history_keys; *curr != NULL; ++curr) {
+    if (strcmp(*curr, key) == 0)
+      return 1;
+  }
+  return 0;
+}
+
+
 static const char *look_up_definition (lua_State *L, const char *name) {
   lua_getglobal(L, "teliva_program");
   int history_array = lua_gettop(L);
@@ -303,6 +319,7 @@ static const char *look_up_definition (lua_State *L, const char *name) {
     /* really we expect only one */
     for (lua_pushnil(L); lua_next(L, table) != 0; lua_pop(L, 1)) {
       const char* key = lua_tostring(L, -2);
+      if (is_special_history_key(key)) continue;
       if (strcmp(key, name) == 0)
         return lua_tostring(L, -1);
     }
@@ -325,6 +342,7 @@ int resume_loading_definitions(lua_State *L) {
     /* really we expect only one */
     for (lua_pushnil(L); lua_next(L, table) != 0; lua_pop(L, 1)) {
       const char* key = lua_tostring(L, -2);
+      if (is_special_history_key(key)) continue;
       if (binding_exists(L, key))
         continue;  // most recent binding trumps older ones
       const char* value = lua_tostring(L, -1);
@@ -655,8 +673,9 @@ restart:
   for (int i = history_array_size; i > 0; --i) {
     lua_rawgeti(L, history_array, i);
     int t = lua_gettop(L);
-    for (lua_pushnil(L); lua_next(L, t) != 0;) {
+    for (lua_pushnil(L); lua_next(L, t) != 0; lua_pop(L, 1)) {
       const char *definition_name = lua_tostring(L, -2);
+      if (is_special_history_key(definition_name)) continue;
       lua_getglobal(L, definition_name);
       int is_userdata = lua_isuserdata(L, -1);
       int is_function = lua_isfunction(L, -1);
@@ -669,8 +688,6 @@ restart:
         if (is_current_definition(L, definition_name, i, history_array, history_array_size))
           draw_definition_name(definition_name);
       }
-      lua_pop(L, 1);  // value
-      // leave key on stack for next iteration
     }
     lua_pop(L, 1);  // history element
   }
@@ -679,8 +696,9 @@ restart:
   for (int i = history_array_size; i > 0; --i) {
     lua_rawgeti(L, history_array, i);
     int t = lua_gettop(L);
-    for (lua_pushnil(L); lua_next(L, t) != 0;) {
+    for (lua_pushnil(L); lua_next(L, t) != 0; lua_pop(L, 1)) {
       const char* definition_name = lua_tostring(L, -2);
+      if (is_special_history_key(definition_name)) continue;
       lua_getglobal(L, definition_name);
       int is_userdata = lua_isuserdata(L, -1);
       lua_pop(L, 1);
@@ -690,8 +708,6 @@ restart:
         if (is_current_definition(L, definition_name, i, history_array, history_array_size))
           draw_definition_name(definition_name);
       }
-      lua_pop(L, 1);  // value
-      // leave key on stack for next iteration
     }
     lua_pop(L, 1);  // history element
   }
@@ -705,16 +721,15 @@ restart:
     for (int i = history_array_size; i > 0; --i) {
       lua_rawgeti(L, history_array, i);
       int t = lua_gettop(L);
-      for (lua_pushnil(L); lua_next(L, t) != 0;) {
+      for (lua_pushnil(L); lua_next(L, t) != 0; lua_pop(L, 1)) {
         const char* definition_name = lua_tostring(L, -2);
+        if (is_special_history_key(definition_name)) continue;
         lua_getfield(L, cgt, definition_name);
         int depth = lua_tointeger(L, -1);
         if (depth == level)
           if (is_current_definition(L, definition_name, i, history_array, history_array_size))
             draw_definition_name(definition_name);
         lua_pop(L, 1);  // depth of value
-        lua_pop(L, 1);  // value
-        // leave key on stack for next iteration
       }
       lua_pop(L, 1);  // history element
     }
@@ -726,8 +741,9 @@ restart:
   for (int i = history_array_size; i > 0; --i) {
     lua_rawgeti(L, history_array, i);
     int t = lua_gettop(L);
-    for (lua_pushnil(L); lua_next(L, t) != 0;) {
+    for (lua_pushnil(L); lua_next(L, t) != 0; lua_pop(L, 1)) {
       const char* definition_name = lua_tostring(L, -2);
+      if (is_special_history_key(definition_name)) continue;
       lua_getglobal(L, definition_name);
       int is_function = lua_isfunction(L, -1);
       lua_pop(L, 1);
@@ -736,8 +752,6 @@ restart:
         if (is_current_definition(L, definition_name, i, history_array, history_array_size))
           draw_definition_name(definition_name);
       lua_pop(L, 1);  // depth of value
-      lua_pop(L, 1);  // value
-      // leave key on stack for next iteration
     }
     lua_pop(L, 1);  // history element
   }