package ca.uwaterloo.joos.symboltable;

import ca.uwaterloo.joos.ast.decl.VariableDeclaration;
import ca.uwaterloo.joos.ast.expr.name.SimpleName;


public class BlockScope extends Scope {
	
	protected Scope parent;

	public BlockScope(String name, Scope parent) {
		super(name);
		this.parent = parent;
	}
	
	public String nameForDecl(VariableDeclaration var) throws Exception {
		String name = this.getName() + "." + var.getName().getName();
		return name;
	}

	public void addVariableDecl(VariableDeclaration var) throws Exception {
		String name = this.nameForDecl(var);
		TableEntry entry = new TableEntry(name, var);

		this.symbols.put(name, entry);
	}

	public TableEntry getVariableDecl(VariableDeclaration var) throws Exception {
		return this.symbols.get(this.nameForDecl(var));
	}
	
	@Override
	public boolean resolveSimpleNameType(SimpleName name) throws Exception {
//		List<TableEntry> matches = new ArrayList<TableEntry>();
//		matches = this.entriesWithSuffix(this.symbols.values(), name.getName());
//		
//		if(matches.size() > 1) {
//			throw new SymbolTableException("More than one match was found in Block Scope for type " + name);
//		} else if(matches.size() == 1) {
//			return true;
//		}
		
		return this.parent.resolveSimpleNameType(name);
	}

	@Override
	public void listSymbols(){
		System.out.println("\tParent:");
		System.out.println("\t\t" + this.parent);
		
		super.listSymbols();
	}
}

@@ -33,16 +33,71 @@
 table;%0A%0A
+import java.util.List;%0A%0Aimport org.hamcrest.Matchers;%0A%0A
 import c
@@ -194,16 +194,113 @@
 leName;%0A
+import ca.uwaterloo.joos.symboltable.SymbolTable.SymbolTableException;%0Aimport ch.lambdaj.Lambda;%0A
 %0A%0Apublic
@@ -719,16 +719,166 @@
 l(var);%0A
+%09%09if(this.getLocalVariable(var.getName().getName()) != null) %7B%0A%09%09%09throw new SymbolTableException(%22Duplicate Variable Declaration of %22 + name);%0A%09%09%7D%0A%09%09%0A
 %09%09TableE
@@ -1089,16 +1089,401 @@
 );%0A%09%7D%0A%09%0A
+%09public TableEntry getLocalVariable(String name) %7B%0A%09%09List%3CTableEntry%3E vars = Lambda.select(this.symbols.values(), Lambda.having(Lambda.on(TableEntry.class).getName(), Matchers.endsWith(%22.%22 + name)));%0A%09%09if(vars.size() == 1) %7B%0A%09%09%09return vars.get(0);%0A%09%09%7D else if(this.parent instanceof BlockScope) %7B%0A%09%09%09return ((BlockScope) this.parent).getLocalVariable(name);%0A%09%09%7D%0A%09%09%0A%09%09return null;%0A%09%7D%0A%09%0A
 %09@Overri

Check duplicate local variable declaration