This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Sewbacca's profile picture

Goto

Started by Sewbacca, 15 March 2016 - 05:00 PM
Sewbacca #1
Posted 15 March 2016 - 06:00 PM
And NO, it isn't a suggestion,
I want to program it myself.

In Lua is a goto command, but CC removed it, I think to prevent errors, but has somebody any ideas to code a goto function?
For example:


_G["goto"] = function(fiilePath, line, startChar)
  <code>
end
Edited on 15 March 2016 - 05:02 PM
KingofGamesYami #2
Posted 15 March 2016 - 06:15 PM
Alright, so the first thing you have to understand is that goto is in Lua 5.2, CC uses 5.1. The second thing you need to understand is goto is not written in Lua.

So essentially: You would have to rewrite Lua 5.2 in Lua 5.1, which would be extremely difficult.
Sewbacca #3
Posted 15 March 2016 - 07:37 PM
Are there any not pessimistic ideas?
Dragon53535 #4
Posted 15 March 2016 - 08:18 PM
Not really, that's basically what you'd have to do since Lua is a language built using other languages (In most cases C). Since you're wanting something that usually requires the backbone language to implement, you're going to have to really just build the entire 5.2 language yourself.
Lupus590 #5
Posted 15 March 2016 - 09:26 PM
If you need a GOTO you are likely doing something very badly. See this wiki page for more information: https://en.wikipedia.org/wiki/Spaghetti_code
Bomb Bloke #6
Posted 16 March 2016 - 12:14 AM
If you need a GOTO you are likely doing something very badly. See this wiki page for more information: https://en.wikipedia.org/wiki/Spaghetti_code

There are instances in where a goto would be the best way to proceed. The fact that it's missing forces coders to write spag when those instances occur: so it was for good reason that it was added to 5.2.

Let's say you've built a cluster of nested loops that goes however many levels deep. For whatever reason you need to break out of them when a certain condition is met.

Under 5.1, you've got two options: either perform your conditional check within each and every loop (as each "break" can only exit one at a time), let the loops all run their course, or turn the entire cluster into a redundant function so you can return from it. All of these solutions are sub-optimal.

Goto allows you to simply exit out of the loop structure. Boom, problem solved.

That said; yes, it's the sort of statement that coders often misuse. That does not, however, make it a bad thing in itself.
Sewbacca #7
Posted 16 March 2016 - 12:36 PM
I know that the goto command is very dangerous, but if you can control it with a function, you can protect loops.
Edited on 16 March 2016 - 11:37 AM
Bomb Bloke #8
Posted 16 March 2016 - 12:41 PM
"Control it with a function"? "Protect loops"? Beats me as to what you mean, but it sounds like something that can't be done with a keyword like "goto".
Edited on 16 March 2016 - 11:42 AM
Sewbacca #9
Posted 16 March 2016 - 12:57 PM
I mean


function goto(file, keyword)
  --read the file
  --format the code after the keyword
  --set an function environment after calling loadstring, so every time, you calling a new goto, it calls coroutine.yield(arguments), before starting the new loop
  --create and start the coroutine
  --than you can handle the new loop
end
Edited on 16 March 2016 - 11:57 AM
Bomb Bloke #10
Posted 16 March 2016 - 01:32 PM
It sounds suspiciously like you'd use this for precisely the sort of thing "goto" shouldn't be used for… got an example of the sort of code you're intending to pass in as "file"?