git @ Cat's Eye Technologies Beturing / master src / beturing.lua
master

Tree @master (Download .tar.gz)

beturing.lua @masterraw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#!/usr/local/bin/lua
--
-- beturing.lua v1.1
-- Interpreter for Beturing v1.1
-- A Befunge-flavoured Turing machine
-- Implemented in Lua 5 by Chris Pressey, June 2005
--
-- This work is in the public domain.  See the file UNLICENSE for more
-- information.
--

--
-- v1.0: June 6 2005: initial release
-- v1.1: June 8 2005: changed semantics of '*' special code
--

--[[ Common functions ]]--

local debug_log = print
local usage = function()
    io.stderr:write("Usage: [lua] beturing.lua [-oq] [-d x1,y1:x2,y2] [filename.bet]\n")
    os.exit(1)
end
local old_semantics = false
local display = nil

--[[ Object Classes ]]--

--[[-----------]]--
--[[ Playfield ]]--
--[[-----------]]--

--
-- Store an unbounded grid.
--
Playfield = {}
Playfield.new = function(tab)
    tab = tab or {}
    local nw, ne, sw, se = {}, {}, {}, {}  -- quadrant storage
    local min_x, min_y, max_x, max_y       -- limits seen so far
    local method = {}

    --
    -- Private function: pick the appropriate quadrant & translate
    --
    local pick_quadrant = function(x, y)
        if x >  0 and y >  0 then return se, x, y end
        if x >  0 and y <= 0 then return ne, x, 1-y end
        if x <= 0 and y >  0 then return sw, 1-x, y end
        if x <= 0 and y <= 0 then return nw, 1-x, 1-y end
    end

    --
    -- Read the symbol at a given position in the playfield
    --
    method.peek = function(pf, x, y)
        local contents, nx, ny = pick_quadrant(x, y)
        contents[ny] = contents[ny] or {} -- make sure row exists
        local sym = contents[ny][nx] or " "
	return sym
    end

    --
    -- Write a symbol at a given position in the playfield
    --
    method.poke = function(pf, x, y, sym)
        local contents, nx, ny = pick_quadrant(x, y)
        contents[ny] = contents[ny] or {} -- make sure row exists
        contents[ny][nx] = sym
	if not min_x or x < min_x then min_x = x end
	if not max_x or x > max_x then max_x = x end
	if not min_y or y < min_y then min_y = y end
	if not max_y or y > max_y then max_y = y end
    end

    --
    -- Store a string starting at (x, y).
    --
    method.poke_str = function(pf, x, y, str)
	local i
	for i = 1, string.len(str) do
	    pf:poke(x + (i - 1), y, string.sub(str, i, i))
	end
    end

    --
    -- Load the playfield from a file.
    --
    method.load = function(pf, filename, callback)
        local file = io.open(filename)
	local line = file:read("*l")
	local x, y = 0, 0

        while line do
	    if string.find(line, "^%s*%#") then
	        -- comment or directive - not included in playfield.
		local found, len, nx, ny =
		  string.find(line, "^%s*%#%s*%@%(%s*(%-?%d+)%s*%,%s*(%-?%d+)%s*%)")
		if found then
		    x = tonumber(nx)
		    y = tonumber(ny)
		    debug_log("Now loading at " ..
		      "(" .. tostring(x) .. "," .. tostring(y) .. ")")
		else
		    callback(line)
		end
	    else
	        pf:poke_str(x, y, line)
		y = y + 1
	    end
            line = file:read("*l")
	end
	file:close()
    end

    --
    -- Return a string representing the playfield.
    --
    method.render = function(pf, start_x, start_y, end_x, end_y)
	start_x = start_x or min_x
        start_y = start_y or min_y
	end_x = end_x or max_x
	end_y = end_y or max_y
        local y = start_y
	local s = "--- (" .. tostring(start_x) .. "," .. tostring(start_y) .. ")-"
	s = s .. "(" .. tostring(end_x) .. "," .. tostring(end_y) .. ") ---\n"
        while y <= end_y do
	    local x = start_x
	    while x <= end_x do
	        s = s .. pf:peek(x, y)
	        x = x + 1
	    end
	    s = s .. "\n"
            y = y + 1
	end

	return s
    end

    return method
