Jump to content




Goto

lua help

  • You cannot reply to this topic
9 replies to this topic

#1 Sewbacca

  • Members
  • 450 posts
  • LocationStar Wars

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 by Sewbacca, 15 March 2016 - 06:02 PM.


#2 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

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.

#3 Sewbacca

  • Members
  • 450 posts
  • LocationStar Wars

Posted 15 March 2016 - 07:37 PM

Are there any not pessimistic ideas?

#4 Dragon53535

  • Members
  • 973 posts
  • LocationIn the Matrix

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.

#5 Lupus590

  • Members
  • 2,028 posts
  • LocationUK

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.../Spaghetti_code

#6 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 16 March 2016 - 12:14 AM

View PostLupus590, on 15 March 2016 - 09:26 PM, said:

If you need a GOTO you are likely doing something very badly. See this wiki page for more information: https://en.wikipedia.../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.

#7 Sewbacca

  • Members
  • 450 posts
  • LocationStar Wars

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 by Sewbacca, 16 March 2016 - 12:37 PM.


#8 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

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 by Bomb Bloke, 16 March 2016 - 12:42 PM.


#9 Sewbacca

  • Members
  • 450 posts
  • LocationStar Wars

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 by Sewbacca, 16 March 2016 - 12:57 PM.


#10 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

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"?





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users