I am working on a Lua 5.1 VM inside of Lua 5.1. Most Opcodes work fine but my TFORLOOP does not work. I get this error: Bad Argument #1 to '?' (table expected, got function)
Here is my implementation of TFORLOOP:
[33] = function(INSTRUCTION) --TFORLOOP
local offset = INSTRUCTION.a + 3
local result = {stack[INSTRUCTION.a](stack[INSTRUCTION.a], stack[INSTRUCTION.a + 1], stack[INSTRUCTION.a + 2])}
for i = 1, INSTRUCTION.c do
stack[offset + i - 1] = result[i]
end
if result[1] ~= nil then
stack[INSTRUCTION.a + 1] = result[1]
else
if result[2] ~= nil then
stack[INSTRUCTION.a + 1] = result[2]
else
pc = pc + 1
end
end
end,
And I parse these instructions:
{a=0,op=5,Bx=0,}
{a=1,op=10,c=0,b=10,}
{a=2,op=1,Bx=1,}
{a=3,op=1,Bx=2,}
{a=4,op=1,Bx=3,}
{a=5,op=1,Bx=4,}
{a=6,op=1,Bx=5,}
{a=7,op=1,Bx=6,}
{a=8,op=1,Bx=7,}
{a=9,op=1,Bx=8,}
{a=10,op=1,Bx=9,}
{a=11,op=1,Bx=10,}
{a=1,op=34,c=1,b=10,}
{a=0,op=28,c=4,b=2,}
{a=0,op=22,sBx=3,}
{a=5,op=5,Bx=11,}
{a=6,op=0,c=0,b=4,}
{a=5,op=28,c=1,b=2,}
{a=0,op=33,c=2,b=0,}
{a=0,op=22,sBx=-5,}
{a=0,op=30,c=0,b=1,}
Constants:
pairs
print
10
9
8
7
6
5
4
3
2
1
Thanks!
To help me understand why my TFORLOOP OPCODE is not working and maybe fix it
TFORLOOP
must calculate R(A)(R(A+1), R(A+2))
You are calculating R(A)(R(A), R(A+1), R(A+2))
instead