com.adamcornett.cminus
Class Compiler

java.lang.Object
  extended by com.adamcornett.cminus.Compiler

public class Compiler
extends Object

This is the main entry point for the compiler.

Author:
Adam Cornett

Nested Class Summary
private static class Compiler.flag
          Represents a command line argument or flag
 
Field Summary
private static HashMap<String,String> args
          The set of command line arguments and the values
static int BuildNumber
          The automatically generated build number
static String BuildStatus
          The build status of the program
private static String className
          The class name of the file being compiled
private static Function curFunc
          The current function the parser is in, stored to make adding & defining functions easier
private static Set<String> extra_args
          The list of files passed via the command line
private static HashSet<String> flags
          The collection of set command line flags
private static FunctionTable functions
          The function table for the current program
private static boolean inFuncDec
          TRUE if the scanner is currently in a function definition, FALSE otherwise.
private static int lastScopeNumber
          This is a hack var to store the last scope number for the parser when it builds the AST.
private static org.apache.commons.logging.Log log
          The Commons Log for the class
private static String logFileName
          The name of the log file being used
static String MajorVersion
          Major version number, pulled from the messages.properties file
static String MinorVersion
          Minor version number, pulled from the messages.properties file.
private static Scope rootScope
          The root("Global") variable scope
 
Constructor Summary
Compiler()
           
 
Method Summary
static void addFunction(Function f)
          Add a function to the function table.
static void addFunctionCall(org.antlr.runtime.Token t)
          When the parser encounters a function call, the ID token is passed to this function.
private static void addIOFunctions()
           
static void addSymbol(Symbol s)
          Add a symbol to the current scope's symbol table.
static void addSymbolUse(org.antlr.runtime.Token t)
          When the parser encounters a variable, it passes the token to this method, where it is added to the symbols use list.
private static boolean compile(org.antlr.runtime.CharStream input)
          This is the actual compile method
static boolean compile(File file)
          This method compiles a given source file into a Jasmin file.
static boolean compile(InputStream source)
           
static boolean compile(String source)
           
static void endFuncDec()
          Called at the end of a function declaration, used in combination witht he addFunction(Function) method to build a function defintion.
static int enterScope(org.antlr.runtime.Token t)
          Creates and pushes a new scope based on the token
static void exitScope()
          Called at the end of the scope to go up the scope chain.
static Function findFunction(String text)
          Looks for a function in the global function table.
static String getArg(String a)
          Returns the value of an argument
static int getLastScopeNumber()
          This method provides the tree constructions constructors with the the last scope number.
protected static void init_flags()
          Setup all possible command line flags
protected static void init_log()
          Setup the log
static boolean isFlag(String f)
          Checks if a given flag has been set
static void main(String[] args)
          The main method of the program
static void printf(String s, Object... args)
          Makes life easier
private static void printUsage()
          Print the information about how to execute the program and the available flags.
private static void printVersion()
          Print version and build information
private static void processArgs(String[] args)
          Process the command line args array into the flags and args data structures
static void puts(Object o)
          A quick wrapper for PrintStream.println(Object)
private static void rebuild()
          Recreates the ANTLR parser and lexer
static void setFlag(String string)
          Set a flag
static void setLastScopeNumber(int lastScopeNumber)
          Sets the last scope number value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BuildNumber

public static final int BuildNumber
The automatically generated build number


BuildStatus

public static final String BuildStatus
The build status of the program


MajorVersion

public static final String MajorVersion
Major version number, pulled from the messages.properties file


MinorVersion

public static final String MinorVersion
Minor version number, pulled from the messages.properties file.


args

private static HashMap<String,String> args
The set of command line arguments and the values


className

private static String className
The class name of the file being compiled


curFunc

private static Function curFunc
The current function the parser is in, stored to make adding & defining functions easier


extra_args

private static Set<String> extra_args
The list of files passed via the command line


flags

private static HashSet<String> flags
The collection of set command line flags


functions

private static FunctionTable functions
The function table for the current program


inFuncDec

private static boolean inFuncDec
TRUE if the scanner is currently in a function definition, FALSE otherwise.


lastScopeNumber

private static int lastScopeNumber
This is a hack var to store the last scope number for the parser when it builds the AST.


log

private static org.apache.commons.logging.Log log
The Commons Log for the class


logFileName

private static String logFileName
The name of the log file being used


rootScope

private static Scope rootScope
The root("Global") variable scope

Constructor Detail

Compiler

public Compiler()
Method Detail

addFunction

public static void addFunction(Function f)
Add a function to the function table.

Parameters:
f - The new function to add
See Also:
functions

addFunctionCall

