commit d4d0fba8808df33a4944fc982551fe42f242d9b2 Author: Christian Lincoln Date: Wed Feb 18 23:24:11 2026 +0000 Initial diff --git a/README.md b/README.md new file mode 100644 index 0000000..1c76fbd --- /dev/null +++ b/README.md @@ -0,0 +1,8 @@ +# This is my FunnyCore interpreter + +The aim of this project was just to create a little VM for the FunnyCore, featured in Imperial's Computing Year 1 Undergraduate Computer Architecture Coursework (that's a mouthful). +I noticed so many people using weird wonderful and whacky combinations of Kotlin, C and Python, so I'm showing my 'native' language a little bit of love. +Sometimes it's not about code quality and maintainability but being completely transparent, functional, reliable and terse. +This is an example of that methodology, in just about an hour and 50 lines of code I can jump the competition in my cohort. + +I would have proferred to use NEL to translate into FunnyCore's instruction set so I can have labels etc. Unfortunately I hadn't finished the standard library by then. diff --git a/a.out b/a.out new file mode 100755 index 0000000..c22e652 Binary files /dev/null and b/a.out differ diff --git a/main.c b/main.c new file mode 100644 index 0000000..34e5faf --- /dev/null +++ b/main.c @@ -0,0 +1,7 @@ + #include + + int main(int argc, const char** argv) { + printf("your mom: %i\n",2); + + } + \ No newline at end of file diff --git a/main.funny b/main.funny new file mode 100644 index 0000000..6fe1e88 --- /dev/null +++ b/main.funny @@ -0,0 +1,3 @@ +TELL 1 +TELL 2 +TELL 3 diff --git a/main.lua b/main.lua new file mode 100644 index 0000000..6ac1721 --- /dev/null +++ b/main.lua @@ -0,0 +1,41 @@ +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 + 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 diff --git a/mainc.lua b/mainc.lua new file mode 100644 index 0000000..585fa3e --- /dev/null +++ b/mainc.lua @@ -0,0 +1,65 @@ +local result = "" + +local function compile_and_run(source) + source = string.format([[ + #include + #include + + 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")) diff --git a/start.funny b/start.funny new file mode 100644 index 0000000..9b733d1 --- /dev/null +++ b/start.funny @@ -0,0 +1,5 @@ +PUT welcome to the funnycore +li 0 10 +li 1 20 +PUT i have updated registers +HALT