com.adamcornett.cminus.codegen
Class ClassGenerator

java.lang.Object
  extended by com.adamcornett.cminus.codegen.ClassGenerator

public class ClassGenerator
extends Object

Generates a Jasmin class file from an abstract syntax tree.

Uses the CMTree.emitCode(ClassGenerator) method to generate code. That method should recursively traverse the tree to generate the appropriate Jasmin code.

Author:
Adam Cornett

Field Summary
private  String className
          The name of the class, the same as name of the source file.
private  int indentLevel
          The current indent level, for making pretty code.
private  org.apache.commons.logging.Log log
           
private  PrintStream output
          The output print stream
private  org.antlr.runtime.tree.CommonTree progTree
          The root node of the AST
 
Constructor Summary
ClassGenerator(String className, org.antlr.runtime.tree.CommonTree programTree)
          Create a new Class Generator, based on the class name and AST parameters
 
Method Summary
 void emitComment(String message, Object... args)
          Emit a comment in to the Jasmin file.
Should be called from a AST node's emit code method.
private  void emitConstructor()
           
private  void emitFoot()
          emit the footer code
private  void emitHead()
          Emit the class header code
private  void emitLB()
          Emit a blank line
 void emitLine(String code, Object... args)
          Emit a line of code.
private  void emitNode(ProgramTree node)
          Begin emiting code starting with the root node
private  void emitPrintFunctions()
           
 void generateCode()
          Generate the Jasmin code for the class
 String getClassName()
           
private  String getIndents()
          Get a string with the appropriate number of tabs
 void popIndent()
          Decrease the indent level
 void pushIndent()
          Increase the indent level
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

className

private final String className
The name of the class, the same as name of the source file.


indentLevel

private int indentLevel
The current indent level, for making pretty code.


log

private final org.apache.commons.logging.Log log

output

private final PrintStream output
The output print stream


progTree

private final org.antlr.runtime.tree.CommonTree progTree
The root node of the AST

Constructor Detail

ClassGenerator

public ClassGenerator(String className,
                      org.antlr.runtime.tree.CommonTree programTree)
               throws FileNotFoundException
Create a new Class Generator, based on the class name and AST parameters

Parameters:
className - The name of the class to create
programTree - The AST of the program
Throws:
FileNotFoundException - If there is an error creating the output file.
Method Detail

emitComment

public void emitComment(String message,
                        Object... args)
Emit a comment in to the Jasmin file.
Should be called from a AST node's emit code method.

Parameters:
message - The formatted string
args - The arguments for printf
See Also:
String.format(String, Object...)

emitLine

public void emitLine(String code,
                     Object... args)
Emit a line of code.
Should be called from a AST node's emit code method.

Parameters:
code - The formatted line of code to emit
args - The arguments for printf
See Also:
String.format(String, Object...)

generateCode

public void generateCode()
Generate the Jasmin code for the class


getClassName

public String getClassName()

popIndent

public void popIndent()
Decrease the indent level


pushIndent

public void pushIndent()
Increase the indent level


emitConstructor

private void emitConstructor()

emitFoot

private void emitFoot()
emit the footer code


emitHead

private void emitHead()
Emit the class header code


emitLB

private void emitLB()
Emit a blank line


emitNode

private void emitNode(ProgramTree node)
Begin emiting code starting with the root node

Parameters:
node - The root node of the AST

emitPrintFunctions

private void emitPrintFunctions()

getIndents

private String getIndents()
Get a string with the appropriate number of tabs

Returns:
A string consisting of indentLevel number of tabs.