#! lua
-- eval
--[[
Put lua code in lines starting > then evaluate !op on next word
in the others.
--]]
(getmetatable "").__call = string.format
local x, y = { }, { }
for line in io.lines (arg[1], "*L") do
   if line:match "^>%s" then
      x[1 + #x] = line:sub(3)
   else
      y[1 + #y] = line
   end -- if
end -- for
local code = load (table.concat (x))
assert (pcall (code))
local var = "!([%w_]+)%s(%S+)"
local undef = "\n[%s undefined]\n"
local bad = "\n[%s not a function]\n"
local eval = \ (op, word)
      local _, v = pcall (load ("=> " .. op))
      if not v then => undef (op) end -- if
      if type (v) == "function" then
         => v (word)
      else
        => v and bad (op)
      end -- if
      end -- function
for _, line in ipairs (y) do
  io.write ((line:gsub (var, eval)))
end -- for
