You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
168 lines
3.4 KiB
168 lines
3.4 KiB
local node_plus_const = function(tape_offset, amount)
|
|
return
|
|
{
|
|
nodetype = "plus",
|
|
pure = true,
|
|
left = {valtype = "tape", offset = tape_offset},
|
|
right = {valtype = "immediate", value = amount},
|
|
out = {valtype = "tape", offset = tape_offset},
|
|
temporal_dependencies = {},
|
|
pure_dependencies = {},
|
|
}
|
|
end
|
|
|
|
local node_out = function(tape_offset)
|
|
return
|
|
{
|
|
nodetype = "out",
|
|
pure = false,
|
|
input = { valtype = "tape", offset = tape_offset },
|
|
temporal_dependencies = {},
|
|
pure_dependencies = {},
|
|
}
|
|
end
|
|
|
|
local node_in = function(tape_offset)
|
|
return
|
|
{
|
|
nodetype = "in",
|
|
pure = false,
|
|
output = { valtype = "tape", offset = tape_offset },
|
|
temporal_dependencies = {},
|
|
pure_dependencies = {},
|
|
}
|
|
end
|
|
|
|
local node_root = function()
|
|
return
|
|
{
|
|
nodetype = "root",
|
|
pure = true,
|
|
temporal_dependencies = {},
|
|
pure_dependencies = {}
|
|
}
|
|
end
|
|
|
|
local func_tab =
|
|
{
|
|
[1] = function(t)
|
|
t.head_pos = t.head_pos - 1
|
|
t.idx = t.idx + 1
|
|
end,
|
|
[2] = function(t)
|
|
t.head_pos = t.head_pos + 1
|
|
t.idx = t.idx + 1
|
|
end,
|
|
[3] = function(t)
|
|
local node = node_plus_const(tape_offset, 1)
|
|
node.temporal_dependencies = { t.current_node }
|
|
t.current_node = node
|
|
t.idx = t.idx + 1
|
|
end,
|
|
[4] = function(t)
|
|
local node = node_plus_const(tape_offset, 1)
|
|
node.temporal_dependencies = { t.current_node }
|
|
t.current_node = node
|
|
t.idx = t.idx + 1
|
|
end,
|
|
[5] = function(t)
|
|
local node = node_out(tape_offset)
|
|
node.temporal_dependencies = { t.current_node }
|
|
t.current_node = node
|
|
t.idx = t.idx + 1
|
|
end,
|
|
[6] = function(t)
|
|
local node = node_in(tape_offset)
|
|
node.temporal_dependencies = { t.current_node }
|
|
t.current_node = node
|
|
t.idx = t.idx + 1
|
|
end,
|
|
[7] = function(t)
|
|
error("unimplemented")
|
|
end,
|
|
[8] = function(t)
|
|
error("unimplemented")
|
|
end,
|
|
-- [nil] = function(t)
|
|
-- t.ret_idx = t.idx
|
|
-- end,
|
|
}
|
|
|
|
function do_parse_seq(sequence, idx)
|
|
local t =
|
|
{
|
|
sym = sequence[idx],
|
|
ret_idx = nil,
|
|
idx = idx,
|
|
head_pos = 0,
|
|
current_node = node_root(),
|
|
}
|
|
|
|
while t.ret_idx == nil do
|
|
func_tab[t.sym](t)
|
|
t.sym = sequence[t.idx]
|
|
if t.sym == nil then
|
|
t.ret_idx = t.idx
|
|
end
|
|
end
|
|
|
|
return t
|
|
end
|
|
|
|
function io_to_string(io)
|
|
if io.valtype == "immediate" then
|
|
return tostring(io.value)
|
|
elseif io.valtype == "tape" then
|
|
return "tape[" .. tostring(io.offset) .. "]"
|
|
else
|
|
return "unknown i/o"
|
|
end
|
|
end
|
|
|
|
function recursive_node_string(node)
|
|
if node.nodetype == "root" then
|
|
return { "root" }
|
|
elseif node.nodetype == "plus" then
|
|
local dep = recursive_node_string(node.temporal_dependencies[1])
|
|
table.insert(dep, "add " .. io_to_string(node.left) .. " + " .. io_to_string(node.right))
|
|
return dep
|
|
elseif node.nodetype == "out" then
|
|
local dep = recursive_node_string(node.temporal_dependencies[1])
|
|
table.insert(dep, "out " .. io_to_string(node.input))
|
|
return dep
|
|
elseif node.nodetype == "in" then
|
|
local dep = recursive_node_string(node.temporal_dependencies[1])
|
|
table.insert(dep, "in " .. tostring(node.output))
|
|
return dep
|
|
else
|
|
return { "unknown node" }
|
|
end
|
|
end
|
|
|
|
--str = "++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++."
|
|
--str = "+[-]..."
|
|
str = ".+.>.+.<.-.>.-."
|
|
--str = "+[+]"
|
|
|
|
char_table =
|
|
{
|
|
[">"] = 1,
|
|
["<"] = 2,
|
|
["+"] = 3,
|
|
["-"] = 4,
|
|
["."] = 5,
|
|
[","] = 6,
|
|
["["] = 7,
|
|
["]"] = 8,
|
|
}
|
|
|
|
prog = {}
|
|
|
|
for i = 1,#str do
|
|
prog[#prog + 1] = char_table[string.sub(str, i, i)]
|
|
end
|
|
|
|
local q = do_parse_seq(prog, 1)
|
|
|
|
print(table.concat(recursive_node_string(q.current_node), "\n"))
|
|
|