|
Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
-- miniforth3.lua - 2007mar25, Edrx
-- For Lua-5.1 (because of string.match)
-- This is part of blogme3
-- http://anggtwu.net/miniforth/miniforth3.lua.html
-- http://anggtwu.net/miniforth/miniforth3.abs.txt.html
-- pat has 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
_F = {}
_F["%L"] = function () assert(loadstring(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])
-- subj = readfile(arg[1])
-- pos = 1
-- outerloop()