Advertisement
C4Cypher

table_value

Sep 1st, 2014
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 1.10 KB | None | 0 0
  1. table_value(Table, Key, Value, L) :-
  2.     semipure det_checkstack(L, 6),
  3.     semipure var_type(Table, table_type, L),
  4.     impure lua_newtable(L), % Memo set table
  5.     impure push_var(Table, L),
  6.     impure lua_pushboolean(L, yes),
  7.     impure lua_rawset(L, -3), % Add the Table to the memo set table
  8.     semipure Last = lua_toref(L, -1),
  9.     impure lua_pop(L, 1).
  10.     semipure table_value(Table, Key, Value, Last, L).
  11.        
  12. :- semipure pred table_value(var, value, value, ref, lua).
  13. :- mode valid_table(in, out, out, in, in) is nondet.
  14.  
  15. table_value(Table, Key, Value, Last, L) :-
  16.     impure push_var(Table, L)   % Table being iterated
  17.     impure lua_pushref(L, Last),    % Last key
  18.     impure lua_next(L, -2), % Pop the last key and push the next pair
  19.     % The stack should now look like [Table, Key, Value]
  20.     semipure lua_isnil(L, -2) ->    % Is there another pair?
  21.         impure lua_pop(L, 3),   % Clear the stack
  22.         fail            % There are no more pairs
  23.     ;
  24.         semipure Next =  lua_toref(L, -2),
  25.         semipure Key = to_value(-2, L),
  26.         semipure Value = to_value(-1, L),
  27.         impure lua_pop(L, 3), % Clear the stack
  28.     ;
  29.         table_value(Table, Key, Value, Next, L).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement