package ca.uwaterloo.joos.ast.visitor;

import ca.uwaterloo.joos.ast.ASTNode;
import ca.uwaterloo.joos.ast.ASTNode.ChildTypeUnmatchException;
import ca.uwaterloo.joos.ast.decl.LocalVariableDeclaration;
import ca.uwaterloo.joos.ast.statement.Block;
import ca.uwaterloo.joos.symbolTable.SymbolTable;

public class BlockVisitor extends SemanticsVisitor {
	private int level = -1;
	private int blocks = -1;

	public BlockVisitor(SymbolTable scope) {
		super();
		this.pushScope(scope);
	}

	public boolean visit(ASTNode node) throws ChildTypeUnmatchException, Exception {
		SymbolTable currentScope = this.getCurrentScope();
		if (node instanceof LocalVariableDeclaration) {
			// if name is not already in view
			LocalVariableDeclaration LNode = (LocalVariableDeclaration) node;

			if (currentScope.getVariableDecl(LNode) != null) {
				throw new Exception("Overlapping Declarations Exit 42");
			}
			level++;
			currentScope.addVariableDecl(LNode, level);
			return false;
		} else if (node instanceof Block) {
			if (blocks == -1) { // Then this is the block we are reading
				blocks++;
			} else {
//				// Level is 0 so we found a nested block
//				// TODO
//				// Make a new scope
//				// Add it to the Scopes
//				SymbolTable nst = new SymbolTable();
//				blocks++;
//				// Add new symbol table to the global scope hash
//				nst.addScope();
//				// Add the LOCAL SCOPE to the nested block
//				List<String> tmp = nst.appendScope(st, blocks, this.level);
//				nst.setName(st.getName() + "." + blocks + "Block");
//				nst.build(new BlockVisitor(nst), node);
//				nst.unAppendScope(blocks, tmp);
				return false;
			}
		}

		return true;
	}

	@Override
	public void willVisit(ASTNode node) {

	}

	@Override
	public void didVisit(ASTNode node) {

	}

}

@@ -169,21 +169,16 @@
 st.decl.
-Local
 Variable
@@ -361,18 +361,17 @@
 level = 
--1
+0
 ;%0A%09priva
@@ -386,18 +386,17 @@
 locks = 
--1
+0
 ;%0A%0A%09publ
@@ -629,21 +629,16 @@
 tanceof 
-Local
 Variable
@@ -692,21 +692,16 @@
 view%0A%09%09%09
-Local
 Variable
@@ -725,13 +725,8 @@
  = (
-Local
 Vari
@@ -873,20 +873,8 @@
 %09%09%7D%0A
-%09%09%09level++;%0A
 %09%09%09c
@@ -916,16 +916,34 @@
 level);%0A
+%09%09%09this.level++;%0A%0A
 %09%09%09retur
@@ -1010,59 +1010,23 @@
  == 
--1) %7B // Then this is the block we are reading%0A%09%09%09%09
+0)  %7B%0A%09%09%09%09this.
 bloc
@@ -1039,404 +1039,351 @@
 %09%09%09%7D
- 
+%0A%09%09%09
 else %7B%0A
-//
 %09%09%09%09
-// Level is 0 so we found a nested block%0A//%09%09%09%09// TODO%0A//%09%09%09%09// Make a new scope%0A//%09%09%09%09// Add it to the Scopes%0A//%09%09%09%09SymbolTable nst = new SymbolTable();%0A//%09%09%09%09blocks++;%0A//%09%09%09%09// Add new symbol table to the global scope hash%0A//%09%09%09%09nst.addScope();%0A//%09%09%09%09// Add the LOCAL SCOPE to the nested block%0A//%09%09%09%09List%3CString%3E tmp = nst.appendScope(st, blocks, this.level);%0A//%09%09%09%09nst.setName(st
+ASTVisitor blockVisitor = new BlockVisitor(this.getCurrentScope());%0A%09%09%09%09node.accept(blockVisitor);%0A%09%09%09%09%0A%09%09%09%09return false;%0A%09%09%09%7D%0A%09%09%7D%0A%0A%09%09return true;%0A%09%7D%0A%0A%09@Override%0A%09public void willVisit(ASTNode node) %7B%0A%09%09if (blocks %3E 0 && node instanceof Block) %7B%0A%09%09%09// Make a new symbol table which builds%0A%09%09%09String name = this.getCurrentScope()
 .get
@@ -1397,16 +1397,21 @@
 + %22.%22 + 
+this.
 blocks +
@@ -1422,138 +1422,109 @@
 ock%22
-)
 ;%0A
-//
 %09%09%09
-%09nst.build(new BlockVisitor(nst), nod
+SymbolTable scope = SymbolTable.getScope(nam
 e);%0A
-//
 %09%09%09
-%09nst.unAppendScope(blocks, tmp);%0A%09%09%09%09return false
+this.pushScope(scope);%0A%09%09%09this.blocks++
 ;%0A
-%09
 %09%09%7D%0A
-%09%09%7D%0A%0A%09%09return true;%0A
 %09%7D%0A%0A
@@ -1539,36 +1539,35 @@
 de%0A%09public void 
-will
+did
 Visit(ASTNode no
@@ -1576,62 +1576,62 @@
 ) %7B%0A
-%0A%09%7D%0A%0A%09@Override%0A%09public void didVisit(ASTNode node) %7B%0A
+%09%09if (node instanceof Block) %7B%0A%09%09%09this.popScope();%0A%09%09%7D
 %0A%09%7D%0A

Add nested block visiting