Brew your own coffee...script
A simple transcompiler in Ruby
Txus (@txustice) - Twitter, GitHub
CoffeeScript
- Source-to-source compiler (or transcompiler)
- Ruby-like language that compiles to JavaScript
CoffeeScript
JavaScript
Transcompiling: how it works
Roll our own... RubyScript!
- Compile a subset of Ruby to JavaScript
- Leverage Melbourne (the Rubinius parser)
- We could do it with any other Ruby parser (Ripper, you-name-it...)
AST representation of a simple program
AST nodes and the Visitor pattern
- Every AST node responds to
#visit
- Every node, when called
#visit on it with a visitor, calls a method on the visitor
- Example: FixnumLiteralNode#visit(visitor) will call
#fixnum_literal on the visitor
Improving our Visitor
- Keeping track of local variables
- Adding proper indentation
- Wrapping output code in a closure
TA-DA!!! RubyScript improved!
RubyScript
JavaScript
That's all folks!
Get RubyScript while it's still hot!
~400 LOC
$ curl http://bit.ly/AsoU9w > rubyscript
$ chmod +x rubyscript
$ ./rubyscript my_file.rb > my_file.js
Go check out the code here:
https://gist.github.com/2017173
By Txus (@txustice) - Twitter, GitHub