summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--automaton/init.lua129
-rwxr-xr-xmachine-repl.lua82
-rwxr-xr-xmachine.lua16
3 files changed, 227 insertions, 0 deletions
diff --git a/automaton/init.lua b/automaton/init.lua
new file mode 100644
index 0000000..8c4bc40
--- /dev/null
+++ b/automaton/init.lua
@@ -0,0 +1,129 @@
+local actions = {}
+
+
+function actions:clone ()
+ local new = {}
+ for i, v in ipairs(self) do
+ new[i] = v
+ end
+ return setmetatable(new, getmetatable(self))
+end
+
+
+function actions:tostring ()
+ return
+ table.concat(self, nil, 1, 9) .."\n "..
+ table.concat(self, nil, 10, 16) .."\n"..
+ table.concat(self, nil, 17, 25)
+end
+
+
+function actions:pivot ()
+ local new = self:clone()
+ new[10] = self[3]
+ new[3] = self[12]
+ new[12] = self[19]
+ new[19] = self[10]
+ return new
+end
+
+
+function actions:up ()
+ local new = self:clone()
+ for i=1, 9 do
+ new[16 + i] = self[i]
+ end
+ for i=2, 8 do
+ new[i] = self[8 + i]
+ new[8 + i] = self[16 + i]
+ end
+ new[1] = self[17]
+ new[9] = self[25]
+ return new
+end
+
+
+function actions:right ()
+ local new = self:clone()
+ new[1] = self[9]
+ for i=2, 9 do
+ new[i] = self[i - 1]
+ end
+ new[10] = self[16]
+ for i=11, 16 do
+ new[i] = self[i - 1]
+ end
+ new[17] = self[25]
+ for i=18, 25 do
+ new[i] = self[i - 1]
+ end
+ return new
+end
+
+
+function actions:down ()
+ local new = self:clone()
+ for i=1, 9 do
+ new[i] = self[26 - i]
+ new[26 - i] = self[i]
+ end
+ for i=1, 3 do
+ new[9 + i] = self[17 - i]
+ new[17 - i] = self[9 + i]
+ end
+ new[13] = self[13]
+ return new
+end
+
+
+function actions:left ()
+ local new = self:clone()
+ for i=1, 8 do
+ new[i] = self[i + 1]
+ end
+ new[9] = self[1]
+ for i=10, 15 do
+ new[i] = self[i + 1]
+ end
+ new[16] = self[10]
+ for i=17, 24 do
+ new[i] = self[i + 1]
+ end
+ new[25] = self[17]
+ return new
+end
+
+
+local map = {
+ [0] = actions.pivot,
+ [1] = actions.up,
+ [2] = actions.right,
+ [3] = actions.down,
+ [4] = actions.left,
+}
+
+
+function actions:apply (eye)
+ return map[eye](self)
+end
+
+
+local mt = {
+ __index = actions,
+ __tostring = actions.tostring,
+}
+
+
+local function new (data)
+ if type(data) == "string" then
+ local obj = {}
+ for i=1, 25 do
+ obj[i] = data:sub(i, i)
+ end
+ data = obj
+ end
+ return setmetatable(data, mt)
+end
+
+
+return new
diff --git a/machine-repl.lua b/machine-repl.lua
new file mode 100755
index 0000000..6193095
--- /dev/null
+++ b/machine-repl.lua
@@ -0,0 +1,82 @@
+#!/usr/bin/env -Slua -i
+local cip = require "automaton"
+local x = "abcdefghijklm opqrstuvxyz"
+local state = cip(x)
+local history = {}
+local format = require "format"
+
+
+local function log (value)
+ table.insert(history, {previous=state, value=value})
+end
+
+
+function back ()
+ local entry = table.remove(history)
+ if not entry then
+ io.stderr:write("already at the earliest revision\n")
+ return
+ end
+ state = entry.previous
+ return state
+end
+
+
+function init (data)
+ if data then
+ x = data
+ end
+ state = cip(x)
+ history = {}
+ return state
+end
+
+
+function show ()
+ print(state)
+end
+
+
+function c ()
+ log(0)
+ state = state:pivot()
+ return state
+end
+
+
+function up ()
+ log(1)
+ state = state:up()
+ return state
+end
+
+
+function down ()
+ log(3)
+ state = state:down()
+ return state
+end
+
+
+function left ()
+ log(4)
+ state = state:left()
+ return state
+end
+
+
+function right ()
+ log(2)
+ state = state:right()
+ return state
+end
+
+
+function flush ()
+ local values = {}
+ for _, v in ipairs(history) do
+ table.insert(values, v)
+ end
+ io.write("{", format.csv(values), "}\n")
+ history = {}
+end
diff --git a/machine.lua b/machine.lua
new file mode 100755
index 0000000..44f7146
--- /dev/null
+++ b/machine.lua
@@ -0,0 +1,16 @@
+#!/usr/bin/env lua
+local eyes = require "eyes"
+local reading = require "reading"
+for _, message in ipairs(eyes) do
+ local cip = require"automaton""abcdefghijklm opqrstuvxyz"
+ local str = ""
+ for index, a, b, c in reading.trigrams(message) do
+ cip = cip:apply(a):apply(b):apply(c)
+ local slot = 3
+ if (index - 1) % 3 == 1 then
+ slot = 19
+ end
+ str = str .. cip[slot]
+ end
+ print(str)
+end