--[[ Function Table 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-12: Built module from Core Interpreter Function Table code
2013-08-22: Added require of Utilities module and dumpFun function
2013-08-24: Cleaned dependencies and comments
            Repaired bug (typo) in dumpFun

1234567890123456789012345678901234567890123456789012345678901234567890

Table "funTab" stores the user-defined function definitions for later
use in function calls.  This is used by some of the Kamin interpreters
(e.g., Core and Lisp) but not all (e.g., Scheme).

--]]

-- KILT tree utilities library
local util = require "utilities"
local treeConcat = util.treeConcat

-- Function definition table
local funTab = {}

-- Procedure "defFun" inserts a user-defined definition with the given
-- "name", "arity", "arglist", and expression "body" into the Function
-- Table.  If the "name" is already defined, then the new definition
-- replaces the old.

local function defFun(name, arity, arglist, body)
  funTab[name] = {arity, arglist, body}
end

-- Function "getFun" returns the function definition for "name".  If
-- "name" is undefined, then "getFun" returns nil.

local function getFun(name) 
  return funTab[name]
end

-- Procedure "dumpFun" prints all bindings in the Function Definition
-- table.  It is intended for debugging.

local function dumpFun()
  print("Begin Function Table dump")
  for n,d in pairs(funTab) do
    print(n,treeConcat(d))
  end
  print("End Function Table dump")
end


-- MODULE EXPORT LIST

-- funTab itself is not exported 

return {
  defFun  = defFun,
  getFun  = getFun,
  dumpFun = dumpFun -- for debugging
}
