<commit_before>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;
//		}
		<commit_msg>Check duplicate local variable declaration<commit_after>package ca.uwaterloo.joos.symboltable;

import java.util.List;

import org.hamcrest.Matchers;

import ca.uwaterloo.joos.ast.decl.VariableDeclaration;
import ca.uwaterloo.joos.ast.expr.name.SimpleName;
import ca.uwaterloo.joos.symboltable.SymbolTable.SymbolTableException;
import ch.lambdaj.Lambda;


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);
		if(this.getLocalVariable(var.getName().getName()) != null) {
			throw new SymbolTableException("Duplicate Variable Declaration of " + name);
		}
		
		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));
	}
	
	public TableEntry getLocalVariable(String name) {
		List<TableEntry> vars = Lambda.select(this.symbols.values(), Lambda.having(Lambda.on(TableEntry.class).getName(), Matchers.endsWith("." + name)));
		if(vars.size() == 1) {
			return vars.get(0);
		} else if(this.parent instanceof BlockScope) {
			return ((BlockScope) this.parent).getLocalVariable(name);
		}
		
		return null;
	}
	
	@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;
//		}
		
{'fraction_non_alphanumeric': 0.125, 'fraction_numerical': 0.001875, 'mean_word_length': 4.538062283737024, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2488548', 'n_tokens_mistral': 1061, 'n_tokens_neox': 975, 'n_words': 230}