So as a little side project, I wanted to make this accumulator with odd syntax, but when I change the valid operators, it doesn’t work.
Module script, in ReplicatedStorage:
local Uhoh = { }
local items = {
zero = { key = "#", value = "0" },
add = { key = "+", value = "+ 1" },
sub = { key = "-", value = "- 1" }
}
local operators = {
items.add.key,
items.sub.key
}
local function find(tab: { any }, value: any)
for i, v in pairs(tab) do
if v == value then
return true
end
end
return false
end
function Uhoh.interpret(source)
local accum = ""
local op = ""
local numb = ""
local cleanedSrc = source:gsub(items.zero.key, items.zero.value):gsub(items.add.key, items.add.value):gsub(items.sub.key, items.sub.value)
local i = 1
for c in cleanedSrc:gmatch(".") do
if i == 1 then
accum = c
elseif find(operators, c) then
op = c
elseif c == "1" then
numb = c
if op == operators[1] then
accum += numb
elseif op == operators[2] then
accum -= numb
end
end
i += 1
end
return accum
end
return Uhoh
Server script, in ServerScriptService:
local uhoh = require(game.ReplicatedStorage.UhOh)
print(uhoh.interpret("#+"))
The current set up will print out 1
in the output, but if I change the code like so:
Module script, in ReplicatedStorage:
local Uhoh = { }
local items = {
zero = { key = "#", value = "0" },
add = { key = "!", value = "+ 1" }, --// New operator is just an example
sub = { key = "-", value = "- 1" }
}
local operators = {
items.add.key,
items.sub.key
}
local function find(tab: { any }, value: any)
for i, v in pairs(tab) do
if v == value then
return true
end
end
return false
end
function Uhoh.interpret(source)
local accum = ""
local op = ""
local numb = ""
local cleanedSrc = source:gsub(items.zero.key, items.zero.value):gsub(items.add.key, items.add.value):gsub(items.sub.key, items.sub.value)
local i = 1
for c in cleanedSrc:gmatch(".") do
if i == 1 then
accum = c
elseif find(operators, c) then
op = c
elseif c == "1" then
numb = c
if op == operators[1] then
accum += numb
elseif op == operators[2] then
accum -= numb
end
end
i += 1
end
return accum
end
return Uhoh
Server script, in ServerScriptService:
local uhoh = require(game.ReplicatedStorage.UhOh)
print(uhoh.interpret("#!"))
It will print out 0
for some reason that I don’t quite understand.
Any help is appreciated!
Yes, it’s supposed to be a parody of the BrainFrick programming language. (Am I allowed to use curse words?)