[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: l-ook!
- From: Klaus Ripke <paul-lua@...>
- Date: Wed, 10 Aug 2005 13:13:15 +0200
sorry,
now please find attached an Ook! to lua translator.
bf2lua is left as an exercise to the reader.
have fun
#!/usr/local/bin/lua
-- (c) 2oo5 ook@malete.org
-- released under http://www.lua.org/license.html#5
-- some Ook! scripts assume cells to contain unsigned bytes,
-- patch the function p(d) accordingly.
-- some Ook! scripts need an io.flush() in w()
-- and, yes, it IS that slow!
local function ook2lua (ook) -- highly optimizing ook compiler
local lua = {}
local incr = { ['.?'] = 'm', ['?.'] = 'm', ['..'] = 'p', ['!!'] = 'p', }
local cmd = {
['.!'] = 'r()', ['!.'] = 'w()',
['!?'] = 'while ook() do', ['?!'] = 'end',
['??'] = '--[[??]]'
}
local indent, cnt, last = '', 0
local function flush ()
if last then table.insert(lua, indent..last..'('..cnt..')') end
cnt, last = 0
end
for o,k in string.gfind(ook, 'Ook([.!?]).-Ook([.!?])') do
local code = o..k
local collect = incr[code]
if collect ~= last then flush() end
if collect then
last = collect
if '.' == o then cnt=cnt+1 else cnt=cnt-1 end
else
local pre, cmd = indent, cmd[code]
if '!?' == code then
indent = indent..'\t'
elseif '?!' == code then
indent = string.sub(indent, 2)
pre = indent
end
table.insert(lua, pre..cmd)
end
end
flush()
return [[
local a,i = {}, 0
local function m (d) i = i + d end
local function p (d) a[i] = d + (a[i] or 0) end
local function r () a[i] = string.byte(io.read(1) or '\0') end
local function w () io.write(string.char(a[i] or 0)) end
local function ook () return a[i] and 0 ~= a[i] end
]] .. table.concat(lua, '\n') .. '\n'
end
lua = ook2lua(io.open(arg[1]):read('*a'))
io.stderr:write(lua)
assert(loadstring(lua))()