--[[ Expression Language, Utilities Module CSci 450: Organization of Programming Languages, Fall 2016 H. Conrad Cunningham, Professor Computer and Information Science University of Mississippi 1234567890123456789012345678901234567890123456789012345678901234567890 2016-09-13: Separated from Assignment #1 code, KILT printTree added These functions are used for output and error messages. Public display functions: treeConcat, printTree Public error/debugging function: show_data --]] -- Function "treeConcat" takes a recursive list of lists in "array" -- argument "t" and returns the corresponding parenthesized traversal -- string. A list is stored in the low positive integer indices of an -- array-style Lua table. This assumes the structure is acyclic. local function treeConcat(t) if type(t) ~= "table" then return tostring(t) end local res = {} for i = 1, #t do res[i] = treeConcat(t[i]) end return "(" .. table.concat(res," ") .. ")" end -- Procedure "printTree" prints the corresponding parenthesized -- traversal string for its argument "t". local function printTree(t) print(treeConcat(t)) end -- Function "show_data" converts raw Lua data structures to strings to -- assist in debugging and testing. This assumes the structure is -- acyclic. local function show_data(d) if type(d) == "table" then local res = {} for k,v in pairs(d) do res[#res+1] = "[" .. show_data(k) .. "] = " .. show_data(v) end return "(" .. table.concat(res, ", ") .. ")" else return tostring(d) end end -- MODULE EXPORT LIST return { treeConcat = treeConcat, printTree = printTree, show_data = show_data }