<commit_before>//Scratch
//TODO 
//	-JAVADOC
//	-Define Table
package ca.uwaterloo.joos.symboltable;

//Proposal
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ca.uwaterloo.joos.ast.ASTNode;
import ca.uwaterloo.joos.ast.ASTNode.ChildTypeUnmatchException;
import ca.uwaterloo.joos.ast.Modifiers.Modifier;
import ca.uwaterloo.joos.ast.decl.BodyDeclaration;
import ca.uwaterloo.joos.ast.decl.LocalVariableDeclaration;
import ca.uwaterloo.joos.ast.decl.MethodDeclaration;
import ca.uwaterloo.joos.ast.decl.ParameterDeclaration;
import ca.uwaterloo.joos.ast.decl.TypeDeclaration;
import ca.uwaterloo.joos.ast.decl.VariableDeclaration;
import ca.uwaterloo.joos.ast.expr.name.Name;
import ca.uwaterloo.joos.ast.type.ReferenceType;


public class Scope{
	/**
	 * Symbol Table
	 * 
	 * Scans an AST of a validated joos source file.
	 * The class maintains a static HashMap. After an AST scan is completed
	 * the HashMap is updated with the declarations held in the file's global
	 * namespace.
	 * 
	 */
	
	private String name = null;	// Represents the name of the current scope
	private Map<String, TableEntry> symbolTable = null;	// A map mapping identifiers to their related ASTNode
	
	//Constructs a symbol table
	//An AST is generated at this point, walk it.
	
	//First task is to build the environment
	//	-Replace each symbol declaration with a reference to the Symbol table.
	//	-Add the symbol to the table at some index and put that index into the AST
	//	-Associate each package...
	
	public Scope(String name){
		this.symbolTable = new HashMap<String, TableEntry>();
		this.name = name;
//		Scope.scopes.put(name, this);
	}
	
	public void appendScope(Scope table){
		//ONLY CALLED FROM BLOCK VISITOR
		
		for (String key : table.symbolTable.keySet()) {
			TableEntry entry = table.symbolTable.get(key);
			this.symbolTable.put(key, entry);
		}
	}
	
	public void addPublicMembers(Scope table) throws ChildTypeUnmatchException {
		for(String key: table.symbolTable.keySet()) {
			TableEntry entry = table.symbolTable.get(key);
			
			if(entry.getNode() instanceof BodyDeclaration) {
				BodyDeclaration node = (BodyDeclaration) entry.getNode();
				if(node.getModifiers().getModifiers().contains(Modifier.PUBLIC)) {
					this.symbolTable.put(key, entry);
				}
			} else if(entry.getNode() instanceof TypeDeclaration) {
				TypeDeclaration node = (TypeDeclaration) entry.getNode();
				if(node.getModifiers().getModifiers().contains(Modifier.PUBLIC)) {
					this.symbolTable.put(key, entry);
				}
			}
		}
	}
	
	public String getName(){
		return name;
	}
	
	public void addClass(String key, ASTNode node){
		TableEntry te = new TableEntry(node);
		te.setLevel(0);
		symbolTable.put(key, te);
	}
	
	public TableEntry getClass(String key){
		return symbolTable.get(key + "{}");
	}
	
	public void addDeclaration(String key, ASTNode node, int level){
		TableEntry te = new TableEntry(node);
		te.setLevel(level);
		symbolTable.put(key, te);
		
	}
	
	public String signatureOfMethod(MethodDeclaration method) throws Exception {
		String name = this.name + "." + method.getName().getName() + "(";
		for(ParameterDeclaration parameter: method.getParameters()) {
			name += parameter.getType().getIdentifier();
		}
		name += ")";
		return name;
	}

