com.adamcornett.cminus
Class Scope

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

public class Scope
extends Object

Represents a variable scope. This implementation is NOT multi-thread safe!

Author:
Adam Cornett

Field Summary
private  Set<Scope> childern
           
private static Scope currentScope
          The current scope that the parser or analyzer is in.
private static int nextScopeNumber
          The next scope number ot be issued.
private  Scope parentScope
          The parent of this scope.
private static Scope rootScope
          The root scope.
private static Map<Integer,Scope> scopeMap
          A map linking a given scope number to a scope.
private  int scopeNumber
          The scope number of this Scope.
private  SymbolTable symbols
          The symbol table for this scope.
private  org.antlr.runtime.Token token
          The token which is at the start of this scope, usually a function identifier, or a '{' at the beginning of a compound statement
 
Constructor Summary
Scope(org.antlr.runtime.Token t)
          Create a new scope.
 
Method Summary
 void addChild(Scope s)
          Add a child scope and set it's parent as this class
static void cleanEmpty()
           
private static void cleanEmpty(Scope s)
           
static void clear()
           
 void delete()
          Delete this scope and re-assign all childern to parent;
static Scope getCurrentScope()
          Get the current scope.
 String getName()
          Get the name of the scope
 Scope getParent()
          Get the parent scope
static Scope getScope(Integer sn)
          Get the Scope object with the given scope number.
 int getScopeNumber()
          Get this scope's number
 SymbolTable getSymbols()
          Get the symbol table for this scope
 org.antlr.runtime.Token getToken()
          Get the token this scope started on.
static Symbol lookup(String s)
          Looks up a symbol with the name s in the current scope
static Symbol lookup(String s, Scope scope)
          Look up a symbol in a specified scope
static void popScope()
          Pops the current scope, replacing with it with its parent, unless it is the root scope, which cannot be popped.
static String print()
          Prints all symbols in the table.
static void pushScope(Scope s)
          Add a new scope to the stack.
static void pushSymbol(Symbol s)
          Add a symbol to the current scope.
 void removeChild(Scope s)
          Remove a child scope
static void setScope(Integer scopeNumber)
          Lookup a given scope and set it as the current scope.
 void setSymbols(SymbolTable symbols)
          Set the symbol table for this scope.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

currentScope

private static Scope currentScope
The current scope that the parser or analyzer is in.


nextScopeNumber

private static int nextScopeNumber
The next scope number ot be issued.


rootScope

private static Scope rootScope
The root scope.


scopeMap

private static Map<Integer,Scope> scopeMap
A map linking a given scope number to a scope.


parentScope

private Scope parentScope
The parent of this scope.


childern

private Set<Scope> childern

scopeNumber

private final int scopeNumber
The scope number of this Scope.


symbols

private SymbolTable symbols
The symbol table for this scope.


token

private final org.antlr.runtime.Token token
The token which is at the start of this scope, usually a function identifier, or a '{' at the beginning of a compound statement

Constructor Detail

Scope

public Scope(org.antlr.runtime.Token t)
Create a new scope.

Parameters:
t - The token which the scope starts at.
Method Detail

cleanEmpty

public static void cleanEmpty()

getCurrentScope

public static Scope getCurrentScope()
Get the current scope.

Returns:
The current scope.

getScope

public static Scope getScope(Integer sn)
Get the Scope object with the given scope number.

Parameters:
sn - The scope number to look up.
Returns:
The scope object with the given number, or NULL if it cannot be found.

lookup

public static Symbol lookup(String s)
Looks up a symbol with the name s in the current scope

Parameters:
s - The name of the symbol to look for.
Returns:
The symbol with the name s in the current scope, or NULL if it does not exist.

lookup

public static Symbol lookup(String s,
                            Scope scope)
Look up a symbol in a specified scope

Parameters:
s - The name of the symbol to look for
scope - The scope to search for the symbol in
Returns:
The symbol with the name s in the current scope, or NULL if it does not exist.

popScope

public static void popScope()
Pops the current scope, replacing with it with its parent, unless it is the root scope, which cannot be popped.


print

public static String print()
Prints all symbols in the table.

Returns:
The string representation of all symbols in this Scope's symbol table.

pushScope

public static void pushScope(Scope s)
Add a new scope to the stack. The current scope is made the parent of s and s becomes the current scope.

Parameters:
s - The scope to push.

pushSymbol

public static void pushSymbol(Symbol s)
Add a symbol to the current scope.

Parameters:
s - The symbol to be added.

setScope

public static void setScope(Integer scopeNumber)
Lookup a given scope and set it as the current scope.

Parameters:
scopeNumber -

cleanEmpty

private static void cleanEmpty(Scope s)

addChild

public void addChild(Scope s)
Add a child scope and set it's parent as this class

Parameters:
s -

delete

public void delete()
Delete this scope and re-assign all childern to parent;


getName

public String getName()
Get the name of the scope

Returns:
A string representation of the scope

getParent

public Scope getParent()
Get the parent scope

Returns:
The scope which is the parent for this scope

getScopeNumber

public int getScopeNumber()
Get this scope's number

Returns:
The unique scope number for this scope

getSymbols

public SymbolTable getSymbols()
Get the symbol table for this scope

Returns:
The symbol table for this scope.

getToken

public org.antlr.runtime.Token getToken()
Get the token this scope started on.

Returns:
A token that caused the start of the scope.

removeChild

public void removeChild(Scope s)
Remove a child scope

Parameters:
s -

setSymbols

public void setSymbols(SymbolTable symbols)
Set the symbol table for this scope.

Parameters:
symbols - The new Symbol Table

toString

public String toString()
Overrides:
toString in class Object

clear

public static void clear()