AlexSexton.com $('human#alex thoughts').filter(':not(.bs)');

4Jul/098

Goto.js

Today I launched my new site www.summerofgoto.com. It is the official new homepage for my new script Goto.js. Goto.js adds the functionality of the goto command into native javascript. It uses James Padolsey's parseScripts as a preparser and rewrites the input code to be valid javascript. Leave it to me to write programs solely for the comedic value, but I actually did find some interest in the fact that it is challenging to implement functionality that is so basic to low level programming.

The script (which can be downloaded at the site) hinges on a single heuristic for implementing Goto. In javascript, while loops can have labels, and they can also have 'continue' statements that accept a label as the parameter.

e.g.

label1: while (i < 10) {
  if (!valid(i)) {
    continue label1;
  }
i++;
}

In effect, this is just a crappy goto statement that is confined to a special case. So my goal in goto.js was to extend this case to all cases. I did this by surrounding everything after a label with a while loop that only runs once by default. The only tricky part being that while loops cannot stretch across multiple functions or blocks. So each while loop ends at the end of its block scope, not the file or script. This is an interesting problem, because typically, when goto was around and actually heavily used, there weren't a whole lot of functions being written. Even if I was somehow able to allow you to start running code from the middle of a function from the middle of a different function, I don't think it would work, because the context of all the variables that have been passed into the new function cannot be guaranteed to exist.

The only feature that I am actively toying with currently is a way to allow goto satements to jump to labels that haven't been declared yet. After reading a bit on the subject, different languages had different rules about this feature in the past, and some languages therefore also did/do not support forward looking goto statements.

Enjoy the Summer of Goto!

P.S. For anyone who is wondering (so far a total of zero people), Summer of Goto is a term that was popularized by Paul Irish, while discussing this script and PHP's decision to add goto into their language.

And for those who do not immediately recognize that this entire thing is a joke, please forgive me. <--(insurance).

Comments (8) Trackbacks (0)
  1. WHY?? Call functions instead!!!!!

    WHY YOU SHOULD NEVER USE GOTO: http://en.wikipedia.org/wiki/Spaghetti_code

    Also: http://xkcd.com/292/

  2. As with most everything else you can think of , this feature is already available for Python. After reading your excellent article here, Richie Hindle borrowed Guido’s time machine and went back just over 5 years to add goto to the Python language:

    http://entrian.com/goto/

    Michael Chermside

    (PS: this really IS a great hack! Now if I could just get Guido to loan ME the time machine, I’d go back and add goto to the first language I ever learned: Basic.)

  3. How are you people missing that this is a joke? Step away from the warm glow of your monitor for a few moments and regain your foothold on reality.

  4. @Stamp

    Thanks for the input man, however, I may or may not have linked to that comic myself, on the actual page. :D

    @Michael

    Thanks for channeling Guido for me. He’s my hero. Time machine and all.

  5. Excellent. XD
    Glad to see the nod to Dijkstra.

  6. You made me laugh so much, that I’m thinking to implement GOTOs in a bunch of languages for the next april’s fool.

    I just couldn’t resist to talk about the “awesomeness” of GOTOs in my blag (http://blogs.cemzoo.com/rogelio/blog/u98/124826-maravillas-de-la-programacion-goto.html).

  7. I think we need the Winter of ComeFrom now! However it should not be used with labels, instead relative jumps are needed.

  8. This is a great script!
    Thanks so much for sharing.


Leave a comment


No trackbacks yet.