registers = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} local r = registers return function(content,deb) local index = 1 local cycles = 0 local instructions = { ["add (%-?%d+) (%-?%d+) (%-?%d+)"] = function(d,a,b) r[d + 1] = r[a + 1] + r[b + 1]; cycles = cycles + 1 end, ["sub (%-?%d+) (%-?%d+) (%-?%d+)"] = function(d,a,b) r[d + 1] = r[a + 1] - r[b + 1]; cycles = cycles + 1 end, ["mul (%-?%d+) (%-?%d+) (%-?%d+)"] = function(d,a,b) r[d + 1] = r[a + 1] * r[b + 1]; cycles = cycles + 1 end, ["shl (%-?%d+) (%-?%d+)"] = function(d,a) r[d + 1] = r[d + 1] << r[a + 1]; cycles = cycles + 1 end, ["shr (%-?%d+) (%-?%d+)"] = function(d,a) r[d + 1] = r[d + 1] >> r[a + 1]; cycles = cycles + 1 end, ["bor (%-?%d+) (%-?%d+)"] = function(d,a,b) r[d + 1] = r[a + 1] | r[b + 1]; cycles = cycles + 1 end, ["band (%-?%d+) (%-?%d+)"] = function(d,a,b) r[d + 1] = r[a + 1] & r[b + 1]; cycles = cycles + 1 end, ["li (%-?%d+) (%-?%d+)"] = function(d,i) r[d + 1] = i; cycles = cycles + 3 end, ["jz (%-?%d+) (%-?%d+)"] = function(a,i) if r[a + 1] == 0 then index = index + i - 1 end; cycles = cycles + 3 end, ["jp (%-?%d+) (%-?%d+)"] = function(a,i) if r[a + 1] > 0 then index = index + i - 1 end; cycles = cycles + 3 end, ["TELL (%-?%d+)"] = function(r) print(" ",registers[r + 1]); end, -- DEBUGGING, NOT IN FUNNYCORE!!! ["COMMENT (.+)"] = function() return true; end, ["HALT"] = function(r) index = -1 end } local lines = {} for line in content:gmatch("[^\n\r]+") do local thing = function() for pattern,callback in pairs(instructions) do local result = {} for index,item in pairs({line:match(pattern)}) do table.insert(result,tonumber(item)) end if #result ~= 0 then local a,b,c = table.unpack(result) return function() callback(a,b,c) end end end end table.insert(lines,thing()) end while lines[index] do if not lines[index]() then if deb then print(lines[index],string.format("R{%s}:%i",table.concat(registers,","),cycles)) end end index = index + 1 end return registers,string.format("R{%s}:%i",table.concat(registers,","),cycles) end