Initial
This commit is contained in:
commit
d4d0fba880
7 changed files with 129 additions and 0 deletions
8
README.md
Normal file
8
README.md
Normal file
|
|
@ -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.
|
||||||
BIN
a.out
Executable file
BIN
a.out
Executable file
Binary file not shown.
7
main.c
Normal file
7
main.c
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(int argc, const char** argv) {
|
||||||
|
printf("your mom: %i\n",2);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
3
main.funny
Normal file
3
main.funny
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
TELL 1
|
||||||
|
TELL 2
|
||||||
|
TELL 3
|
||||||
41
main.lua
Normal file
41
main.lua
Normal file
|
|
@ -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
|
||||||
65
mainc.lua
Normal file
65
mainc.lua
Normal 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"))
|
||||||
5
start.funny
Normal file
5
start.funny
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
PUT welcome to the funnycore
|
||||||
|
li 0 10
|
||||||
|
li 1 20
|
||||||
|
PUT i have updated registers
|
||||||
|
HALT
|
||||||
Loading…
Add table
Add a link
Reference in a new issue