This commit is contained in:
Christian Lincoln 2026-02-18 23:24:11 +00:00
commit d4d0fba880
7 changed files with 129 additions and 0 deletions

65
mainc.lua Normal file
View file

@ -0,0 +1,65 @@
local result = ""
local function compile_and_run(source)
source = string.format([[
#include <stdio.h>
#include <stdint.h>
int main(int argc, const char** argv) {
%s
}
]],source)
print(source.."\n")
local file = io.open("main.c","w")
file:write(source)
file:close()
os.execute("gcc main.c")
os.execute("./a.out")
end
local instructions = {
["add (%d+) (%d+) (%d+)"] = function(l,d,a,b)
end,
["sub (%d+) (%d+) (%d+)"] = function(l,d,a,b)
end,
["mul (%d+) (%d+) (%d+)"] = function(l,d,a,b)
end,
["shl (%d+) (%d+)"] = function(l,d,a,b)
end,
["shr (%d+) (%d+)"] = function(l,d,a,b)
end,
["bor (%d+) (%d+) (%d+)"] = function(l,d,a,b)
end,
["band (%d+) (%d+) (%d+)"] = function(l,d,a,b)
end,
["li (%d+) (%d+)"] = function(l,d,a,b)
end,
["li (%d+) (%d+)"] = function(l,d,a,b)
end,
["li (%d+) (%d+)"] = function(l,d,a,b)
end
}
local source = {string.format("int32_t registers = {%s}",string.rep("0",0,","))}
local input = io.open("main.funny")
local index = 1
for line in input:read("a"):gmatch("[^\n\r]") do
table.insert(string.format("instruction%i:",index)) -- add the goto label for the line
instruction = instruction + 1
local target
for pattern,callback in pairs(instructions) do
line:match(pattern)
end
end
compile_and_run(table.concat(source,"\n"))