A JavaScript parser and interpreter written in Go
github.com
github.com
It looks like this interpreter is using tagged unions for values, and using the empty interface to emulate a union type. I seem to remember that we may have read something at the time that recommended using the empty interface instead of unions, though I don't remember for sure. Nice to see some interpretation efforts finally being realized in Go!
It's less convenient than writing a compiler in Haskell, but then, what isn't? It does give you reasonable type safety, though. (Again, don't say that where a Haskell programmer is listening, but it's at least decent.)
But for better performance, approaches like tagged pointers (often used in Lisp implementations) are useful, as it eliminates a level of indirection for most integer operations. If you can afford to use 64 bits for your values, you might consider using doubles everywhere, with invalid exponents for stuffing a shortened pointer inside the float (I believe luajit uses, or used to use, this technique; it would probably be a good match for JS as well, as JS doesn't have integers).
Is it because JS has dynamic types ? var x = 5; x = "John Doe";
For example, you can have a union between a 64-bit pointer and a 32-bit type identifier plus 32-bit value. This means you can store 32-bit integers in the same space as a pointer.
That being said, using interface{} for unions is extremely unfortunate. The bright side is that after 4 years of using Go almost exclusively, I only had to abuse interface{} only once, with compiler parsers (like the parent says). Every other time there was a better design which did not require using interface{}.
I'm actually really excited by all the people starting to jump in and try to learn/help. We've even got 2 really smart high schoolers doing some amazing contributions to GHC recently.
seriously: its easy to get involved in hacking on interesting open source projects. Just choose one you care about and stay excited by, and dig in!
I entertained the idea that maybe it would have been written in JavaScript, but I think a JS interpreter written in JS has already been done at least once.
Edit to add link to example code using Otto https://github.com/couchbase/sync_gateway/blob/master/src/gi...
Since gofmt sorts imports, the grouping is soon undone.
In any case, it is quite easy to distinguish the three kinds of imports if local imports share a common prefix or set of prefixes.
Am I missing something here?
I can't think of any reasonable use case. Grab little NPM ditties and incorporate them into your Go binary - Javascript to Go becomes as Lua is to C? Somebody enlighten me.
Edit: Not that this needs a use case per say, just that the intent behind it is underspecified enough for me to wonder about it.
Duckduckgo goodies[2] also a good example of what we've been doing.
Imagine a highly dynamic web page, beyond the initial page load when you interact with the page javascript executes the user's actions then rewrites large chunks of the page. As a developer, you need to write code on your backend that knows how to render the initial HTML for the page, but then you have to duplicate this functionality in JavaScript since the client needs to be able to render any chunk of the page that changes in response to a request.
You have a few options as a developer here. You can live with maintaining two code paths in different languages that do essentially the same thing. Or you can get rid of the backend rendering entirely, making your page less friendly to no-script users and web crawlers (and sometimes making the site flicker a bit as content gets loaded initially for all users.)
Or you can use shared code on the backend and the client for rendering HTML by having a backend that speaks JavaScript.
This project may not be the fastest JS engine or the one with the most features right now, but if nothing else, it's a really nice project for learning Go and writing interpreters.
Maybe some new ideas will be explored in this engine first, because it's faster to implement them in Go than in the (huge) V8 or other JS engines.
Having new alternatives to established software is always a good thing.
Oh, wait causality doesn't work that way. Never mind. :P
[0]: http://en.wikipedia.org/wiki/Continuation-passing_style
Not serious. haha.
Because Google Closure Compiler is unbelievably slow
1. No cgo required. 2. Portable to any platform Go runs on that V8 doesn't.