public static void addFunctionCall(org.antlr.runtime.Token t)
When the parser encounters a function call, the ID token is passed to this function. The location of the call is passed to the function's Function.addCall(TokenLocation) method.

Parameters:
t - The ID token of the call

addSymbol

public static void addSymbol(Symbol s)
Add a symbol to the current scope's symbol table. This method is called by the parser when it encounters a variable definition.

Parameters:
s - The symbol to add.
See Also:
Scope.pushScope(Scope)

addSymbolUse

public static void addSymbolUse(org.antlr.runtime.Token t)
When the parser encounters a variable, it passes the token to this method, where it is added to the symbols use list.

Parameters:
t -
See Also:
Symbol.addUse(TokenLocation)

compile

public static boolean compile(File file)
                       throws IOException,
                              org.antlr.runtime.RecognitionException
This method compiles a given source file into a Jasmin file.

Parameters:
fname - The name of the C-Minus source file
Throws:
IOException - If the file cannot be opened
org.antlr.runtime.RecognitionException - If there is an error during parsing
See Also:
compile(CharStream)

compile

public static boolean compile(InputStream source)
                       throws IOException,
                              org.antlr.runtime.RecognitionException
Parameters:
source - An input stream representing the source to be compiled
Returns:
TRUE if the code is compiled without errors, otherwise FALSE.
Throws:
IOException - If there is an error reading from the InputStream.
org.antlr.runtime.RecognitionException - If there is an error during compilation.
See Also:
compile(CharStream)

compile

public static boolean compile(String source)
                       throws IOException,
                              org.antlr.runtime.RecognitionException
Parameters:
source - A string containing the source to be compiled.
Returns:
TRUE if the code is compiled without errors, otherwise FALSE.
Throws:
IOException - If there is an error reading the string during parsing.
org.antlr.runtime.RecognitionException - Thrown if there is an error during the compilation.
See Also:
compile(CharStream)

endFuncDec

public static void endFuncDec()
Called at the end of a function declaration, used in combination witht he addFunction(Function) method to build a function defintion.


enterScope

public static int enterScope(org.antlr.runtime.Token t)
Creates and pushes a new scope based on the token

Parameters:
t - The token to associated with beginning with the scope
Returns:
The scope number of the new scope
See Also:
Scope.pushScope(Scope)

exitScope

public static void exitScope()
Called at the end of the scope to go up the scope chain.

See Also:
Scope.popScope()

findFunction

public static Function findFunction(String text)
Looks for a function in the global function table.

Parameters:
text - The name of the function.
Returns:
The funciton object if it is found, otherwise null if it is not found.

getArg

public static String getArg(String a)
Returns the value of an argument

Parameters:
a - The argument name to get the value for
Returns:
the value of the argument passed in for argument a

getLastScopeNumber

public static int getLastScopeNumber()
This method provides the tree constructions constructors with the the last scope number. This is needed because the ANTLR tree construction occurs after the exitScope() method gets called.

Returns:
The scope number of the last scope created

isFlag

public static boolean isFlag(String f)
Checks if a given flag has been set

Parameters:
f - The flag to be checked for
Returns:
True if flag f is set

main

public static void main(String[] args)
The main method of the program

Parameters:
args - execution arguments

printf

public static void printf(String s,
                          Object... args)
Makes life easier

Parameters:
s - The format string.
args - The arguments/values
See Also:
PrintStream.printf(String, Object...)

puts

public static void puts(Object o)
A quick wrapper for PrintStream.println(Object)

Parameters:
o - The object to print

setFlag

public static void setFlag(String string)
Set a flag

Parameters:
string - the name of the flag (without the '--')

setLastScopeNumber

public static void setLastScopeNumber(int lastScopeNumber)
Sets the last scope number value.

Parameters:
lastScopeNumber -
See Also:
getLastScopeNumber()

addIOFunctions

private static void addIOFunctions()

compile

private static boolean compile(org.antlr.runtime.CharStream input)
                        throws IOException,
                               org.antlr.runtime.RecognitionException
This is the actual compile method

Parameters:
input - The input to use
Returns:
true If there are no errors, false otherwise
Throws:
IOException
org.antlr.runtime.RecognitionException

printUsage

private static void printUsage()
Print the information about how to execute the program and the available flags.


printVersion

private static void printVersion()
Print version and build information


processArgs

private static void processArgs(String[] args)
Process the command line args array into the flags and args data structures

Parameters:
args -

rebuild

private static void rebuild()
Recreates the ANTLR parser and lexer


init_flags

protected static void init_flags()
Setup all possible command line flags


init_log

protected static void init_log()
Setup the log