Code from my series on writing a Ruby compiler in Ruby
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
examples
features
lib
spec
test
.gitignore
AUTHORS
Dockerfile
Gemfile
Gemfile.lock
Makefile
README.md
Rakefile
arrayoutput.rb
ast.rb
atom.rb
classcope.rb
compile
compile_arithmetic.rb
compile_calls.rb
compile_class.rb
compile_comparisons.rb
compile_control.rb
compile_include.rb
compiler.rb
controlscope.rb
cucumber
debugscope.rb
driver.rb
emitter.rb
extensions.rb
funcscope.rb
function.rb
globals.rb
globalscope.rb
iooutput.rb
localvarscope.rb
operators.rb
output_functions.rb
parser.rb
parserbase.rb
print_sexp.rb
quoted.rb
regalloc.rb
register.rb
saveregs.rb
scanner.rb
scope.rb
sexp.rb
sexpscope.rb
shunting.rb
splat.rb
stackfence.rb
sym.rb
tokenizeradapter.rb
tokens.rb
trace.rb
transform.rb
treeoutput.rb
utils.rb
value.rb
vtableoffsets.rb

README.md

Writing a (Ruby) compiler in Ruby

Source for my series on writing a compiler in Ruby.

See http://www.hokstad.com/compiler

NOTE This is still wildly incomplete.

Caveats

This section covers caveats about compiled Ruby vs. MRI, not generally missing pieces or bugs in the current state of the compiler (of which there are many).

require

Presently, "require" is evaluated statically at compile time.

This makes certain Ruby patterns hard or impossible to support. E.g. reading the contents of a directory and caling "require" for each .rb file found will not presently work, and may never work, as it is not clear in the context of compilation whether or not the intent is to load this file at compile time or runtime.

Ruby allows the argument to "require" to be dynamically generated. E.g. "require File.dirname(FILE) + '/blah'". To facilitate compatibility, limited forms of this pattern may eventually be supported.

On MRI, "require" is generally overridden by a custom version for rubygems or bundler. This is not likely to ever be supported. "require" is likely to be treated as a keyword, rather than as an overrideable method.

$0

While $0 will at some point be initialized with the name of the file compilation is triggered for, certain patterns of Ruby, such as conditionally executing code based on whether a given file is executed directly are conceptually different, given that $0 gets bound at compile time.

We'll need to consider if the right behaviour is for $0 and/or __FILE__ to contain the equivalent of C's argv[0] instead. Possibly make $0 and __FILE__ refer to different things.

$:, $LOAD_PATH

The load path is malleable in MRI, and this is very frequently used alongside certain methods to modify which files may be loaded. Currently this is not supported.

It is likely that for compatibility a limited subset of Ruby will be interpreted at compile time to support some forms of this pattern. See also "require"