|
Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
-- miniforth3.lua - 2007jun04, Edrx
-- For Lua-5.1 (because of string.match)
-- This includes the blogme3 core, that will eventually be moved out
-- http://angg.twu.net/miniforth/miniforth3.lua.html
-- http://angg.twu.net/miniforth/miniforth3.abs.txt.html
eval = function (str) return assert(loadstring(str))() end
-- pat is a pattern with one "real" capture
-- and then an empty capture
parsebypattern = function (pat)
local capture, newpos = string.match(subj, pat, pos)
if newpos then pos = newpos; return capture end
end
parsespaces = function () return parsebypattern("^([ \t]*)()") end
parseword = function () return parsebypattern("^([^ \t\n]+)()") end
parsenewline = function () return parsebypattern("^(\n)()") end
parserestofline = function () return parsebypattern("^([^\n]*)()") end
getword = function () parsespaces(); return parseword() end
getwordornewline = function ()
parsespaces()
return parseword() or parsenewline()
end
state = "interpret"
states = {}
states.interpret = function ()
word = getwordornewline()
if word == nil then state = "stop"; return end
semantics = _F[word]
if type(semantics) == "function" then semantics(); return end
end
* (eepitch-lua51)
A = {}
= A[nil]
while state ~= "stop" do printstate(); states[state]() end
_F = {}
_F["%L"] = function () eval(parserestofline()) end
_F["\n"] = function () end
outer = function ()
word = getwordornewline()
if not word then return nil end -- EOF -> word=nil -> break
if _F[word] then _F[word](); return true end
error(string.format("Unrecognized word: %q", word))
end
outerloop = function () if outer() then outerloop() end end
readfile = function (fname)
local f = assert(io.open(fname, "r"))
local fcontents = f:read("*a"); f:close(); return fcontents
end
setsubjto = function (str) subj = str; pos = 1 end
runstring = function (str) setsubjto(str); outerloop() end
runfile = function (fname) runstring(readfile(fname)) end
-- runfile(arg[1])
-- (find-blogmefile "")
-- (find-blogmefile "blogme2-middle.lua")
-- (find-luamanualw3m "#pdf-string.match")
-- ee = function () runfile(ee_expand("$EEVTMPDIR/ee.mf3")) end
--]=]