From 16e4920a632a4a7981381955bb4f35ae46565c96 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Sat, 17 Apr 2010 17:16:05 +0200 Subject: =?UTF-8?q?using=20metatables=20for=20table=20generation,=20allowi?= =?UTF-8?q?ng=20=E2=80=9Caddition=E2=80=9D=20of=20dictionaries?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../third/transliterator/t-transliterator.mkiv | 53 ++++++++++++++++++---- 1 file changed, 44 insertions(+), 9 deletions(-) (limited to 'tex/context/third/transliterator/t-transliterator.mkiv') diff --git a/tex/context/third/transliterator/t-transliterator.mkiv b/tex/context/third/transliterator/t-transliterator.mkiv index ef8c15e..b4a3c86 100644 --- a/tex/context/third/transliterator/t-transliterator.mkiv +++ b/tex/context/third/transliterator/t-transliterator.mkiv @@ -54,7 +54,7 @@ translit.debug_count = 0 %D We want to keep track of all the tables we'll create so we put them into %D a separate dictionary accompanied by a description string. \startluacode -translit.tables = {} +translit.tables = translit.tables or {} \stopluacode @@ -96,6 +96,35 @@ translit.ru_consonants = {"б", "в", "г", "д", "ж", "з", "к", "л", "м", %D and %D trans_tables_trsc.lua.} +%D Metatables allow for easy concatenation. +\startluacode + +do + -- This returns the Union of both key sets for the “+” operator. + -- The values of the first table will be updated (read: overridden) by those given in the second. + local Dict_add = { + __add = function (dict_a, dict_b) + assert (type(dict_a) == "table" and type(dict_b) == "table") + local dict_result = setmetatable({}, Dict_add) + + for key, val in pairs(dict_a) do + dict_result[key] = val + end + + for key, val in pairs(dict_b) do + dict_result[key] = val + end + return dict_result + end + } + + function translit.make_add_dict(dict) + return setmetatable(dict, Dict_add) + end +end + +\stopluacode + %D The function \type{translit.subst(s, t)} is used to replace any key of %D \type{t} that occurs in \type{s} with the according value of \type{t}. @@ -116,14 +145,20 @@ end \startluacode -function translit.add_table (t, ...) - for _,tab in ipairs(arg) do - for key, value in pairs (tab) do - t[key] = value - end - end - return t -end +--function translit.add_table (...) + --print ("---------------HERE---------------") + --print (type(arg) .. ", Länge: " .. #arg) + --local t = translit.make_add_dict{} + --local cnt = 0 + --for _, tab in pairs(arg) do + --cnt=cnt+1 + --print(cnt .. " - " .. type(tab)) + --t = t + tab + --end + --print(cnt) + --for i,j in pairs(t) do print (i,j) end + --return t +--end \stopluacode %D We might want to have all the table data nicely formatted by \CONTEXT\ -- cgit v1.2.3