end

--[[------]]--
--[[ Head ]]--
--[[------]]--

--
-- Represent a readable(/writeable) location within a playfield.
--
Head = {}
Head.new = function(tab)
    tab = tab or {}

    local pf = assert(tab.playfield)
    local x = tab.x or 0
    local y = tab.y or 0
    local moves_left = 0
    local moves_right = 0

    local method = {}

    method.report = function(hd)
        io.stdout:write("Moves left:  " .. tostring(moves_left) .. ", moves right: " .. tostring(moves_right) .. "\n")
	io.stdout:write("Total moves: " .. tostring(moves_left + moves_right) .. "\n")
    end

    method.read = function(hd, sym)
        return pf:peek(x, y)
    end

    method.write = function(hd, sym)
        pf:poke(x, y, sym)
    end

    --
    -- look for this symbol         -> 13 <- on match, write this symbol
    -- on match, move head this way -> 24 <- choose next state on this
    --
    method.read_code = function(hd)
        local seek_sym, repl_sym, move_cmd, state_cmd

	debug_log("rd cd")
	seek_sym = hd:read()
	hd:move(">")
	repl_sym = hd:read()
	hd:move("v")
	state_cmd = hd:read()
	hd:move("<")
	move_cmd = hd:read()
	hd:move("^")
	debug_log("cd rd")
	
	return seek_sym, repl_sym, move_cmd, state_cmd
    end

    method.move = function(hd, sym)
        if sym == "^" then
            y = y - 1
        elseif sym == "v" then
            y = y + 1
        elseif sym == "<" then
            x = x - 1
	    moves_left = moves_left + 1
        elseif sym == ">" then
            x = x + 1
	    moves_right = moves_right + 1
        elseif sym ~= "." then
            error("Illegal movement symbol '" .. sym .. "'")
        end
    end

    return method
end

--[[---------]]--
--[[ Machine ]]--
--[[---------]]--

--
-- Perform the mechanics of the machine.
--
Machine = {}
Machine.new = function(tab)
    tab = tab or {}

    local pf = tab.playfield or Playfield.new()
    local data_head = Head.new{
        playfield = pf,
        x = tab.data_head_x or 0,
	y = tab.data_head_y or 0
    }
    local code_head = Head.new{
        playfield = pf,
        x = tab.code_head_x or 0,
	y = tab.code_head_y or 0
    }

    local method = {}

    --
    -- Private function: provide interpretation of the state-
    -- transition operator.
    --
    local interpret = function(sym, sense)
        if sense then
	    -- Positive interpretation.
	    -- Backwards compatibility:
	    if old_semantics then
	        if sym == "/" then
		    return ">"
		else
		    return sym
		end
	    end
	    if sym == "/" or sym == "`" then
		return ">"
	    elseif sym == "\\" or sym == "'" or sym == "-" then
		return "<"
	    elseif sym == "|" then
		return "^"
	    else
	        return sym
	    end
	else
	    -- Negative interpretation.
	    -- Backwards compatibility:
	    if old_semantics then
	        if sym == "/" then
		    return "v"
		else
		    return sym
		end
	    end
	    if sym == "/" or sym == "\\" or sym == "|" then
		return "v"
	    elseif sym == "-" then
		return ">"
	    elseif sym == "`" or sym == "'" then
		return "^"
	    else
	        return state_cmd
	    end
	end
    end

    --
    -- Advance the machine's configuration one step.
    --
    method.step = function(m)
        local this_sym = data_head:read()
        local seek_sym, repl_sym, move_cmd, state_cmd = code_head:read_code()
	local code_move

	debug_log("Symbol under data head is '" .. this_sym .. "'")
	debug_log("Instruction under code head is:")
	debug_log("(" .. seek_sym .. repl_sym .. ")")
	debug_log("(" .. move_cmd .. state_cmd .. ")")

	--
	-- Main processing logic
	--
	if move_cmd == "*" then
	    --
	    -- Special - match anything, do no rewriting,
	    -- move the data head using the replacement symbol
	    -- (unless using the old compatibility semantics,)
	    -- and advance the state using the positive interpretation.
	    --
	    debug_log("-> Wildcard!")
	    if not old_semantics then
	        data_head:move(repl_sym)
	    end
	    code_move = interpret(state_cmd, true)
	elseif seek_sym == this_sym then
	    --
	    -- The seek symbol matches the symbol under the data head.
	    -- Rewrite it, move the head, and advance the state
	    -- using the positive interpretation.
	    --
	    debug_log("-> Symbol matches, replacing with '" .. repl_sym .. "'")
	    debug_log("-> moving data head '" .. move_cmd .. "'")
	    data_head:write(repl_sym)
	    data_head:move(move_cmd)
	    code_move = interpret(state_cmd, true)
	else
	    --
	    -- No match - just advance the state, using negative interp.
	    --
	    debug_log("-> No match.")
	    code_move = interpret(state_cmd, false)
	end

	--
	-- Do the actual state advancement here.
	--
	if code_move == "@" then
	    debug_log("-> Machine halted!")
	    return false
	else
            debug_log("-> moving code head '" .. code_move .. "'")
            code_head:move(code_move)
	    code_head:move(code_move)
	    return true
	end
    end

    --
    -- Run the machine 'til it halts.
    --
    method.run = function(m)
	local done = false
	while not done do
	    if display then
		io.stdout:write(pf:render(display.x1, display.y1,
		                          display.x2, display.y2))
	    else
	        debug_log(pf:render())
	    end
	    done = not m:step()
	end
	data_head:report()
    end

    return method
