Homework B (benchmarks)
This commit is contained in:
parent
72cc3206c4
commit
76fbabdf53
141 changed files with 7540 additions and 2032 deletions
|
@ -3,24 +3,23 @@ package cd.frontend.semantic;
|
|||
import cd.frontend.semantic.SemanticFailure.Cause;
|
||||
import cd.ir.Symbol;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* A simple symbol table, with a pointer to the enclosing scope.
|
||||
/**
|
||||
* A simple symbol table, with a pointer to the enclosing scope.
|
||||
* Used by {@link TypeChecker} to store the various scopes for
|
||||
* local, parameter, and field lookup. */
|
||||
* local, parameter, and field lookup.
|
||||
*/
|
||||
public class SymTable<S extends Symbol> {
|
||||
|
||||
|
||||
private final Map<String, S> map = new HashMap<String, S>();
|
||||
|
||||
|
||||
private final SymTable<S> parent;
|
||||
|
||||
|
||||
public SymTable(SymTable<S> parent) {
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
|
||||
public void add(S sym) {
|
||||
// check that the symbol is not already declared *at this level*
|
||||
if (containsLocally(sym.name))
|
||||
|
@ -28,10 +27,21 @@ public class SymTable<S extends Symbol> {
|
|||
map.put(sym.name, sym);
|
||||
}
|
||||
|
||||
public List<S> allSymbols() {
|
||||
List<S> result = new ArrayList<S>();
|
||||
SymTable<S> st = this;
|
||||
while (st != null) {
|
||||
for (S sym : st.map.values())
|
||||
result.add(sym);
|
||||
st = st.parent;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public Collection<S> localSymbols() {
|
||||
return this.map.values();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* True if there is a declaration with the given name at any
|
||||
* level in the symbol table
|
||||
|
@ -40,7 +50,7 @@ public class SymTable<S extends Symbol> {
|
|||
return get(name) != null;
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* True if there is a declaration at THIS level in the symbol
|
||||
* table; may return {@code false} even if a declaration exists
|
||||
* in some enclosing scope
|
||||
|
@ -49,9 +59,10 @@ public class SymTable<S extends Symbol> {
|
|||
return this.map.containsKey(name);
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Base method: returns {@code null} if no symbol by that
|
||||
* name can be found, in this table or in its parents */
|
||||
* name can be found, in this table or in its parents
|
||||
*/
|
||||
public S get(String name) {
|
||||
S res = map.get(name);
|
||||
if (res != null)
|
||||
|
@ -60,11 +71,12 @@ public class SymTable<S extends Symbol> {
|
|||
return null;
|
||||
return parent.get(name);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Finds the symbol with the given name, or fails with a
|
||||
* Finds the symbol with the given name, or fails with a
|
||||
* NO_SUCH_TYPE error. Only really makes sense to use this
|
||||
* if S == TypeSymbol, but we don't strictly forbid it... */
|
||||
* if S == TypeSymbol, but we don't strictly forbid it...
|
||||
*/
|
||||
public S getType(String name) {
|
||||
S res = get(name);
|
||||
if (res == null)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue