27 comments

[ 3.7 ms ] story [ 52.4 ms ] thread
Twostroke is also capable enough to run the CoffeeScript compiler: http://github.com/charliesome/coffee-script-pure
Very cool! If you added a Twostroke adapter to ExecJS (https://github.com/sstephenson/execjs), the ruby-coffee-script gem would work without modification.
I've been thinking of making a pure-ruby javascript interpreter in order to embed it into ExecJS as a fallback for when nothing else is present.

I believe this is only the case on Linux, but I've found that it gives my coworkers who are trying out rails for the first time quite a bit of confusion. (For that matter, it would help if execjs appended #readme in its error message instead of just to the github page, since the same coworkers were also unfamiliar with github, and didn't realize they had to look below the fold for the list of runtimes.)

Looks fun, but it'd be interesting to hear more about your rationale for this vs. embedding V8 etc.
Something like Twostroke could be useful if for some reason or other you can't embed V8 (eg. no platform support, no access to compilers, etc.) Twostroke is dog slow compared to a 'real' JavaScript engine, but it will do the job if there's no other options.

It's also a fun project that has taught me a lot about parsers, language implementations and JavaScript's object model.

Loving the pun on the name, as "Twostroke is dog slow compared to [V8]"
Fun is always a good reason... I need to dust off my (horribly incomplete) Ruby compiler project.
I'm quite flabbergasted seeing this:

https://github.com/charliesome/twostroke/blob/master/lib/two...

That is, a parser which is essentially coded by hand, with all token checking, saving and restoring parser's state, functions corresponding to non-terminating symbols of grammar etc.

Are there no good parsing libraries in Ruby (similar to Python's pyparsing or Haskell's Parsec), and could this have forced the author to code his parser from scratch?

Sure there are, kpeg, citrus, treetop... Probably more I don't know about.
There's treetop, kpeg and a bunch of other stuff but I prefer writing my parsers by hand. Pretty much all that's missing from mine is semicolon insertion, but that's something I'm working to nut out.
Given that Ragel (http://www.complang.org/ragel/) can output Ruby code, it might be a great choice there.
Ragel just deals with FSMs, it can't generate a parser for a non-regular language.
You're right, thanks for the correction, I did not realize FSMs couldn't do that.
Writing a parser by hand makes for a good exercise. No need to be flabbergasted!
Forced? I've written dozens of parsers, and personally I avoid parsing libraries like the plague - unless you're writing a really simple parser with few needs for error reporting and recovery, my experience is that parsing libraries usually end up being far more pain than they are worth.
Completely agreed. Once I got the hang of hand-writing parsers, I never looked back; they're higher-quality, generally faster, easier to write, and are easier to maintain than generated parsers.
My favorite compiler writer (dgc of mwc fame) says "Yacc makes the hard part of a compiler harder and the easy part easier".
This look pretty neat. Could you share your motivation behind why you build this and some use cases? :)
Very cool! I was looking for a python js interpreter just the other day, maybe I'll take a crack at it :)
I'm starting to wonder if the people behind these sorts of projects (eg. old console emulators written in JavaScript, this, etc.) are insane or if this is all some sort of epic trolling for lulz.
It's just people enjoying themselves, it's fun to work on crazy projects (at least for some people). Don't worry about it :)
You should check out HotRuby, which is a Ruby interpreter, written in Javascript: http://hotruby.yukoba.jp/

Should be pretty interesting to see Twostroke running on HotRuby running on Twostroke running on HotRuby running...

The front page for the project hasn't been updated in over 3 years, and a quick search for github activity shows stagnation too. What direction is this project heading and how fast is it getting there?
I see no use case for this. You must be masters of both languages after this project.