So I was working on a binary based command line, and I wanted to add a jump command, but I couldn’t figure out how to do it. I am currently using continue to do it but it’s just not working. It continues the loop, but it doesn’t increase the index, meaning it’s not actually continuing.
Module script, inside of ReplicatedStorage:
local reader = { }
local commands = {
["jmp"] = "10101010",
["out"] = "00000111",
["end"] = "10000001"
}
export type tape = { string }
function base10(int: string)
local bin = int
bin = string.reverse(bin)
local sum = 0
for i = 1, string.len(bin) do
local num = string.sub(bin, i, i) == "1" and 1 or 0
sum += num * math.pow(2, i - 1)
end
return tostring(sum)
end
function reader.read(tape: tape)
for ind, int in pairs(tape) do
print(int, ind)
if int == commands["jmp"] then
local newPos = base10(tape[ind + 1])
script.JumpPosition.Value = newPos
repeat task.wait(1) print(ind) continue until ind == script.JumpPosition.Value
end
print(int, ind)
end
end
return reader
The value of the JumpPosition IntValue stays the same, and it keeps printing out 1, and not 2.
Any help is appreciated!
What exactly is this meant to be doing? continue
being the final thing in a loop actually does nothing at all. Continue is used as a way to skip over the rest of whatever needs to happen during a single iteration, which in your case is nothing.
Using repeat inside of your for-loop
here will result in an infinite loop, unless the JumpPosition
value is the same as your current for-loop
index value. If you need to jump back wouldn’t using a while-loop
work better for you?
I’m envisioning something like this, bear in mind this is pseudo-code.
function reader.read(tape: tape)
local index = 1
while tape[index] do
local command = tape[index]
if command == commands["jmp"] then
index = base10(tape[index + 1]) -- I'm assuming this is the jump position.
else
index += 1
end
task.wait(1) -- Just to avoid any script timeouts in the event of an infinite jump loop.
end
end
2 Likes
It does jump to the position that I tell it to jump to, but it doesn’t continue from there. Or another issue that I don’t understand. If I input:
Reader.read({ "10101010", "00000011", "00000111" })
It will jump to the third value, but it wont read the value.
Updated read function:
function reader.read(tape: tape)
local index = 1
while tape[index] do
local command = tape[index]
if command == commands["jmp"] then
index = base10(tape[index + 1]) -- I'm assuming this is the jump position.
script.JumpPosition.Value = index
else
index += 1
end
if command == commands["out"] then
print(#characters) --// This should print "26" since there are 26 characters in the characters string (so far atleast)
end
task.wait(1) -- Just to avoid any script timeouts in the event of an infinite jump loop.
print(tape[index], command) --// This prints "nil 10101010", not what I actually need it to print.
end
end
If I make the input table only have “00000111”, then it will print out 26.
After a while of just piecing together random stuff, I finally got something that works. I can print stuff to the output, and I can jump to a position in the tape (table of binary ints).
Module script, inside of ReplicatedStorage:
local reader = { }
local commands = {
["jmp"] = "00100001",
["out"] = "01000001",
["end"] = "10000001"
}
local characters = "abcdefghijklmnopqrstuvwxyz "
export type tape = { string }
function base10(int: string)
local bin = int
bin = string.reverse(bin)
local sum = 0
for i = 1, string.len(bin) do
local num = string.sub(bin, i, i) == "1" and 1 or 0
sum += num * math.pow(2, i - 1)
end
return tostring(sum)
end
local function find(tab: { any }, value: any): number
for i, v in pairs(tab) do
if v == value then
return i
end
end
return 0
end
--function reader.read0(tape: tape)
-- local index = 1
-- while tape[index] do
-- local command = tape[index]
-- if command == commands["jmp"] then
-- index = base10(tape[index + 1]) -- I'm assuming this is the jump position.
-- script.JumpPosition.Value = index
-- else
-- index += 1
-- end
-- if command == commands["out"] then
-- print(#characters)
-- end
-- task.wait(1) -- Just to avoid any script timeouts in the event of an infinite jump loop.
-- print(tape[index], command)
-- end
--end
function reader.read(tape: tape)
local ind = 1
local jumpPos = script.JumpPosition
while tape[ind] do
local int = string.format("%08d", tape[ind])
if int == commands["jmp"] then
ind = base10(tape[ind + 1]) -- I'm assuming this is the jump position.
jumpPos.Value = ind
end
if int == commands["out"] then
local text = ""
for i = ind + 1, find(tape, commands["end"]) do
local character = characters:sub(base10(tape[i]), base10(tape[i]))
text ..= character
end
print(text)
end
ind += 1
task.wait(1)
print(ind, int, jumpPos.Value)
end
end
return reader
Updated script:
function read(data: string)
local newData = { }
newData = data:split("_")
Reader.read(newData)
end
read("00100001_10_01000001_1000_101_1100_1100_1111_11011_10111_1111_10010_1100_100_10000001_00100001_1") --// This prints "hello world" on loop
1 Like