|
Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
-- An implementation of "textual" colon words (more like macros) for
-- miniforth. This is truly minimal, and will work on a system that
-- doesn't even dream of having bytecodes.
-- (find-miniforthfile "cube1.mflua")
dstack = {}
rstack = {program}
dpush = function( val ) tinsert(dstack, 1, val) end
dpop = function( ) return tremove(dstack, 1) end
rpush = function( prog ) tinsert(rstack, 1, prog); program = prog end
rpop = function( ) tremove(rstack, 1); program = rstack[1] end
dict[""] = function( )
getline()
if program.pos == strlen(program.string) then
return program.EOP, rpop()
end
end
mfpush = function( code ) rpush({string=code, pos=0}) end
mf = function( code ) -- run code now, then return
rpush({string=code, pos=0, EOP="break"})
mainloop()
end
mainloop = function( ) while dict[getword()]() ~= "break" do end end
re(res, ";;", "[ \t\n];;([ \t\n]|$)")
dict["::"] = function( )
local word, code = getword(), getuntilre(";;")
dict[word] = function( ) mfpush(%code) end
end
-- A cousin of "::" that treats its body as Lua code. It doesn't have
-- anything to do with the textual colon words, but it's definition is
-- very similar to "::".
dict["::lua"] = function()
local word, code = getword(), getuntilre(";;")
dict[word] = dostring(format("return function() %s\nend", code))
end