<commit_before>package visitor;

import syntaxtree.*;

import java.util.*;

import errorMsg.*;
import java.io.*;
import java.awt.Point;

//the purpose here is to annotate things with their offsets:
// - formal parameters, with respect to the (callee) frame
// - instance variables, with respect to their slot in the object
// - methods, with respect to their slot in the v-table
public class CG1Visitor extends ASTvisitor {
	
	// Error message object
	ErrorMsg errorMsg;
	
	// IO stream to which we will emit code
	CodeStream code;
	
	// v-table offset of next method we encounter
	int currentMethodOffset;
	
	// offset in object of next "object" instance variable we encounter
	int currentObjInstVarOffset;
	
	// offset in object of next "data" instance variable we encounter
	int currentDataInstVarOffset;
	
	// stack-offset of next formal parameter we encounter
	int currentFormalVarOffset;
	
	// stack method tables for current class and all superclasses
	Stack<Vector<String>> superclassMethodTables;
	
	// current method table
	Vector<String> currentMethodTable;
	
	public CG1Visitor(ErrorMsg e, PrintStream out) {
		errorMsg = e;
		initInstanceVars(e, out);
	}
	
	private void initInstanceVars(ErrorMsg e, PrintStream out) {
		errorMsg = e;
		currentMethodOffset = 0;
		currentObjInstVarOffset = 0;
		currentDataInstVarOffset = 0;
		code = new CodeStream(out, e);
		superclassMethodTables = new Stack<Vector<String>>();
		superclassMethodTables.addElement(new Vector<String>());
	}

	///////////////// Helper Methods /////////////////////////////

	//computes the number of total methods that cd has seen.
    //TODO: this method needs to be finished!!
	public int numMethods(ClassDecl cd) {

	    if (cd == null) {
	        return 0;
        }

        int totalMethods = 0;

        totalMethods = cd.methodTable.size();//methods on current class

		//recursively finds the number of methods for its super class
        int superClassMethods = numMethods(cd.superLink);

        //then adds in the number of methods defined in cd that do not override existing in super
        for (String name : cd.superLink.methodTable.keySet()) {
            //see if its not in our current class table
            if (!(cd.methodTable.containsKey(name))) {
                //increment the totalMethods
                totalMethods ++;
            }
        }
        return totalMethods;
    }

	//stores the method's label in the currentMethodTable vector -- based on vtable offset
	public void registerMethodInTable( MethodDecl md) {

		//may require enlarging the currentMethodTable, so that it has room for new entry
        if(md.pos < 0) {
            currentMethodTable.add(md.vtableOffset - 1, md.name + "_" + md.classDecl.name);
        }
        else{
            currentMethodTable.add(md.vtableOffset - 1, "fcn_" + md.uniqueId + "_" + md.name);
        }

        //TODO: grow the currentMethodTable?? please check this
	}

	//computes the # of words that vdl's variable declarations would place on the stack frame
	public int wordsOnStackFrame(VarDeclList vdl) {<commit_msg>fixed the register method in table problem!<commit_after>package visitor;

import syntaxtree.*;

import java.util.*;

import errorMsg.*;
import java.io.*;
import java.awt.Point;

//the purpose here is to annotate things with their offsets:
// - formal parameters, with respect to the (callee) frame
// - instance variables, with respect to their slot in the object
// - methods, with respect to their slot in the v-table
public class CG1Visitor extends ASTvisitor {

	// Error message object
	ErrorMsg errorMsg;

	// IO stream to which we will emit code
	CodeStream code;

	// v-table offset of next method we encounter
	int currentMethodOffset;

	// offset in object of next "object" instance variable we encounter
	int currentObjInstVarOffset;

	// offset in object of next "data" instance variable we encounter
	int currentDataInstVarOffset;

	// stack-offset of next formal parameter we encounter
	int currentFormalVarOffset;

	// stack method tables for current class and all superclasses
	Stack<Vector<String>> superclassMethodTables;

	// current method table
	Vector<String> currentMethodTable;

	public CG1Visitor(ErrorMsg e, PrintStream out) {
		errorMsg = e;
		initInstanceVars(e, out);
	}

	private void initInstanceVars(ErrorMsg e, PrintStream out) {
		errorMsg = e;
		currentMethodOffset = 0;
		currentObjInstVarOffset = 0;
		currentDataInstVarOffset = 0;
		code = new CodeStream(out, e);
		superclassMethodTables = new Stack<Vector<String>>();
		superclassMethodTables.addElement(new Vector<String>());
	}

	///////////////// Helper Methods /////////////////////////////

	//computes the number of total methods that cd has seen.
	public int numMethods(ClassDecl cd) {

	    if (cd == null) {
	        return 0;
        }

        int totalMethods = 0;

        totalMethods = cd.methodTable.size();//methods on current class

		//recursively finds the number of methods for its super class
        int superClassMethods = numMethods(cd.superLink);

        //then adds in the number of methods defined in cd that do not override existing in super
        for (String name : cd.superLink.methodTable.keySet()) {
            //see if its not in our current class table
            if (!(cd.methodTable.containsKey(name))) {
                //increment the totalMethods
                totalMethods ++;
            }
        }
        return totalMethods;
    }

	//stores the method's label in the currentMethodTable vector -- based on vtable offset
	public void registerMethodInTable( MethodDecl md) {

		//may require enlarging the currentMethodTable, so that it has room for new entry
        if(md.pos < 0) {
            currentMethodTable.add(md.vtableOffset - 1, md.name + "_" + md.classDecl.name);
			//label = md.name + "_" + md.classDecl.name
        }
        else{
            currentMethodTable.add(md.vtableOffset - 1, "fcn_" + md.uniqueId + "_" + md.name);
			//label = "fcn_" + md.uniqueId + "_" + md.name
        }

        if (md.superMethod != null) { //grow the table correctly if our super method is null!
			md.vtableOffset = md.superMethod.vtableOffset;
			currentMethodTable.set(md.vtableOffset, label); //where label is the class table names above
		} else {
			md.vtableOffset = currentMethodTable.size() - 1;
			currentMethodTable.add(label)
		}
	}

	//computes the # of words that vdl's variable declarations would place on the stack frame
	public int wordsOnStackFrame(VarDeclList vdl) {
{'fraction_non_alphanumeric': 0.09828009828009827, 'fraction_numerical': 0.0032248157248157246, 'mean_word_length': 2.8675771971496435, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, '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': '6706288', 'n_tokens_mistral': 1892, 'n_tokens_neox': 1757, 'n_words': 655}