package ca.uwaterloo.joos.ast.visitor;

import java.util.List;

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;
	private int blocklevel;
	public BlockVisitor(SymbolTable ist) {
		super(ist);
		
	}
	
	
	public boolean visit(ASTNode node) throws ChildTypeUnmatchException, Exception{

		if (node instanceof LocalVariableDeclaration){
			
			//TODO Check for multiple definitions
			//if name is not already in view
			LocalVariableDeclaration LNode = (LocalVariableDeclaration) node;
			
			
			if (st.hasField(st.getName() + "." + LNode.getName().getName())) {
				System.err.println("Overlapping Declarations Exit 42");
				System.exit(42);
			}
			level++;
			st.addDeclaration(st.getName()+"."+LNode.getName().getName(), node, level);			
			return false;
		}
		
		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++;
				nst.addScope();//Add new symbol table to the global scope hash
				List<String> tmp = nst.appendScope(st, blocks, this.level);				//Add the LOCAL SCOPE to the nested block
				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){
		
	}

}

@@ -366,16 +366,17 @@
 sVisitor
+ 
 %7B%0A%09priva
@@ -424,32 +424,8 @@
 -1;%0A
-%09private int blocklevel;
 %0A%09pu
@@ -475,25 +475,20 @@
 r(ist);%0A
-%09%09
 %0A%09%7D%0A
-%09%0A%09
 %0A%09public
@@ -559,16 +559,17 @@
 xception
+ 
 %7B%0A%0A%09%09if 
@@ -614,19 +614,18 @@
 ion)
+ 
 %7B%0A
-%09%09%09
 %0A%09%09%09//
+ 
 TODO
@@ -661,16 +661,17 @@
 ns%0A%09%09%09//
+ 
 if name 
@@ -762,23 +762,16 @@
 ) node;%0A
-%09%09%09%0A%09%09%09
 %0A%09%09%09if (
@@ -964,21 +964,25 @@
 etName()
-+%22.%22+
+ + %22.%22 + 
 LNode.ge
@@ -1013,19 +1013,16 @@
  level);
-%09%09%09
 %0A%09%09%09retu
@@ -1034,22 +1034,22 @@
 lse;%0A%09%09%7D
-%0A%09%09%0A%09%09
+ else 
 if (node
@@ -1066,16 +1066,17 @@
 f Block)
+ 
 %7B%0A%09%09%09if 
@@ -1091,18 +1091,20 @@
 == -1) %7B
+ 
 //
+ 
 Then thi
@@ -1155,21 +1155,23 @@
 %09%09%09%7D
-%0A%0A%09%09%09
+ 
 else %7B
+%0A%09%09%09%09
 //
+ 
 Leve
@@ -1210,16 +1210,17 @@
 k%0A%09%09%09%09//
+ 
 TODO%0A%09%09%09
@@ -1222,17 +1222,17 @@
 O%0A%09%09%09%09//
-%09
+ 
 Make a n
@@ -1246,17 +1246,17 @@
 e%0A%09%09%09%09//
-%09
+ 
 Add it t
@@ -1267,17 +1267,16 @@
 e Scopes
- 
 %0A%09%09%09%09Sym
@@ -1331,25 +1331,11 @@
 %09%09%09%09
-nst.addScope();
 //
+ 
 Add 
@@ -1384,73 +1384,31 @@
 %09%09%09%09
-List%3CString%3E tmp = 
 nst.a
-ppen
+d
 dScope(
-st, blocks, this.level
 );
+%0A
 %09%09%09%09//
+ 
 Add 
@@ -1439,24 +1439,88 @@
 ested block%0A
+%09%09%09%09List%3CString%3E tmp = nst.appendScope(st, blocks, this.level);%0A
 %09%09%09%09nst.setN
@@ -1539,20 +1539,26 @@
 me()
-+%22.%22+
+ + %22.%22 + 
 blocks
-+
+ + 
 %22Blo
@@ -1665,13 +1665,8 @@
 se;%0A
-%09%09%09%09%0A
 %09%09%09%7D
@@ -1670,18 +1670,16 @@
 %09%09%7D%0A%09%09%7D%0A
-%09%09
 %0A%09%09retur
@@ -1689,21 +1689,16 @@
 rue;%0A%09%7D%0A
-%09%0A%09%0A%09
 %0A%09@Overr
@@ -1741,17 +1741,15 @@
 ode)
+ 
 %7B%0A
-%09%09
 %0A%09%7D%0A
-%09
 %0A%09@O
@@ -1791,19 +1791,18 @@
 de node)
+ 
 %7B%0A
-%09%09
 %0A%09%7D%0A%0A%7D%0A

Clean up BlockVisitor