From 2bf2a6a88958f404171d20030feb98642aae2950 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Thu, 12 Aug 2010 17:07:57 +0200 Subject: iterating over strings via lpeg --- life.lua | 47 +++++---- mplife.cld | 318 ------------------------------------------------------------- mplife.lua | 262 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 285 insertions(+), 342 deletions(-) delete mode 100644 mplife.cld create mode 100644 mplife.lua diff --git a/life.lua b/life.lua index 8d03a20..a760804 100644 --- a/life.lua +++ b/life.lua @@ -18,12 +18,9 @@ gol = {} gol.helpers = {} -gol.setup = gol.setup or {} -gol.setup.current = gol.setup.current or {} -gol.setup.fade = gol.setup.fade or false +local C, Cs, Ct, P, R, S, match = lpeg.C, lpeg.Cs, lpeg.Ct, lpeg.P, lpeg.R, lpeg.S, lpeg.match -local C, Ct, P, R, S, match = lpeg.C, lpeg.Ct, lpeg.P, lpeg.R, lpeg.S, lpeg.match -- http://lua-users.org/lists/lua-l/2009-06/msg00343.html gol.helpers.utfchar = R("\000\127") + @@ -167,17 +164,21 @@ end --- Computing single lines and whole frames and intervals thereof function gol.next_line (rows, rule) - local new = "" local live = "1" - local fade = gol.setup.current.fade or gol.setup.fade + local fade = false + if mplife then + fade = mplife.setup.current.fade + end local n = 1 local max = string.len(rows[2]) - repeat - local env = {} + local cell = R("09") + P("D") + local nocell = 1-cell + local ce = Cs(cell) / function (current) + local env = {} local lpos, rpos -- positions left of / right of current -- toroidal, flips over at borders @@ -193,8 +194,6 @@ function gol.next_line (rows, rule) rpos = n + 1 end - local current = string.sub( rows[2], n, n ) - -- +---+---+---+ -- |nw | n | ne| -- +---+---+---+ @@ -222,13 +221,16 @@ function gol.next_line (rows, rule) end end + n = n + 1 + -- -- adding new cell according to ruleset - new = new .. gol.apply_rule(current, cnt, rule, fade, true) + return gol.apply_rule(current, cnt, rule, fade, true) + end - n = n + 1 - until n > max + local noce = Cs(nocell) / "" + local c = Cs(ce * (noce + ce)^0) - return new + return c:match(rows[2]) end function gol.next_frame (old, rule) @@ -245,10 +247,6 @@ function gol.next_frame (old, rule) rows[1] = old[n-1] or old[#old] -- last rows[3] = old[n+1] or old[1] -- next - -- dead borders - --rows[1] = old[n-1] or gol.helpers.dead_row(string.len(b)) -- last - --rows[3] = old[n+1] or gol.helpers.dead_row(string.len(b)) -- next - new[n] = gol.next_line( rows, rule ) n = n + 1 @@ -321,12 +319,14 @@ function gol.pre_frame (frame) ["9"] = "9", } + local cell = R"09" + P"D" + local nocell = 1-cell + local ce = Cs(cell) / repl + local noce = Cs(nocell) / "" + local c = Cs(ce * (noce + ce)^0) + for j, row in ipairs(frame) do - local out = row - for chr, subst in pairs(repl) do - out = out:gsub(chr, subst) - end - io.write("\n"..out) + io.write("\n" .. c:match(row)) end end @@ -341,6 +341,5 @@ function gol.pre_movie (frames, section) io.write("\n") end - return gol diff --git a/mplife.cld b/mplife.cld deleted file mode 100644 index eac9bbb..0000000 --- a/mplife.cld +++ /dev/null @@ -1,318 +0,0 @@ --- --------------------------------------------------------------------------------- --- FILE: mplife.cld --- USAGE: ./mplife.cld --- DESCRIPTION: Metapost output for Conway's Game of Life --- OPTIONS: --- --- REQUIREMENTS: --- --- BUGS: --- --- NOTES: --- --- AUTHOR: Philipp Gesang (Phg), --- COMPANY: --- VERSION: 1.0 --- CREATED: 06/08/10 12:28:35 CEST --- REVISION: --- --------------------------------------------------------------------------------- --- - -require "run" - -local mplife = {} - -gol.setup = gol.setup or {} -gol.setup.current = gol.setup.current or {} - -do - local c = gol.setup.current - - --c.file = c.file or "examples/10x10_glider.gol" - c.file = c.file or "examples/gliders.gol" - --c.file = c.file or "examples/ggun.gol" - c.rule = gol.parse_rule("B3/S23") -- default Conway - c.init = gol.parse_file(c.file) - c.last = c.init -- for successive mode - - c.pensize = .1 - c.color = { r = .6, g = .6, b = .8 } - c.fade = false - c.firstframe = 1 - c.frames = 5 - c.preamble = [[ -\setupcolors[state=start] -\setupbackgrounds[state=start] -\setuppapersize[S6][S6] - -\setuppagenumbering[state=stop,location=] -]] - --c.before_frame = [[ ---\defineoverlay - --[back] - --[\bgroup ---]] - --c.after_frame = [[ ---\egroup] - ---\setupbackgrounds [paper] [background=back] ---\page ---]] - c.before_frame = [[ -\startuseMPgraphic{back} -pickup pencircle xyscaled (.25*]] .. c.pensize .. [[pt, ]] .. c.pensize .. [[pt) rotated 45; -]] - c.after_frame = [[ -currentpicture := currentpicture xysized (\the\paperwidth, \the\paperheight); -\stopuseMPgraphic -\defineoverlay[back][\useMPgraphic{back}] - -\setupbackgrounds [page] [background=back] -]] - - gol.setup.current = c -end - - - -mplife.fade = true - -mplife.format = { - - nl = "\n", space = " ", - - opar = "(", cpar = ")", - - hyphen = "-", comma = ",", - scolon = ";", path = "--", - - filldraw = "fill", -- "draw", - draw = "draw", - cycle = "cycle", - - p = function (x,y) - return string.format("(%s,%s)", x, y) - end, -} - -function mplife.runcode (mpcode) - --context.startMPcode() - context(mpcode) - --context.stopMPcode() - --context.MPdrawing(mpcode) -- comes with next beta! -end - -function mplife.p_square (from, filled, fade_level) - local f = mplife.format - - -- p1 p2 - -- from from + d.x - -- +-------+ - -- | | - -- | | - -- | | - -- | | - -- +-------+ - -- from - d.y from + d.x && - d.y - -- p4 p3 - - local d = { x = 1, y = 1, unit = "" } - local points = { - [1] = { x = from.x, y = from.y }, - [2] = { x = from.x+d.x, y = from.y }, - [3] = { x = from.x+d.x, y = from.y-d.y }, - [4] = { x = from.x, y = from.y-d.y }, - } - - - local buildsqr = function (unit) - local draw - if filled then - draw = f.filldraw - else - draw = f.draw - end - - local tmp = f.nl .. draw - tmp = tmp .. f.p( points[1].x .. unit, points[1].y .. unit ) .. f.path -- automatic type conversion to string - tmp = tmp .. f.p( points[2].x .. unit, points[2].y .. unit ) .. f.path - tmp = tmp .. f.p( points[3].x .. unit, points[3].y .. unit ) .. f.path - tmp = tmp .. f.p( points[4].x .. unit, points[4].y .. unit ) .. f.path - tmp = tmp .. f.cycle - - if filled and fade_level then - --tmp = tmp .. f.space .. "withcolor" .. f.space .. 1/fade_level .. "black" - if fade_level == 0 then - fade_level = 1 - elseif fade_level == 1 then - fade_level = 0 - else - fade_level = .05 * fade_level + .5 - end - tmp = tmp .. f.space .. "scaled 200 " .. "withcolor" .. f.space .. fade_level .. "white" - end - - tmp = tmp .. f.scolon - return tmp - end - - return buildsqr (from.unit) -end - -function mplife.draw_grid(grid) - local h = gol.helpers - local pat = "" - if type(grid) == "string" then - grid = h.split(grid, "\n") -- may leave an empty string as last item - if grid[#grid] == "" then grid[#grid] = nil end - end - - local max = {} - max.x, max.y = grid[1]:len(), #grid - - local n = 0 - local pos -- kept in broader scope for picture border estimation - while grid[#grid-n] ~= nil do - - local row = grid[#grid-n] - pos = 1 - - repeat - local opacity - local cell = row:sub(pos, pos) - local p = { x = pos-1, y = n, unit = "" } - - local fill, fade_level - fill = true - - if fill and mplife.fade then - fade_level = tonumber(cell) - end - - if not fade_level then -- no number --> cell once dead (marked "D") - fade_level = 9.3 - end - - if fade_level ~= 0 then -- don't draw dead cells - if fade_level == 1 then - opacity = .33 - pat = pat .. string.format("%s unitsquare shifted (%s,%s) withcolor transparent(1,%s,\\MPcolor{lifesquare});\n", - "draw", p.x, p.y, opacity*2) - else - opacity = (1/3) - (fade_level / 30) - end - pat = pat .. string.format("%s unitsquare scaled .9 shifted (%s+.05,%s+.05) withcolor transparent(1,%s,\\MPcolor{lifesquare});\n", - "filldraw", p.x, p.y, opacity) - end - pos = pos + 1 - until pos > row:len() - pat = pat .. "\n" - n = n + 1 - end - - pat = pat .. string.format([[ -path border; -border = (0,0)--(%s,0)--(%s,%s)--(0,%s)--cycle; -fill border withcolor transparent (1,.75, white) ; -setbounds currentpicture to boundingbox border ; -]], max.x, max.x, max.y, max.y) - ---fill border withcolor transparent (1,.3,1white) ; - --print("Pic\n", pat) - mplife.runcode(pat) - return true -end - ---- testing section - -function mplife.checker(n) - local squares = "" - local cnt = 0 - for y=1, n, 1 do - for x=1, n, 1 do - local fill - if cnt % 2 == 0 then fill = true end - local p = { x = x, y = y, unit = "" } - squares = squares .. mplife.p_square( p, fill ) - cnt = cnt + 1 - end - if n % 2 == 0 then cnt = cnt + 1 end -- needed for even column numbers - end - --print (squares) - return squares -end - -function mplife.rand_pattern(n, digits) - local pat = "" - for y=1, n, 1 do - for x=1, n, 1 do - pat = pat .. math.random(0,digits) - end - pat = pat .. "\n" - end - return pat -end - ---print(mplife.rand_pattern(10,1)) - ---- interfacing with life.lua - -function mplife.life_frame (frame) - --local c = gol.setup.current - - mplife.draw_grid(frame) - - return true -end - -function mplife.life_movie (frames, next_page) - local c = gol.setup.current - for i, frame in ipairs(frames) do - context(c.before_frame) - mplife.life_frame(frame) - context(c.after_frame) - end -end - -function mplife.life_frames () - local c = gol.setup.current - - - local frames = gol.frames( c.init, c.rule, c.frames, c.firstframe ) - mplife.life_movie( frames, false ) - return true -end - -function mplife.successive () - local c = gol.setup.current - context(c.before_frame) - mplife.draw_grid(c.last) - context(c.after_frame) - gol.setup.current.last = gol.next_frame( c.last, c.rule ) - return true -end - - - - -function mplife.main () - local c = gol.setup.current - - context.definecolor({ "lifesquare" }, c.color) - context(c.preamble) -- only once - context.starttext() - - --mplife.runcode (mplife.checker(20)) - - --mplife.draw_grid(mplife.rand_pattern(10,6)) - - --mplife.life_frames() - - for i=1,10,1 do - mplife.successive() - --context("\\input knuth") - context.page() - end - context.stoptext() - return 0 -end - - -return mplife.main() diff --git a/mplife.lua b/mplife.lua new file mode 100644 index 0000000..e430827 --- /dev/null +++ b/mplife.lua @@ -0,0 +1,262 @@ +-- +-------------------------------------------------------------------------------- +-- FILE: mplife.cld +-- USAGE: ./mplife.cld +-- DESCRIPTION: Metapost output for Conway's Game of Life +-- OPTIONS: --- +-- REQUIREMENTS: --- +-- BUGS: --- +-- NOTES: --- +-- AUTHOR: Philipp Gesang (Phg), +-- COMPANY: +-- VERSION: 1.0 +-- CREATED: 06/08/10 12:28:35 CEST +-- REVISION: --- +-------------------------------------------------------------------------------- +-- + +require "run" + +local C, Cs, Ct, P, R, S, match = lpeg.C, lpeg.Cs, lpeg.Ct, lpeg.P, lpeg.R, lpeg.S, lpeg.match + +mplife = {} + +mplife.setup = mplife.setup or {} +mplife.setup.current = mplife.setup.current or {} + +do + local c = mplife.setup.current + + --c.file = c.file or "examples/10x10_glider.gol" + --c.file = c.file or "examples/gliders.gol" + c.file = c.file or "examples/ggun.gol" + c.rule = gol.parse_rule("B3/S23") -- default Conway + c.init = gol.parse_file(c.file) + c.last = c.init -- for successive mode + + c.pensize = .1 + c.color = { r = .6, g = .6, b = .8 } + c.opacity = 1/3 + c.fade = true + c.gestalt = "unitsquare" -- try "unitcircle" + + c.firstframe = 1 + c.frames = 5 + + c.preamble = [[ +\setupcolors[state=start] +\setupbackgrounds[state=start] +\setuppapersize[S6][S6] + +\setuppagenumbering[state=stop,location=] + +\defineoverlay[back][\ctxlua{mplife.successive()}] + +\setupbackgrounds [page] [background=back] +]] + c.before_frame = [[ +\startMPcode +pickup pencircle xyscaled (.25*]] .. c.pensize .. [[pt, ]] .. c.pensize .. [[pt) rotated 45; +]] + c.after_frame = [[ +currentpicture := currentpicture xysized (\the\paperwidth, \the\paperheight); +\stopMPcode +]] + + mplife.setup.current = c +end + +mplife.format = { + + nl = "\n", space = " ", + + opar = "(", cpar = ")", + + hyphen = "-", comma = ",", + scolon = ";", path = "--", + + filldraw = "fill", -- "draw", + draw = "draw", + cycle = "cycle", + + p = function (x,y) + return string.format("(%s,%s)", x, y) + end, +} + +function mplife.runcode (mpcode) + --context.startMPcode() + context(mpcode) + --context.stopMPcode() + --context.MPdrawing(mpcode) -- comes with next beta! +end + +function mplife.draw_grid(grid, settings) + local h = gol.helpers + local c = mplife.setup.current + local pat = "" + if type(grid) == "string" then + grid = h.split(grid, "\n") -- may leave an empty string as last item + if grid[#grid] == "" then grid[#grid] = nil end + end + + local max = {} + max.x, max.y = grid[1]:len(), #grid + + local n = 0 + while grid[#grid-n] ~= nil do + + local row = grid[#grid-n] + local pos = 1 + + local cell = Cs(R("09") + P("D")) / function (char) + --repeat + local opacity = c.opacity + local p = { x = pos-1, y = n} + + local fill, fade_level = true + --fill = true + + if fill and c.fade then + fade_level = tonumber(char) + end + + if not fade_level then -- no number --> cell once dead (marked "D") + fade_level = 9.3 + end + + if fade_level ~= 0 then -- skip dead cells + if fade_level == 1 then + pat = pat .. string.format("%s %s shifted (%s,%s) withcolor transparent(1,%s,%s);\n", + "draw", c.gestalt, p.x, p.y, opacity*2, settings.color) + else + opacity = opacity - (fade_level * opacity * .1) + end + pat = pat .. string.format("%s %s scaled .9 shifted (%s+.05,%s+.05) withcolor transparent(1,%s,%s);\n", + "filldraw", c.gestalt, p.x, p.y, opacity, settings.color) + end + pos = pos + 1 + end + + local line = cell * ((1-cell) + cell)^0 + line:match(row) + pat = pat .. "\n" + n = n + 1 + end + + pat = pat .. string.format([[ +path border; +border = (0,0)--(%s,0)--(%s,%s)--(0,%s)--cycle; +setbounds currentpicture to boundingbox border ; +]], max.x, max.x, max.y, max.y) + +--fill border withcolor transparent (1,.75, white) ; +--fill border withcolor transparent (1,.3,1white) ; + --print("Pic\n", pat) + mplife.runcode(pat) + return true +end + +--- testing section + +function mplife.checker(n) + local squares = "" + local cnt = 0 + for y=1, n, 1 do + for x=1, n, 1 do + local fill + if cnt % 2 == 0 then fill = true end + local p = { x = x, y = y } + squares = squares .. mplife.p_square( p, fill ) + cnt = cnt + 1 + end + if n % 2 == 0 then cnt = cnt + 1 end -- needed for even column numbers + end + --print (squares) + return squares +end + +function mplife.rand_pattern(n, digits) + local pat = "" + for y=1, n, 1 do + for x=1, n, 1 do + pat = pat .. math.random(0,digits) + end + pat = pat .. "\n" + end + return pat +end + +--- interfacing with life.lua + +function mplife.life_frame (frame) + --local c = mplife.setup.current + + mplife.draw_grid(frame) + + return true +end + +function mplife.life_movie (frames, next_page) + local c = mplife.setup.current + for i, frame in ipairs(frames) do + context(c.before_frame) + mplife.life_frame(frame) + context(c.after_frame) + end +end + +function mplife.life_frames () + local c = mplife.setup.current + + + local frames = gol.frames( c.init, c.rule, c.frames, c.firstframe ) + mplife.life_movie( frames, false ) + return true +end + +function mplife.successive () + local c = mplife.setup.current + local settings = {} + if mplife.slides then + settings.color = "\\MPcolor{simpleslides:contrastcolor}" + else + context.definecolor({ "automaton:color" }, c.color) + settings.color = "\\MPcolor{automaton:color}" + end + context(c.before_frame) + mplife.draw_grid(c.last, settings) + context(c.after_frame) + mplife.setup.current.last = gol.next_frame( c.last, c.rule ) + return true +end + + +function mplife.main () + local c = mplife.setup.current + + if context then + context.definecolor({ "lifesquare" }, c.color) + end + --mplife.successive() + --context(c.preamble) -- only once + --context.starttext() + + --mplife.runcode (mplife.checker(20)) + + --mplife.draw_grid(mplife.rand_pattern(10,6)) + + --mplife.life_frames() + + --for i=1,10,1 do + --context("\\input knuth") + --context.page() + --end + --context.stoptext() + return 0 +end + + +if not mplife.slides then -- standalone + return mplife.main() +end -- cgit v1.2.3