The Bistro compiler translates Bistro source code into Java source code and uses a standard Java compiler to generate Java class files. The classes that embody the abstract syntax tree (AST) originally included the methods for generating code (an example use of the Composite pattern). However, while convenient for the compiler prototype, this approach limited the use of the AST classes to code generation. The AST classes were also intended to support other purposes. So, a change in the approach used for code generation was needed. The change has also led to a dramatic improvement in the readability of the generated Java code.
The Bistro compiler now uses a variation of the Visitor pattern to
organize and consolidate the code generation methods. Ordinarily, the Visitor
pattern is implemented using a single interface that includes the methods for
visiting all the elements of a Composite structure. However, the usual approach
to Visitor design complicates the implementation of the visitors if new classes
need to be added to the Composite structure. The approach taken with the Bistro
compiler overcomes the usual limitation of the Visitor pattern by distributing
the definition of the Visitor interfaces to the various AST classes. Taking
advantage of the Java interface mechanism, each AST class defines its own
Visitor as a public static interface. Then, the classes that contain the
code generation methods only need to implement the Visitor interfaces of those
AST classes they intend to visit. See the classes BistroJavaEncoder
and BistroJavaGenerator
in the
smalltalk.compiler package for more details.
Java is a trademark of Sun Microsystems, Inc.
Permission is granted to copy this document provided this
copyright statement is retained in all copies.
Copyright 1999-2001 Nikolas
S. Boyd.