--[[ Kamin Core Language Values 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-19: Built Scheme module from Scheme interpreter code
2013-08-24: Reverted Lisp/Scheme changes to build Core module
2013-08-29: Changed to require Opcode Factory module

1234567890123456789012345678901234567890123456789012345678901234567890

--]]

-- KILT Interpreter Opcode Global Constants
local opcodes = require "opcodes"

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

-- Function "hasOpCode" determines whether the first item in
-- array-style table "t" equals "op".  In particular, "op" is
-- expected to be one of the semantic opcodes.

local function hasOpCode(t,op)
  return (type(t) == "table" and t[1] == op)
end

-- HANDLE FALSE AND TRUE VALUES IN INTERPRETED LANGUAGE

-- The representation of the concepts false and true differs from one
-- of the Kamin interpreted languages to another and from the values
-- used by Lua.  The following constants and functions enable the Lua
-- program to manipulate the interpreted language "booleans".

local FALSEVAL = 0
local TRUEVAL  = 1

local function isFalse(v)
  return v == FALSEVAL
end

local function isTrue(v)
  return v ~= FALSEVAL
end

-- Function "boolAsVal" takes a Lua boolean (false or true) and
-- returns the corresponding value used in the interpreted language
-- (FALSEVAL or TRUEVAL).

local function boolAsVal(b) 
  if b then return TRUEVAL else return FALSEVAL end 
end


-- CONVERT LANGUAGE VALUES TO STRINGS

-- Function "valToString converts value "v" to a string and 
-- returns it.

local function valToString(v)
  local tv = type(v)
  if tv == "string" then
    return v
  elseif tv == "number" then
    return tostring(v)
  else
    return "Bad value:  " .. treeConcat(v)
  end
end

-- MODULE EXPORT LIST

return {
  FALSEVAL    = FALSEVAL,
  TRUEVAL     = TRUEVAL,
  isFalse     = isFalse,
  isTrue      = isTrue,
  boolAsVal   = boolAsVal,
  hasOpCode   = hasOpCode,
  valToString = valToString
}