end

--[[ INIT ]]--

local pf = Playfield.new()

--[[ command-line arguments ]]--

local argno = 1
while arg[argno] and string.find(arg[argno], "^%-") do
    if arg[argno] == "-q" then          -- quiet
        debug_log = function() end
    elseif arg[argno] == "-o" then      -- use v1.0 semantics
        old_semantics = true
    elseif arg[argno] == "-d" then
        argno = argno + 1
	local found, len
	display = {}
        found, len, display.x1, display.y1, display.x2, display.y2 =
	    string.find(arg[argno], "(%-?%d+)%,(%-?%d+)%:(%-?%d+)%,(%-?%d+)")
	if not found then
	    usage()
	end
	display.x1 = tonumber(display.x1)
	display.y1 = tonumber(display.y1)
	display.x2 = tonumber(display.x2)
	display.y2 = tonumber(display.y2)	
    else
        usage()
    end
    argno = argno + 1
end

if not arg[argno] then
    usage()
end

--[[ load playfield ]]--

local data_head_x, data_head_y, code_head_x, code_head_y = 0, 0, 0, 0
local directive_processor = function(directive)
    local found, len, x, y

    found, len, x, y = 
      string.find(directive, "^%s*%#%s*D%(%s*(%-?%d+)%s*%,%s*(%-?%d+)%s*%)")
    if found then
        data_head_x = tonumber(x)
	data_head_y = tonumber(y)
	debug_log("Data head initially located at " ..
          "(" .. tostring(data_head_x) .. "," .. tostring(data_head_y) .. ")")
	return true
    end
    found, len, x, y = 
      string.find(directive, "^%s*%#%s*C%(%s*(%-?%d+)%s*%,%s*(%-?%d+)%s*%)")
    if found then
        code_head_x = tonumber(x)
	code_head_y = tonumber(y)
	debug_log("Code head initially located at " ..
          "(" .. tostring(code_head_x) .. "," .. tostring(code_head_y) .. ")")
	return true
    end

    return false
end

pf:load(arg[argno], directive_processor)

--[[ MAIN ]]--

local m = Machine.new{
    playfield = pf,
    data_head_x = data_head_x,
    data_head_y = data_head_y,
    code_head_x = code_head_x,
    code_head_y = code_head_y
}

m:run()