Thursday, December 17, 2009

ThothCompiler revamped

ThothCompiler has been revamped very near completely. Get it by executing:

Gofer it
squeaksource: 'ASTBridge';
addPackage: 'ThothCompiler';
load


Then go to the Preference Browser (System->Preferences in the world menu) and choose ThothCompiler for the option "whichCompiler".

ThothCompiler allows you to use a modern AST in Pharo, rather than the 20 year old default one. The code is parsed with Lukas' really fast RBParser and then transformed to the 20 year old nodes, from there compiled. What's the advantage?

Well, you can transform the modern AST nodes before compilation! It has a great visitor API, that lets you do all kinds of transformations. You can use ThothCompiler as is just to feel fancy, or better: subclass it and overwrite the transform: method, in which you can transform the AST to your liking.

ThothCompiler is now used in Helvetica (albeit under the name of RBCompiler, because things can never have enough names!).

Changes since the last blog post:
  • Now uses the RBParser, which is much faster than SmaCC
  • No more dependency on the NewCompiler or AST package
  • Adds a preference to the system (in the system menu), where you can choose the compiler, even when you can't compile anymore :)

Tuesday, December 15, 2009

What to keep in mind when playing with compilers in Pharo

If you play with the compiler in Pharo, here's an urgent hint: don't lock yourself out of your image! If you break the compiler, you want to have something to fall back to. So here's what you do.

The compiler for the system is whatever Behavior compilerClass returns, so change that method to read out a preference:

Behavior>compilerClass
"Answer a compiler class appropriate for source methods of this class."
|default|
default := Compiler.
^(Smalltalk classNamed: (Preferences valueOfPreference: #whichCompiler ifAbsent: [^ default]) ) ifNil: [default]


That's step 1. Second step is to create the preference, so you can read it out at all:

Preferences addTextPreference: #whichCompiler category: #compiler default: 'Compiler' balloonHelp: 'Compiler to be used throughout the system' .

That's it, you're set. Happy hacking!