local function split(inputstr, sep)
if sep == nil then
sep = "%s"
end
local t = {}
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
table.insert(t, str)
end
return t
end
local function strtobool(str)
if str == "true" or str == "false" then
if str == "true" then
return true
end
if str == "false" then
return false
end
else
error(str .. " is not a boolean")
end
end
function strTabletoBoolTable(t)
local newTable = {}
for i, v in pairs(t) do
newTable[i] = strtobool(v)
end
return newTable
end
local function loopDictionaryInOrder(dict, func)
local data = {} -- array to hold the indexes
for key, value in pairs(dict) do -- loops though the dictionary provided in the first parameter
table.insert(data, key) -- this adds each key to the 'data' array
end
table.sort(data, function(a, b) -- this sorts the data array by each key's byte (numeric representations)
local toNumA = tonumber(a)
local toNumB = tonumber(b)
if toNumA and toNumB then
-- this checks for 2 integer values and sorts them accordingly
return toNumA < toNumB
end
return tostring(a):lower():byte() < tostring(b):lower():byte()
end)
for _, key in ipairs(data) do
-- loops through the 'data' array and calls the function provided in the second parameter
-- the function gives the key as the first argument and the key's value as the second argument
func(key, dict[key])
end
end
local chips = {}
local chipInfo = {}
chips["and"] = function(inputs)
if inputs[1] and inputs[2] then
return {true}
end
return {false}
end
chipInfo["and"] = {inputNum = 2}
chips["not"] = function(inputs)
if inputs[1] ~= true then
return {true}
end
return {false}
end
chipInfo["not"] = {inputNum = 1}
function newChip(chipsContained, wiring, outputs, name, info)
chips[name] = function(chipinputs)
local someTable = {}
loopDictionaryInOrder(wiring, function(output, input)
local thing = nil
if tonumber(output) then output = tonumber(output) end
print(output)
if type(output) == "number" then
thing = chipinputs[output]
else
local t = split(output, "_")
thing = chipsContained[t[1]][tonumber(t[2])]
end
local function doInput(inp)
if type(inp) == "table" then
for i, v in pairs(inp) do
doInput(v)
end
else
local t = split(inp, "_")
local a = chipsContained[t[1]]
if t[2] then
if type(thing) == "table" then
thing = table.unpack(thing)
end
if someTable[t[1]] then
someTable[t[1]][#someTable[t[1]]+1] = thing
else
someTable[t[1]] = {thing}
end
end
local ae = string.gsub(t[1], "%d+", "")
local inffoo = chipInfo[ae]
print(thing)
if someTable[t[1]] and #someTable[t[1]] == inffoo["inputNum"] then
chipsContained[t[1]] = a(someTable[t[1]])
end
end
end
end)
local newTable = {}
for i, chip in ipairs(outputs) do
local t = split(chip, "_")
if t[2] then
newTable[i] = chipsContained[t[1]][t[2]]
else
newTable[i] = table.unpack(chipsContained[t[1]])
end
end
return newTable
end
chipInfo[name] = info
end
newChip({["and1"] = chips["and"], ["not1"] = chips["not"]}, {["1"] = "and1_1", ["2"] = "and1_2", ["and1_1"] = "not1_1"}, {"not1"}, "Nand", {inputNum = 2})
newChip({["not1"] = chips["not"], ["not2"] = chips["not"], ["Nand1"] = chips["Nand"]}, {["1"] = "not1_1", ["2"] = "not2_1", ["not1_1"] = "Nand1_1", ["not2_1"] = "Nand1_2"}, {"Nand1"}, "or", {inputNum = 2})
newChip({["or1"] = chips["or"], ["not1"] = chips["not"]}, {["1"] = "or1_1", ["2"] = "or1_2", ["or1_1"] = "not1_1"}, {"not1"}, "Nor", {inputNum = 2})
newChip({["Nand1"] = chips["Nand"], ["or1"] = chips["or"], ["and1"] = chips["and"]}, {["1"] = {"or1_1", "Nand1_2"}, ["2"] = {"or1_2", "Nand1_1"}, ["or1"] = "and1_1", ["Nand1"] = "and1_2"}, {"and1"}, "xor", {inputNum = 2})
local things = io.read()
local args = split(things, " ")
local chipType = args[1]
table.remove(args, 1)
if chips[chipType] then
local e = chips[chipType](strTabletoBoolTable(args))
local str = ""
for i, v in pairs(e) do
local add = ""
if i ~= #e then
add = ", "
end
str = str .. add .. tostring(v)
end
print(str)
--print(table.concat(args, ", "))
else
error(chipType .. " is not a valid chip")
end
To embed this project on your website, copy the following code and paste it into your website's HTML: