|
Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
-- miniforth2.lua
-- This is a rewrite of (find-angg "miniforth/miniforth1.lua")
-- using empty captures to make the code cleaner.
-- Author and version: Edrx, 2005nov29
-- License: GPL
subj = ""
pos = 0
getword = function ()
local _, _, word, newpos = string.find(subj, "^[ \t]*([^ \t\n\r]+)()", pos)
if _ then pos = newpos; return word end
end
getnewline = function ()
local _, _, newpos = string.find(subj, "^[ \t]*\r?\n()", pos)
if _ then pos = newpos; return "\n" end
end
geteof = function ()
local _, _, newpos = string.find(subj, "^[ \t]*()$", pos)
if _ then pos = newpos; return "" end
end
getword1 = function () return getword() or getnewline() or geteof() end
eval = function (luacode) return assert(loadstring(luacode))() end
getuntillua = function ()
local _, _, luacode, newpos = string.find(subj, "^(.-)lua>()", pos)
if _ then pos = newpos; return luacode end
end
forths = {}
forths["\n"] = function () end
forths[""] = function () interpret = nil end
forths["<lua"] = function () eval(assert(getuntillua())) end
getrestofline = function ()
local _, _, li, newpos = string.find(subj, "^([^\n]*)\n?()", pos)
pos = newpos; return li
end
forths["--"] = getrestofline
interpret = function () word = getword1(); (forths[word] or unknown)() end
run = function () while interpret do interpret() end end
-- Usage: subj = "..."; pos = 0; run()
tounix = function (str) return (string.gsub(str, "\r", "")) end
runstr = function (str, interpreter)
local s, p, i = subj, pos, interpret
subj, pos, interpret = str, 0, interpreter or interpret
run()
subj, pos, interpret = s, p, i
end
runfile = function (fname) runstr(tounix(readfile(fname))) end
demo = [[
<lua
forths["hello"] = function () print("Hello!") end
lua>
hello hello
]]
-- PP(arg)
-- (find-sh "lua -l miniforth2.lua -e runstr(demo)")
-- (find-sh "lua -e print(\\\"foo\\\")")