<commit_before>package irgeneration;

import java.util.ArrayList;

import syntaxtree.*;
import symboltable.*;
import visitor.*;

public class IRGenVisitor {

  private ArrayList<IRQuadruple> _irList;
  private int _tempCount;
  private SymbolTable _symbolTable;
  private int _ifCount;
  private int _loopCount;
  private int _loopEndCount;

  public IRGenVisitor(SymbolTable symbols)
  {
    _irList = new ArrayList<IRQuadruple>();
    _tempCount = 0;
    _ifCount = 0;
    _loopCount = 0;
    _loopEndCount = 0;
    _symbolTable = symbols;
  }

  //// UTILITY FUNCTIONS /////

  private VariableSymbol getNextTemp(Type type)
  {
    VariableSymbol nextTemp = new VariableSymbol("_$t" + _tempCount++, type);
    _symbolTable.addVariable(nextTemp);
    return nextTemp;
  }

  // generate a unique method label
  public String generateMethodLabel()
  {
    MethodSymbol currMethod = _symbolTable.getCurrentMethod();
    StringBuilder label = new StringBuilder("_");
    label.append(_symbolTable.getCurrentClass().getName());
    label.append("_");
    label.append(currMethod.getName());
    for(Type t : currMethod.getFormalTypes())
    {
      label.append("_");
      label.append(t.toString());
    }

    currMethod.setLabel(label.toString());

    return label.toString();
  }

  // generate a unique else label
  public String generateElseLabel()
  {
    String result = generateMethodLabel() + "_ELSE" + _ifCount;
    return result;
  }

  public String generateEndIfLabel()
  {
    String result = generateMethodLabel() + "_ENDIF" + _ifCount++;
    return result;
  }

  // generate a unique loop label
  public String generateLoopLabel()
  {
    String result = generateMethodLabel() + "_LOOP" + _loopCount++;
    return result;
  }

  // generate a unique loop end label
  public String generateLoopEndLabel()
  {
    String result = generateMethodLabel() + "_OUT" + _loopEndCount++;
    return result;
  }

  //////// END UTILITY FUNCTIONS ///////////////////


  public SymbolInfo visit(Program n)
  {
    n.m.accept(this);
    for ( int i = 0; i < n.cl.size(); i++ ) {
      n.cl.elementAt(i).accept(this);
    }
    return null;
  }

  public SymbolInfo visit(MainClass n)
  {

    IRLabel mainLabel = new IRLabel("_main");
    _irList.add(mainLabel);
    _symbolTable.enterClass(n.i1.s);
    n.s.accept(this);
    _symbolTable.exitClass();

    // todo: system.exit
    MethodSymbol exit = new MethodSymbol("System.exit", null);
    exit.setLabel("_system_exit");
    IRCall exitCall = new IRCall(exit, 0, null);
    _irList.add(exitCall);
    return null;
  }

  public SymbolInfo visit(ClassDeclSimple n)
  {
    _symbolTable.enterClass(n.i.s);  // enter the class scope

    // add implicit 'this'
    VariableSymbol thisLocal = new VariableSymbol("this", new IdentifierType(n.i.s));
    _symbolTable.addVariable(thisLocal);

    for(int i = 0; i < n.ml.size(); i++)  // process all methods
      n.ml.elementAt(i).accept(this);

    _symbolTable.exitClass(); // exit the class scope
    return null;
  }

  public SymbolInfo visit(ClassDeclExtends n)
  {
    _symbolTable.enterClass(n.i.s);  // enter the class scope
<commit_msg>Fix bug where virtual temporaries were being added at class-level<commit_after>package irgeneration;

import java.util.ArrayList;

import syntaxtree.*;
import symboltable.*;
import visitor.*;

public class IRGenVisitor {

  private ArrayList<IRQuadruple> _irList;
  private int _tempCount;
  private SymbolTable _symbolTable;
  private int _ifCount;
  private int _loopCount;
  private int _loopEndCount;

  public IRGenVisitor(SymbolTable symbols)
  {
    _irList = new ArrayList<IRQuadruple>();
    _tempCount = 0;
    _ifCount = 0;
    _loopCount = 0;
    _loopEndCount = 0;
    _symbolTable = symbols;
  }

  //// UTILITY FUNCTIONS /////

  private VariableSymbol getNextTemp(Type type)
  {
    VariableSymbol nextTemp = new VariableSymbol("_$t" + _tempCount++, type);
    _symbolTable.addVariable(nextTemp);
    return nextTemp;
  }

  // generate a unique method label
  public String generateMethodLabel()
  {
    MethodSymbol currMethod = _symbolTable.getCurrentMethod();
    StringBuilder label = new StringBuilder("_");
    label.append(_symbolTable.getCurrentClass().getName());
    label.append("_");
    label.append(currMethod.getName());
    for(Type t : currMethod.getFormalTypes())
    {
      label.append("_");
      label.append(t.toString());
    }

    currMethod.setLabel(label.toString());

    return label.toString();
  }

  // generate a unique else label
  public String generateElseLabel()
  {
    String result = generateMethodLabel() + "_ELSE" + _ifCount;
    return result;
  }

  public String generateEndIfLabel()
  {
    String result = generateMethodLabel() + "_ENDIF" + _ifCount++;
    return result;
  }

  // generate a unique loop label
  public String generateLoopLabel()
  {
    String result = generateMethodLabel() + "_LOOP" + _loopCount++;
    return result;
  }

  // generate a unique loop end label
  public String generateLoopEndLabel()
  {
    String result = generateMethodLabel() + "_OUT" + _loopEndCount++;
    return result;
  }

  //////// END UTILITY FUNCTIONS ///////////////////


  public SymbolInfo visit(Program n)
  {
    n.m.accept(this);
    for ( int i = 0; i < n.cl.size(); i++ ) {
      n.cl.elementAt(i).accept(this);
    }
    return null;
  }

  public SymbolInfo visit(MainClass n)
  {

    IRLabel mainLabel = new IRLabel("_main");
    _irList.add(mainLabel);
    _symbolTable.enterClass(n.i1.s);
    _symbolTable.enterMethod("main");
    n.s.accept(this);
    _symbolTable.exitMethod();
    _symbolTable.exitClass();

    // todo: system.exit
    MethodSymbol exit = new MethodSymbol("System.exit", null);
    exit.setLabel("_system_exit");
    IRCall exitCall = new IRCall(exit, 0, null);
    _irList.add(exitCall);
    return null;
  }

  public SymbolInfo visit(ClassDeclSimple n)
  {
    _symbolTable.enterClass(n.i.s);  // enter the class scope

    // add implicit 'this'
    VariableSymbol thisLocal = new VariableSymbol("this", new IdentifierType(n.i.s));
    _symbolTable.addVariable(thisLocal);

    for(int i = 0; i < n.ml.size(); i++)  // process all methods
      n.ml.elementAt(i).accept(this);

    _symbolTable.exitClass(); // exit the class scope
    return null;
  }

  public SymbolInfo visit(ClassDeclExtends n)
  {
    _symbolTable.enterClass(n.i.s);  // enter the class scope

{'fraction_non_alphanumeric': 0.1256813580439184, 'fraction_numerical': 0.0024918237034729793, 'mean_word_length': 3.1728395061728394, 'pattern_counts': {'":': 0, '<': 11, '<?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': '4720142', 'n_tokens_mistral': 2049, 'n_tokens_neox': 1966, 'n_words': 483}