Hacker News new | ask | show | jobs
by gilgad13 5051 days ago
If we're golfing:

https://github.com/quartzjer/js0n/blob/master/js0n.c

Appears to work the same way, though it doesn't bubble back type information.

(Also, the `goto * go[ * cur];` trick was pretty crazy the first time I saw it)

2 comments

This is called computed (or assigned) goto and is a GCC extension (that is, not in the C standard): http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html
Note that other compilers (such as Oracle Solaris Studio) support computed gotos as well:

  http://docs.oracle.com/cd/E19205-01/820-7598/bjabt/index.html
And LLVM:

  http://blog.llvm.org/2010/01/address-of-label-and-indirect-branches.html
Arg! "[0 ... 255] = &&l_bad" compiles as valid C, totally confused by this code. Is this some weird gcc specific extension? Hm, probably part of the C99 array extensions. Checking...

Answer: C99, borrowed from a GCC extension. http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Designated-Inits...

There are two not in C99 gcc extensions in that snippet. && to take the address of a label, and ... to produce a range of indices. It may also happen to be a designated initializer, but the whole piece is far from standard.
Yeah, I was interested in the [0...255] thing, the calculated gotos were discussed in the other comment.

As far as js0n, it's a great example of cool things that can be done in gcc, but isn't really useful at all since it only handles the first level of depth and has no typing. The jsmn, with typing and nesting, is actually usable, also compiles a lot smaller than the big tables in js0n.