|
Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
-- This file:
-- http://angg.twu.net/LUA/Arith1.lua.html
-- http://angg.twu.net/LUA/Arith1.lua
-- (find-angg "LUA/Arith1.lua")
-- Author: Eduardo Ochs <eduardoochs@gmail.com>
--
-- (defun e () (interactive) (find-angg "LUA/Arith1.lua"))
Tokens = Class {
fromcs = function (str) return Tokens(split(str, "(.)")) end,
fromws = function (str) return Tokens(split(str)) end,
type = "Tokens",
__tostring = function (tks) return tks:tostring() end,
__index = {
torect1 = function (tks, i)
return Rect.from(tks[i].."\n"..i)
end,
torect = function (tks, i, j)
i, j = i or 1, j or #tks
local out = tks:torect1(i)
for k=i+1,j do out = out.." "..tks:torect1(k) end
return out
end,
torectp = function (tks) return tks:torect() .. " pos=" .. tks.pos end,
tostring1 = function (tks, i, j, sep)
return table.concat(tks, sep or tks.sep or "", i or 1, j or #tks)
end,
tostring = function (tks, i, j)
return tks:torect(i or 1, j or #tks):tostring()
end,
tostringp = function (tks) return tks:torectp():tostring() end,
sub = function (tks, i, j) return TokenSeq.from(tks, i, j) end,
},
}
TokenSeq = Class {
type = "TokenSeq",
from = function (tks, i, j) return TokenSeq {tks=tks, i=i, j=j} end,
__tostring = function (ts) return ts:tostring1() end,
__concat = function (tsl, tsr) return tsl:glue(tsr) end,
__index = {
tostring1 = function (ts) return ts.tks:tostring1(ts.i, ts.j) end,
tostring = function (ts) return ts.tks:tostring (ts.i, ts.j) end,
glue = function (tsl, tsr) return TokenSeq.from(tsl.tks, tsl.i, tsr.j) end,
},
}
-- (find-dn6 "stacks.lua" "Stack")
--
Stack = Class {
type = "Stack",
new = function () return Stack {} end,
--
__tostring = function (s) return mapconcat(tostring, s, " ") end,
__index = {
push = function (s, o) table.insert(s, o); return s end,
pushs = function (s, ...) for _,o in {...} do s:push(o) end; return s end,
--
check = function (s) assert(#s>0, s.msg or "Empty stack"); return s end,
drop = function (s) s:check(); s[#s]=nil; return s end,
dropn = function (s, n) for i=1,n do s:drop() end; return s end,
dropuntil = function (s, n) while #s>n do s:drop() end; return s end,
clear = function (s) return s:dropn(#s) end,
--
pop = function (s) return s[#s], s:dropn(1) end,
pop2 = function (s) return s[#s-1], s[#s], s:dropn(2) end,
pop3 = function (s) return s[#s-2], s[#s-1], s[#s], s:dropn(3) end,
pop4 = function (s) return s[#s-3], s[#s-2], s[#s-1], s[#s], s:dropn(4) end,
--
pick = function (s, offset) return s[#s-offset] end,
pock = function (s, offset, o) s[#s-offset] = o; return s end,
},
}
push = function (stack, o) return stack:push(o) end
pop = function (stack) return stack:pop() end
popuntil = function (stack, depth) return stack:dropuntil(depth) end
pick = function (stack, offset) return stack:pick(offset) end
pock = function (stack, offset, o) return stack:pock(offset, o) end
tks = Tokens.fromcs "(1+2)*3^(4+5)"
tks.pos = 1
s = Stack.new()
prt = function () print(tks:torect()) end
prt = function () print(tks:tostringp()) end
prs = function () print(s) end
pup = function () s:push(tks:sub(tks.pos, tks.pos)); tks.pos = tks.pos + 1 end
glue = function () local l,r = s:pop2(); s:push(l..r) end
-- (find-angg "LUA/lua50init.lua" "Code")
vc = Code.vc
cmds = {
["push"] = vc [[ o => pup() ]],
["_+"] = vc [[ o => glue() ]],
["_+_"] = vc [[ o => glue() ]],
["(_)"] = vc [[ o => pup(); glue(); glue() ]],
}
runcmds = function (bigstr)
for _,cmd in ipairs(split(bigstr)) do
printf("%-5s -> ", cmd)
cmds[cmd]()
local newstack = tostring(s)
print(newstack)
end
end
--[[
• (eepitch-lua51)
• (eepitch-kill)
• (eepitch-lua51)
dofile "Arith1.lua"
prt()
runcmds "push push push _+ push _+_ (_) "
runcmds "push _+ push push _+ "
runcmds "push push push _+ "
runcmds "push _+_"
runcmds "(_) _+_ _+_"
--]]
--[[
• (eepitch-lua51)
• (eepitch-kill)
• (eepitch-lua51)
dofile "Arith1.lua"
prt()
pup(); prs()
pup(); prs()
pup(); prs()
glue(); prs()
pup(); prs()
glue(); prs()
pup(); glue(); glue(); prs()
pup(); prs()
glue(); prs()
pup(); prs()
= (tks:sub(2,5) .. tks:sub(6, 9))
= (tks:sub(2,5) .. tks:sub(6, 9)):tostring()
a, b = s:pop2(); print(a..b)
= s
--]]
-- (find-angg "LUA/Rect.lua" "Rect-tests")
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "Arith1.lua"
= tks
= tks:tostring1()
= tks:tostring1(3, 5)
= tks:sub(3, 5)
= tks:sub(3, 5):tostring()
--]]
-- Local Variables:
-- coding: utf-8-unix
-- End: