diff options
author | Philipp Gesang <pgesang@ix.urz.uni-heidelberg.de> | 2010-04-17 17:16:05 +0200 |
---|---|---|
committer | Philipp Gesang <pgesang@ix.urz.uni-heidelberg.de> | 2010-04-17 17:16:05 +0200 |
commit | 16e4920a632a4a7981381955bb4f35ae46565c96 (patch) | |
tree | 8cfb2a69eaeafd87acbe13350c0babf0b3f93eac /tex/context/third/transliterator/t-transliterator.mkiv | |
parent | d69d1ec6541c7956fae49c6354ee391044dd415e (diff) | |
download | transliterator-16e4920a632a4a7981381955bb4f35ae46565c96.tar.gz |
using metatables for table generation, allowing “addition” of dictionaries
Diffstat (limited to 'tex/context/third/transliterator/t-transliterator.mkiv')
-rw-r--r-- | tex/context/third/transliterator/t-transliterator.mkiv | 53 |
1 files changed, 44 insertions, 9 deletions
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\ |