91 lines
2.2 KiB
Java
91 lines
2.2 KiB
Java
|
/*
|
||
|
* Name: Mike Cifelli
|
||
|
* Course: CIS 443 - Programming Languages
|
||
|
* Assignment: Lisp Interpreter 2
|
||
|
*/
|
||
|
|
||
|
package eval;
|
||
|
|
||
|
import parser.*;
|
||
|
import java.util.HashMap;
|
||
|
|
||
|
/**
|
||
|
* A <code>SymbolTable</code> maps symbol names to values.
|
||
|
*/
|
||
|
public class SymbolTable {
|
||
|
|
||
|
private HashMap<String, SExpression> table;
|
||
|
private SymbolTable parent;
|
||
|
|
||
|
/**
|
||
|
* Create a new symbol table with no parent.
|
||
|
*/
|
||
|
public SymbolTable() {
|
||
|
this(null);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Create a new symbol table with the specified parent.
|
||
|
*
|
||
|
* @param parent
|
||
|
* the parent of this symbol table
|
||
|
*/
|
||
|
public SymbolTable(SymbolTable parent) {
|
||
|
this.table = new HashMap<String, SExpression>();
|
||
|
this.parent = parent;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Determine if the specified symbol name is in this symbol table.
|
||
|
*
|
||
|
* @param symbolName
|
||
|
* the name of the symbol to look up
|
||
|
* @return
|
||
|
* <code>true</code> if the symbol is in this symbol table;
|
||
|
* <code>false</code> otherwise
|
||
|
*/
|
||
|
public boolean contains(String symbolName) {
|
||
|
return table.containsKey(symbolName);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns the value to which the specified symbol name is mapped in this
|
||
|
* symbol table.
|
||
|
*
|
||
|
* @param symbolName
|
||
|
* the name of the symbol whose associated value is to be returned
|
||
|
* @return
|
||
|
* the value to which this symbol table maps <code>symbolName</code>, or
|
||
|
* null if no mapping exists
|
||
|
*/
|
||
|
public SExpression get(String symbolName) {
|
||
|
return table.get(symbolName);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Associates the specified symbol name with the specified value in this
|
||
|
* symbol table. If the symbol table previously contained a mapping for
|
||
|
* this symbol name, the old value has been replaced.
|
||
|
*
|
||
|
* @param symbolName
|
||
|
* the name of the symbol with which the specified value is to be
|
||
|
* associated
|
||
|
* @param value
|
||
|
* the value to be associated with the specified symbol name
|
||
|
*/
|
||
|
public void put(String symbolName, SExpression value) {
|
||
|
table.put(symbolName, value);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns the parent of this symbol table.
|
||
|
*
|
||
|
* @return
|
||
|
* the parent of this symbol table
|
||
|
*/
|
||
|
public SymbolTable getParent() {
|
||
|
return parent;
|
||
|
}
|
||
|
|
||
|
}
|