

Oct 25 19:56 2013 utilities.lua Page 1


--[[ Utilities Module
     KILT -- Kamin Interpreters in Lua Toolset
     H. Conrad Cunningham, Professor
     Computer and Information Science
     University of Mississippi

Developed for CSci 658, Software Language Engineering, Fall 2013

2013-08-22: Built from Core-Lisp-Scheme common interpreter code
2013-10-24: Added show_data function (from Complex Number package)

1234567890123456789012345678901234567890123456789012345678901234567890

--]]

-- Function "treeConcat" takes a recursive list of lists in 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.

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 is borrowed from the Complex
-- Number package.)

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








Oct 25 19:56 2013 utilities.lua Page 2



-- MODULE EXPORT LIST

return {
  treeConcat  = treeConcat,
  printTree   = printTree,
  show_data   = show_data
}


















































