From 28f25c593790d47514a1c3e03bf32151d2859b98 Mon Sep 17 00:00:00 2001 From: Christian Lincoln Date: Thu, 19 Feb 2026 16:32:58 +0000 Subject: [PATCH] Final --- comp.lua | 47 +++++++++++++++++++++ core.lua | 46 +++++++++++++++++++++ main.lua | 63 ++++++++++++---------------- main.silly | 36 ++++++++++++++++ main.silly.funny | 104 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 258 insertions(+), 38 deletions(-) create mode 100644 comp.lua create mode 100644 core.lua create mode 100644 main.silly create mode 100644 main.silly.funny diff --git a/comp.lua b/comp.lua new file mode 100644 index 0000000..1818fd4 --- /dev/null +++ b/comp.lua @@ -0,0 +1,47 @@ +local input,output = ... +if not input then error("No input provided") end +output = output or input..".funny" +local file = io.open(input) +if not file then error("Failed to read: "..input) end +local content = file:read("*a") +local definitions = {} +local def_pattern = "%$(%a-)={(.-)}" +for identifier,block in content:gmatch(def_pattern) do + definitions[identifier] = block +end +content = content:gsub(def_pattern,"") +content = content:gsub("/*.-*/","") +content = content:gsub("//.-[\n\r]","") +content = content:gsub("//.-[\n\r]","") +content = content:gsub("%$(%a+)",function(identifier) + return definitions[identifier] +end) +content = content:gsub("%$(%a+)",function(identifier) + return definitions[identifier] +end) +local lines = {} +local index = 0 +content=content:gsub("repeat(%d-){(.-)}",function(num,block) + return string.rep(block,num) +end) +content=content:gsub("[^\n\r]+",function(line) + index = index + 1 + local result,line2 = line:match("^(.+)%s?:(%a+)") + if line2 then lines[line2] = index end + return result or line +end) +local subs = {} +local index = 0 +content=content:gsub("[^\n\r]+",function(line) + index = index + 1 + return line:gsub("%%(%a+)",function(name) + return tostring(lines[name] - index) + end) +end) +content = content:gsub("^[\n\r]+","") +content = content:gsub("[\n\r%s]*(\n)[\n\r%s]*","\n") +local result = io.open(output,"w") +result:write(content) +print(content) +result:close() +file:close() diff --git a/core.lua b/core.lua new file mode 100644 index 0000000..06bd18b --- /dev/null +++ b/core.lua @@ -0,0 +1,46 @@ +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 diff --git a/main.lua b/main.lua index 6ac1721..e55cf85 100644 --- a/main.lua +++ b/main.lua @@ -1,41 +1,28 @@ -local registers = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} -local r = registers -for _,path in pairs({...}) do +-- An example for testing + +local runner = require("core") + +local file = io.open(({...})[1]) +if not file then error("Could not open: "..path) end +local content = file:read("*a") +for x = 32768,1,-1 do + print(x) + for y = 32768,1,-1 do + registers[1] = x + registers[2] = y + local reg,str = runner(content,false) + local dx = math.floor(x/y) + local rx = x%y + local dy = reg[3] + local ry = reg[4] + if not (dx==dy and rx==ry) then + end + print(x,y,dx,rx,dy,ry) + end +end +--[[for _,path in pairs({...}) do local file = io.open(path) if not file then error("Could not open: "..path) end local content = file:read("*a") - local index = 1 - local instructions = { - ["add (%d+) (%d+) (%d+)"] = function(d,a,b) r[d + 1] = r[a + 1] + r[b + 1] end, - ["sub (%d+) (%d+) (%d+)"] = function(d,a,b) r[d + 1] = r[a + 1] - r[b + 1] end, - ["mul (%d+) (%d+) (%d+)"] = function(d,a,b) r[d + 1] = r[a + 1] * r[b + 1] end, - ["shl (%d+) (%d+)"] = function(d,a,b) r[d + 1] = r[d + 1] << r[a + 1] end, - ["shr (%d+) (%d+)"] = function(d,a,b) r[d + 1] = r[d + 1] >> r[a + 1] end, - ["bor (%d+) (%d+)"] = function(d,a,b) r[d + 1] = r[a + 1] | r[b + 1] end, - ["band (%d+) (%d+)"] = function(d,a,b) r[d + 1] = r[a + 1] & r[b + 1] end, - ["li (%d+) (%d+)"] = function(d,i) r[d + 1] = i end, - ["jz (%d+) (%d+)"] = function(d,a,i) if r[a + 1] == 0 then index = index + r[i + 1] - 1 end end, - ["jp (%d+) (%d+)"] = function(d,a,i) if r[a + 1] > 0 then index = index + r[i + 1] - 1 end end, - ["TELL (%d+)"] = function(r) print(" ",registers[r]) end, -- DEBUGGING, NOT IN FUNNYCORE!!! - ["PUT (.+)"] = function(r) end, - ["HALT"] = function(r) index = -1 end - } - local lines = {} - for line in content:gmatch("[^\n\r]+") do table.insert(lines,line) end - local function execute_instruction(line) - 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 - callback(table.unpack(result)) - end - end - end - while lines[index] do - print(lines[index],string.format("R{%s}",table.concat(registers,","))) - execute_instruction(lines[index]) - index = index + 1 - end -end + runner(content) +end]] diff --git a/main.silly b/main.silly new file mode 100644 index 0000000..dadfe44 --- /dev/null +++ b/main.silly @@ -0,0 +1,36 @@ +// how much to shift divisor by +$shiftrange={15} +/* the difference between dividend and +multiplied */ +$difference={14} +// the thing we multiplied ^ by +$multiplied={13} +// what we're using to subtract right now +$attacker={12} +$one={11} +$zero={10} +$dividend={0} +$divisor={1} +$quotient={2} +$remainder={3} + +// Load some stuff +li $shiftrange 16 +li $multiplied 1 +li $zero 0 +mul $quotient $quotient $zero +mul $remainder $remainder $zero +add $one $multiplied $zero +// shift multiplied +shl $multiplied $shiftrange + +// main loop +repeat16{ + shr $multiplied $one + mul $attacker $multiplied $divisor + sub $difference $attacker $dividend + jp $difference 3 + add $quotient $quotient $multiplied + sub $dividend $dividend $attacker +} +add $remainder $remainder $dividend :final diff --git a/main.silly.funny b/main.silly.funny new file mode 100644 index 0000000..4df7b1c --- /dev/null +++ b/main.silly.funny @@ -0,0 +1,104 @@ +li 15 16 +li 13 1 +li 10 0 +mul 2 2 10 +mul 3 3 10 +add 11 13 10 +shl 13 15 +shr 13 11 +mul 12 13 1 +sub 14 12 0 +jp 14 3 +add 2 2 13 +sub 0 0 12 +shr 13 11 +mul 12 13 1 +sub 14 12 0 +jp 14 3 +add 2 2 13 +sub 0 0 12 +shr 13 11 +mul 12 13 1 +sub 14 12 0 +jp 14 3 +add 2 2 13 +sub 0 0 12 +shr 13 11 +mul 12 13 1 +sub 14 12 0 +jp 14 3 +add 2 2 13 +sub 0 0 12 +shr 13 11 +mul 12 13 1 +sub 14 12 0 +jp 14 3 +add 2 2 13 +sub 0 0 12 +shr 13 11 +mul 12 13 1 +sub 14 12 0 +jp 14 3 +add 2 2 13 +sub 0 0 12 +shr 13 11 +mul 12 13 1 +sub 14 12 0 +jp 14 3 +add 2 2 13 +sub 0 0 12 +shr 13 11 +mul 12 13 1 +sub 14 12 0 +jp 14 3 +add 2 2 13 +sub 0 0 12 +shr 13 11 +mul 12 13 1 +sub 14 12 0 +jp 14 3 +add 2 2 13 +sub 0 0 12 +shr 13 11 +mul 12 13 1 +sub 14 12 0 +jp 14 3 +add 2 2 13 +sub 0 0 12 +shr 13 11 +mul 12 13 1 +sub 14 12 0 +jp 14 3 +add 2 2 13 +sub 0 0 12 +shr 13 11 +mul 12 13 1 +sub 14 12 0 +jp 14 3 +add 2 2 13 +sub 0 0 12 +shr 13 11 +mul 12 13 1 +sub 14 12 0 +jp 14 3 +add 2 2 13 +sub 0 0 12 +shr 13 11 +mul 12 13 1 +sub 14 12 0 +jp 14 3 +add 2 2 13 +sub 0 0 12 +shr 13 11 +mul 12 13 1 +sub 14 12 0 +jp 14 3 +add 2 2 13 +sub 0 0 12 +shr 13 11 +mul 12 13 1 +sub 14 12 0 +jp 14 3 +add 2 2 13 +sub 0 0 12 +add 3 3 0