	public void addMethod(MethodDeclaration node) throws Exception{
		String name = this.signatureOfMethod(node);
		symbolTable.put(name, new TableEntry(node));<commit_msg>Add Constructor flag into method signature<commit_after>//Scratch
//TODO 
//	-JAVADOC
//	-Define Table
package ca.uwaterloo.joos.symboltable;

//Proposal
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ca.uwaterloo.joos.ast.ASTNode;
import ca.uwaterloo.joos.ast.ASTNode.ChildTypeUnmatchException;
import ca.uwaterloo.joos.ast.Modifiers.Modifier;
import ca.uwaterloo.joos.ast.decl.BodyDeclaration;
import ca.uwaterloo.joos.ast.decl.ConstructorDeclaration;
import ca.uwaterloo.joos.ast.decl.LocalVariableDeclaration;
import ca.uwaterloo.joos.ast.decl.MethodDeclaration;
import ca.uwaterloo.joos.ast.decl.ParameterDeclaration;
import ca.uwaterloo.joos.ast.decl.TypeDeclaration;
import ca.uwaterloo.joos.ast.decl.VariableDeclaration;
import ca.uwaterloo.joos.ast.expr.name.Name;
import ca.uwaterloo.joos.ast.type.ReferenceType;


public class Scope{
	/**
	 * Symbol Table
	 * 
	 * Scans an AST of a validated joos source file.
	 * The class maintains a static HashMap. After an AST scan is completed
	 * the HashMap is updated with the declarations held in the file's global
	 * namespace.
	 * 
	 */
	
	private String name = null;	// Represents the name of the current scope
	private Map<String, TableEntry> symbolTable = null;	// A map mapping identifiers to their related ASTNode
	
	//Constructs a symbol table
	//An AST is generated at this point, walk it.
	
	//First task is to build the environment
	//	-Replace each symbol declaration with a reference to the Symbol table.
	//	-Add the symbol to the table at some index and put that index into the AST
	//	-Associate each package...
	
	public Scope(String name){
		this.symbolTable = new HashMap<String, TableEntry>();
		this.name = name;
//		Scope.scopes.put(name, this);
	}
	
	public void appendScope(Scope table){
		//ONLY CALLED FROM BLOCK VISITOR
		
		for (String key : table.symbolTable.keySet()) {
			TableEntry entry = table.symbolTable.get(key);
			this.symbolTable.put(key, entry);
		}
	}
	
	public void addPublicMembers(Scope table) throws ChildTypeUnmatchException {
		for(String key: table.symbolTable.keySet()) {
			TableEntry entry = table.symbolTable.get(key);
			
			if(entry.getNode() instanceof BodyDeclaration) {
				BodyDeclaration node = (BodyDeclaration) entry.getNode();
				if(node.getModifiers().getModifiers().contains(Modifier.PUBLIC)) {
					this.symbolTable.put(key, entry);
				}
			} else if(entry.getNode() instanceof TypeDeclaration) {
				TypeDeclaration node = (TypeDeclaration) entry.getNode();
				if(node.getModifiers().getModifiers().contains(Modifier.PUBLIC)) {
					this.symbolTable.put(key, entry);
				}
			}
		}
	}
	
	public String getName(){
		return name;
	}
	
	public void addClass(String key, ASTNode node){
		TableEntry te = new TableEntry(node);
		te.setLevel(0);
		symbolTable.put(key, te);
	}
	
	public TableEntry getClass(String key){
		return symbolTable.get(key + "{}");
	}
	
	public void addDeclaration(String key, ASTNode node, int level){
		TableEntry te = new TableEntry(node);
		te.setLevel(level);
		symbolTable.put(key, te);
		
	}
	
	public String signatureOfMethod(MethodDeclaration method) throws Exception {
		String name = this.name + "." + method.getName().getName() + "(";
		if(method instanceof ConstructorDeclaration) {
			name += "C";
		}
		for(ParameterDeclaration parameter: method.getParameters()) {
			name += parameter.getType().getIdentifier();
		}
		name += ")";
		return name;
	}

	public void addMethod(MethodDeclaration node) throws Exception{
		String name = this.signatureOfMethod(node);
		symbolTable.put(name, new TableEntry(node));
{'fraction_non_alphanumeric': 0.11435353253281207, 'fraction_numerical': 0.00027925160569673273, 'mean_word_length': 4.116428571428571, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '7239731', 'n_tokens_mistral': 2443, 'n_tokens_neox': 2187, 'n_words': 579}