Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
-- The outer (text) interpreter - minimal version -- (find-angg "LFORTH/README" "outer-mini") p = {} -- p.text = arg[1] p.pos = 0 p.parseluare = function (errfunction, luare) local arr = pack(string.find(p.text, luare, p.pos+1)) if arr[1] == nil then return errfunction(luare) end local startre = table.remove(arr, 1) local endre = table.remove(arr, 1) if DBG then P(p.pos, luare, startre-p.pos-1, endre-startre+1, unpack(arr)) end return startre-p.pos-1, endre-startre+1, unpack(arr) end getword = function () local _, nspaces = p.parseluare(nil, "^[ \t]*") p.pos = p.pos + nspaces local __, dpos, word = p.parseluare(nil, "^([^ \t\n]*)") if dpos == 0 then _, dpos, word = p.parseluare(nil, "^(\n?)") end p.pos = p.pos + dpos return word end getuntilluare = function (errfunction, luare) local arr = pack(p.parseluare(errfunction, luare)) local _, len = table.remove(arr, 1), table.remove(arr, 1) p.pos = p.pos+_+len return unpack(arr) end dict = {} dict[""] = os.exit -- EOF dict["\n"] = function () end -- just skip the newline dict["[lua"] = function () assert(loadstring(getuntilluare(nil, "^(.-)lua%]")))() end interpret = function () local word = getword(); local d = dict[word] if d then d() else unknown(word) end end outerloop = function (text) if text then p.text = text end while 1 do interpret() end end