Clean up Lua code.

- Refactor ALU to a single function to be reused
- Abstract registers out of a global variable into a function argument
- Use proper table initialisations
- Accept that LUA starts arrays at 1. Partially.
This commit is contained in:
2017-12-18 15:18:39 +01:00
parent 80bf90c034
commit 432fc46a2b

View File

@@ -27,7 +27,7 @@ end
registers = {} registers = {}
function argval(input) function argval(input, registers)
if tonumber(input) ~= nil then if tonumber(input) ~= nil then
return tonumber(input) return tonumber(input)
elseif registers[input] ~= nil then elseif registers[input] ~= nil then
@@ -37,80 +37,62 @@ function argval(input)
end end
end end
function alu(instr, registers)
-- Perform instruction and return offset to iptr
if instr[1] == "set" then
registers[instr[2]] = argval(instr[3], registers)
elseif instr[1] == "snd" then
last_played = argval(instr[2], registers)
elseif instr[1] == "add" then
registers[instr[2]] = argval(instr[2], registers) + argval(instr[3], registers)
elseif instr[1] == "mul" then
registers[instr[2]] = argval(instr[2], registers) * argval(instr[3], registers)
elseif instr[1] == "mod" then
registers[instr[2]] = argval(instr[2], registers) % argval(instr[3], registers)
elseif instr[1] == "jgz" then
if argval(instr[2], registers) > 0 then
return argval(instr[3], registers)
end
else
print("Invalid instruction", instr[1])
return nil
end
return 1
end
iptr = 0 iptr = 0
while true do while true do
instr = instructions[iptr] instr = instructions[iptr]
if instr[1] == "set" then if instr[1] == "snd" then
registers[instr[2]] = argval(instr[3]) last_played = argval(instr[2], registers)
elseif instr[1] == "snd" then iptr = iptr + 1
last_played = argval(instr[2])
elseif instr[1] == "add" then
registers[instr[2]] = argval(instr[2]) + argval(instr[3])
elseif instr[1] == "mul" then
registers[instr[2]] = argval(instr[2]) * argval(instr[3])
elseif instr[1] == "mod" then
registers[instr[2]] = argval(instr[2]) % argval(instr[3])
elseif instr[1] == "rcv" then elseif instr[1] == "rcv" then
print("Last played", last_played) print("Last played", last_played)
iptr = iptr + 1
break break
elseif instr[1] == "jgz" then
if argval(instr[2]) > 0 then
iptr = iptr + argval(instr[3]) - 1 -- correct for the subsequent add
end
else else
print("Illegal instruction", instr[1]) iptr = iptr + alu(instr, registers)
break
end
iptr = iptr + 1
end
registers = {}
registers[0] = {}
registers[1] = {}
function argval(input, pid)
if tonumber(input) ~= nil then
return tonumber(input)
elseif registers[pid][input] ~= nil then
return registers[pid][input]
else
return pid
end end
end end
-- Double instruction pointer registers = {{p=0}, {p=1}}
iptr = {}
iptr[0] = 0
iptr[1] = 0
-- Double receive buffer
rbuf = {}
rbuf[0] = {}
rbuf[1] = {}
-- Pointer in the receive buffer
rptr = {}
rptr[0] = 0
rptr[1] = 0
-- Number of entries in each buffer
rctr = {}
rctr[0] = 0
rctr[1] = 0
-- Whether one thread is waiting
waiting = {}
waiting[0] = false
waiting[1] = false
sends = 0 iptr = {0, 0} -- instruction pointers
rbuf = {{}, {}} -- receive buffers
rptr = {0, 0} -- Pointer in the receive buffer
rctr = {0, 0} -- Number of entries in each buffer
waiting = {false, false} -- Whether one thread is waiting
pid = 0 pid = 1
function nextpid(pid) function nextpid(pid)
if pid == 0 then if pid == 1 then
return 1 return 2
else else
return 0 return 1
end end
end end
@@ -122,39 +104,27 @@ while true do
break break
end end
if instr[1] == "set" then if instr[1] == "snd" then
registers[pid][instr[2]] = argval(instr[3], pid) npid = nextpid(pid)
elseif instr[1] == "snd" then rbuf[npid][rctr[npid]] = argval(instr[2], registers[pid])
rbuf[nextpid(pid)][rctr[nextpid(pid)]] = argval(instr[2], pid) rctr[npid] = rctr[npid] + 1
rctr[nextpid(pid)] = rctr[nextpid(pid)] + 1 waiting[npid] = false
elseif instr[1] == "add" then
registers[pid][instr[2]] = argval(instr[2], pid) + argval(instr[3], pid) iptr[pid] = iptr[pid] + 1
elseif instr[1] == "mul" then
registers[pid][instr[2]] = argval(instr[2], pid) * argval(instr[3], pid)
elseif instr[1] == "mod" then
registers[pid][instr[2]] = argval(instr[2], pid) % argval(instr[3], pid)
elseif instr[1] == "rcv" then elseif instr[1] == "rcv" then
if rbuf[pid][rptr[pid]] ~= nil then if rbuf[pid][rptr[pid]] ~= nil then
-- Have value, read and continue -- Have value, read and continue
registers[pid][instr[2]] = rbuf[pid][rptr[pid]] registers[pid][instr[2]] = rbuf[pid][rptr[pid]]
rptr[pid] = rptr[pid] + 1 rptr[pid] = rptr[pid] + 1
waiting[nextpid(pid)] = false iptr[pid] = iptr[pid] + 1
else else
-- Need to wait for value, continue with execution of the other -- Need to wait for value, continue with execution of the other
waiting[pid] = true waiting[pid] = true
pid = nextpid(pid) pid = nextpid(pid)
iptr[pid] = iptr[pid] - 1 -- Correct for subsequent increase
end
elseif instr[1] == "jgz" then
if argval(instr[2], pid) > 0 then
iptr[pid] = iptr[pid] + argval(instr[3], pid) - 1 -- correct for the subsequent add
end end
else else
print("Illegal instruction", instr[1]) iptr[pid] = iptr[pid] + alu(instr, registers[pid])
break
end end
iptr[pid] = iptr[pid] + 1
end end
print("Final sends from 1:", rctr[1])
print("Final sends from 1:", rctr[0])