<commit_before>tree grammar Templater;

options {
  language = Java;
  output = template;
  tokenVocab = vCalc;
  ASTLabelType = CommonTree;
}

@header {
	import helpers.*;
	
	import java.util.ArrayList;
	import java.util.HashMap;
	import java.util.List;
	import java.util.Map;
}

@members {	
  SymbolTable symtab = new SymbolTable();
  Scope currentScope = symtab.globals;
  
  InitContainer container = new InitContainer();
  
  int ifCounter = 0;
  int loopCounter = 0;
  int rangeCounter = 0;
  int counter = 0;
  
  Type intType = new BuiltInTypeSymbol("int");
  Type vecType = new BuiltInTypeSymbol("vector");
  
  public Type exprType(Type lhs, Type rhs) {
     if (lhs.equals(vecType) || rhs.equals(vecType)) {
       return vecType;
     } else {
       return intType;
     }
  }
  
  public String resolveVar(String v) {
    Symbol symbol = currentScope.resolve(v);
    String scope = symbol.scope.getScopeName();
    if (scope.equals("global")) {
      return "@" + v;
    } else {
      return "\%" + v;
    }
  }
}

program
  : (decl+=declaration)*
    (stat+=statement)*
    -> program(container = {container}, decl = {$decl}, stat = {$stat})
  ;
  
declaration
  : ^(DECL type VARNUM exp=expression)
    {
      Type type = $type.tsym;
      VarSymbol S = new VarSymbol($VARNUM.text, type);
      currentScope.define(S);
      Type exprType = $exp.tsym;
      
  	  if (!exprType.equals(type)) {
  	    throw new RuntimeException("Type Check error.");	
  	  }
  	  
      if (type.equals(intType)) {
        container.inits.put($VARNUM.text, $exp.st);
        container.intNames.add($VARNUM.text);
        container.counters.put($VARNUM.text, new Integer($exp.c));
      } else if (type.equals(vecType)) {
        container.inits.put($VARNUM.text, $exp.st);
        container.vecNames.add($VARNUM.text);
        container.counters.put($VARNUM.text, new Integer($exp.c));
      } else {
        throw new RuntimeException("Invalid type " + type.getName());
      }
    }
  	-> {type.equals(intType)}?  declInt(name = {$VARNUM.text})
  	// Add expression template to intInits
  	-> declVec(name = {$VARNUM.text})
  	// Add expression template to vecInits
  ;
  

statement
  : ^(IFSTAT exp=expression 
    {
      if (!($exp.tsym.equals(intType))) {
        throw new RuntimeException("Error: statement conditional must be an integer.");
      }
      counter++;
      int localCounter = counter;
      ifCounter++;
      int localIfCounter = ifCounter;
    }
     b=block) 
    -> if(counter = {localCounter}, if_counter = {localIfCounter}, expr_counter = {$exp.c}, expr={$exp.st}, b={$b.st})
  | ^(LOOPSTAT exp=expression
    {
      if (!($exp.tsym.equals(intType))) {
        throw new RuntimeException("Error: statement conditional must be an integer.");
      }
      counter++;
      int localCounter = counter;
      loopCounter++;
      int localLoopCounter = loopCounter;
    }
     b=block) 
    -> loop(counter = {localCounter}, loop_counter = {localLoopCounter}, expr_counter = {$exp.c}, expr={$exp.st}, b={$b.st})
  | ^(PRINTSTAT exp=expression)
    {
      counter++;
    }
    -> {$exp.tsym.equals(vecType)}? printVec(counter = {counter}, expr_counter = {$exp.c}, expr = {$exp.st})
    -> printInt(counter = {counter}, expr_counter = {$exp.c}, expr = {$exp.st})
  | ^(ASSIGN VARNUM exp=expression)
    {
      Symbol s = currentScope.resolve($VARNUM.text);
      Type stype = s.getType();
      Type exptype = $exp.tsym;
      if (!(stype.equals(exptype))) {
        throw new RuntimeException("Error: type mismatch");
      }
      String var = resolveVar($VARNUM.text);
    }
    -> {$exp.tsym.equals(vecType)}? assignVec(name = {var}, expr_counter = {$exp.c}, expr = {$exp.st})
    -> assignInt(name = {var}, expr_counter = {$exp.c}, expr = {$exp.st})
  ;
  
block
  : ^(BLOCK s+=statement*)
    -> block(s = {$s})
  ;

expression returns [int c, Type tsym]
  : ^('==' lhs=expression rhs=expression)
    {
      counter++;
      $c = counter;
      $tsym = exprType($lhs.tsym, $rhs.tsym);
    }
    -> {$lhs.tsym.equals(vecType) && $rhs.tsym.equals(vecType)}? eqVecVec(counter = {counter}, lhs_counter = {$lhs.c}, lhs = {$lhs.st}, rhs_counter = {$rhs.c}, rhs = {$rhs.st})
    -> {$lhs.tsym.equals(vecType)}? eqVecInt(counter = {counter}, lhs_counter = {$lhs.c}, lhs = {$lhs.st}, rhs_counter = {$rhs.c}, rhs = {$rhs.st})
    -> {$rhs.tsym.equals(vecType)}? eqIntVec(counter = {counter}, lhs_counter = {$lhs.c}, lhs = {$lhs.st}, rhs_counter = {$rhs.c}, rhs = {$rhs.st})
    -> eqIntInt(c1 = {counter - 1}, c2 = {counter}, lhs_counter = {$lhs.c}, lhs = {$lhs.st}, rhs_counter = {$rhs.c}, rhs = {$rhs.st})
  | ^('!=' lhs=expression rhs=expression)
{
      counter++;
      $c = counter;
      $tsym = exprType($lhs.tsym, $rhs.tsym);
    }
    -> {$lhs.tsym.equals(vecType) && $rhs.tsym.equals(vecType)}? neVecVec(counter = {counter}, lhs_counter = {$lhs.c}, lhs = {$lhs.st}, rhs_counter = {$rhs.c}, rhs = {$rhs.st})
    -> {$lhs.tsym.equals(vecType)}? neVecInt(counter = {counter}, lhs_counter = {$lhs.c}, lhs = {$lhs.st}, rhs_counter = {$rhs.c}, rhs = {$rhs.st})
    -> {$rhs.tsym.equals(vecType)}? neIntVec(counter = {counter}, lhs_counter = {$lhs.c}, lhs = {$lhs.st}, rhs_counter = {$rhs.c}, rhs = {$rhs.st})
    -> neIntInt(c1 = {counter - 1}, c2 = {counter}, lhs_counter = {$lhs.c}, lhs = {$lhs.st}, rhs_counter = {$rhs.c}, rhs = {$rhs.st})
  | ^('<' lhs=expression rhs=expression)
{
      counter++;
      $c = counter;
      $tsym = exprType($lhs.tsym, $rhs.tsym);
    }
    -> {$lhs.tsym.equals(vecType) && $rhs.tsym.equals(vecType)}? ltVecVec(counter = {counter}, lhs_counter = {$lhs.c}, lhs = {$lhs.st}, rhs_counter = {$rhs.c}, rhs = {$rhs.st})
    -> {$lhs.tsym.equals(vecType)}? ltVecInt(counter = {counter}, lhs_counter = {$lhs.c}, lhs = {$lhs.st}, rhs_counter = {$rhs.c}, rhs = {$rhs.st})
    -> {$rhs.tsym.equals(vecType)}? ltIntVec(counter = {counter}, lhs_counter = {$lhs.c}, lhs = {$lhs.st}, rhs_counter = {$rhs.c}, rhs = {$rhs.st})
    -> ltIntInt(c1 = {counter - 1}, c2 = {counter}, lhs_counter = {$lhs.c}, lhs = {$lhs.st}, rhs_counter = {$rhs.c}, rhs = {$rhs.st})
  | ^('>' lhs=expression rhs=expression)
{
      counter++;
      $c = counter;
      $tsym = exprType($lhs.tsym, $rhs.tsym);
    }
    -> {$lhs.tsym.equals(vecType) && $rhs.tsym.equals(vecType)}? gtVecVec(counter = {counter}, lhs_counter = {$lhs.c}, lhs = {$lhs.st}, rhs_counter = {$rhs.c}, rhs = {$rhs.st})
    -> {$lhs.tsym.equals(vecType)}? gtVecInt(counter = {counter}, lhs_counter = {$lhs.c}, lhs = {$lhs.st}, rhs_counter = {$rhs.c}, rhs = {$rhs.st})
    -> {$rhs.tsym.equals(vecType)}? gtIntVec(counter = {counter}, lhs_counter = {$lhs.c}, lhs = {$lhs.st}, rhs_counter = {$rhs.c}, rhs = {$rhs.st})
    -> gtIntInt(c1 = {counter - 1}, c2 = {counter}, lhs_counter = {$lhs.c}, lhs = {$lhs.st}, rhs_counter = {$rhs.c}, rhs = {$rhs.st})
  | ^('+' lhs=expression rhs=expression)
    {
      counter++;
      $c = counter;
      $tsym = exprType($lhs.tsym, $rhs.tsym);
    }
    -> {$lhs.tsym.equals(vecType) && $rhs.tsym.equals(vecType)}? addVecVec(counter = {counter}, lhs_counter = {$lhs.c}, lhs = {$lhs.st}, rhs_counter = {$rhs.c}, rhs = {$rhs.st})
    -> {$lhs.tsym.equals(vecType)}? addVecInt(counter = {counter}, lhs_counter = {$lhs.c}, lhs = {$lhs.st}, rhs_counter = {$rhs.c}, rhs = {$rhs.st})<commit_msg>Fixed incorrect counter being passed to int comparisons<commit_after>tree grammar Templater;

options {
  language = Java;
  output = template;
  tokenVocab = vCalc;
  ASTLabelType = CommonTree;
}

@header {
	import helpers.*;
	
	import java.util.ArrayList;
	import java.util.HashMap;
	import java.util.List;
	import java.util.Map;
}

@members {	
  SymbolTable symtab = new SymbolTable();
  Scope currentScope = symtab.globals;
  
  InitContainer container = new InitContainer();
  
  int ifCounter = 0;
  int loopCounter = 0;
  int rangeCounter = 0;
  int counter = 0;
  
  Type intType = new BuiltInTypeSymbol("int");
  Type vecType = new BuiltInTypeSymbol("vector");
  
  public Type exprType(Type lhs, Type rhs) {
     if (lhs.equals(vecType) || rhs.equals(vecType)) {
       return vecType;
     } else {
       return intType;
     }
  }
  
  public String resolveVar(String v) {
    Symbol symbol = currentScope.resolve(v);
    String scope = symbol.scope.getScopeName();
    if (scope.equals("global")) {
      return "@" + v;
    } else {
      return "\%" + v;
    }
  }
}

program
  : (decl+=declaration)*
    (stat+=statement)*
    -> program(container = {container}, decl = {$decl}, stat = {$stat})
  ;
  
declaration
  : ^(DECL type VARNUM exp=expression)
    {
      Type type = $type.tsym;
      VarSymbol S = new VarSymbol($VARNUM.text, type);
      currentScope.define(S);
      Type exprType = $exp.tsym;
      
  	  if (!exprType.equals(type)) {
  	    throw new RuntimeException("Type Check error.");	
  	  }
  	  
      if (type.equals(intType)) {
        container.inits.put($VARNUM.text, $exp.st);
        container.intNames.add($VARNUM.text);
        container.counters.put($VARNUM.text, new Integer($exp.c));
      } else if (type.equals(vecType)) {
        container.inits.put($VARNUM.text, $exp.st);
        container.vecNames.add($VARNUM.text);
        container.counters.put($VARNUM.text, new Integer($exp.c));
      } else {
        throw new RuntimeException("Invalid type " + type.getName());
      }
    }
  	-> {type.equals(intType)}?  declInt(name = {$VARNUM.text})
  	// Add expression template to intInits
  	-> declVec(name = {$VARNUM.text})
  	// Add expression template to vecInits
  ;
  

statement
  : ^(IFSTAT exp=expression 
    {
      if (!($exp.tsym.equals(intType))) {
        throw new RuntimeException("Error: statement conditional must be an integer.");
      }
      counter++;
      int localCounter = counter;
      ifCounter++;
      int localIfCounter = ifCounter;
    }
     b=block) 
    -> if(counter = {localCounter}, if_counter = {localIfCounter}, expr_counter = {$exp.c}, expr={$exp.st}, b={$b.st})
  | ^(LOOPSTAT exp=expression
    {
      if (!($exp.tsym.equals(intType))) {
        throw new RuntimeException("Error: statement conditional must be an integer.");
      }
      counter++;
      int localCounter = counter;
      loopCounter++;
      int localLoopCounter = loopCounter;
    }
     b=block) 
    -> loop(counter = {localCounter}, loop_counter = {localLoopCounter}, expr_counter = {$exp.c}, expr={$exp.st}, b={$b.st})
  | ^(PRINTSTAT exp=expression)
    {
      counter++;
    }
    -> {$exp.tsym.equals(vecType)}? printVec(counter = {counter}, expr_counter = {$exp.c}, expr = {$exp.st})
    -> printInt(counter = {counter}, expr_counter = {$exp.c}, expr = {$exp.st})
  | ^(ASSIGN VARNUM exp=expression)
    {
      Symbol s = currentScope.resolve($VARNUM.text);
      Type stype = s.getType();
      Type exptype = $exp.tsym;
      if (!(stype.equals(exptype))) {
        throw new RuntimeException("Error: type mismatch");
      }
      String var = resolveVar($VARNUM.text);
    }
    -> {$exp.tsym.equals(vecType)}? assignVec(name = {var}, expr_counter = {$exp.c}, expr = {$exp.st})
    -> assignInt(name = {var}, expr_counter = {$exp.c}, expr = {$exp.st})
  ;
  
block
  : ^(BLOCK s+=statement*)
    -> block(s = {$s})
  ;

expression returns [int c, Type tsym]
  : ^('==' lhs=expression rhs=expression)
    {
      counter++;
      $c = counter;
      $tsym = exprType($lhs.tsym, $rhs.tsym);
    }
    -> {$lhs.tsym.equals(vecType) && $rhs.tsym.equals(vecType)}? eqVecVec(counter = {counter}, lhs_counter = {$lhs.c}, lhs = {$lhs.st}, rhs_counter = {$rhs.c}, rhs = {$rhs.st})
    -> {$lhs.tsym.equals(vecType)}? eqVecInt(counter = {counter}, lhs_counter = {$lhs.c}, lhs = {$lhs.st}, rhs_counter = {$rhs.c}, rhs = {$rhs.st})
    -> {$rhs.tsym.equals(vecType)}? eqIntVec(counter = {counter}, lhs_counter = {$lhs.c}, lhs = {$lhs.st}, rhs_counter = {$rhs.c}, rhs = {$rhs.st})
    -> eqIntInt(counter = {counter}, lhs_counter = {$lhs.c}, lhs = {$lhs.st}, rhs_counter = {$rhs.c}, rhs = {$rhs.st})
  | ^('!=' lhs=expression rhs=expression)
{
      counter++;
      $c = counter;
      $tsym = exprType($lhs.tsym, $rhs.tsym);
    }
    -> {$lhs.tsym.equals(vecType) && $rhs.tsym.equals(vecType)}? neVecVec(counter = {counter}, lhs_counter = {$lhs.c}, lhs = {$lhs.st}, rhs_counter = {$rhs.c}, rhs = {$rhs.st})
    -> {$lhs.tsym.equals(vecType)}? neVecInt(counter = {counter}, lhs_counter = {$lhs.c}, lhs = {$lhs.st}, rhs_counter = {$rhs.c}, rhs = {$rhs.st})
    -> {$rhs.tsym.equals(vecType)}? neIntVec(counter = {counter}, lhs_counter = {$lhs.c}, lhs = {$lhs.st}, rhs_counter = {$rhs.c}, rhs = {$rhs.st})
    -> neIntInt(counter = {counter}, lhs_counter = {$lhs.c}, lhs = {$lhs.st}, rhs_counter = {$rhs.c}, rhs = {$rhs.st})
  | ^('<' lhs=expression rhs=expression)
{
      counter++;
      $c = counter;
      $tsym = exprType($lhs.tsym, $rhs.tsym);
    }
    -> {$lhs.tsym.equals(vecType) && $rhs.tsym.equals(vecType)}? ltVecVec(counter = {counter}, lhs_counter = {$lhs.c}, lhs = {$lhs.st}, rhs_counter = {$rhs.c}, rhs = {$rhs.st})
    -> {$lhs.tsym.equals(vecType)}? ltVecInt(counter = {counter}, lhs_counter = {$lhs.c}, lhs = {$lhs.st}, rhs_counter = {$rhs.c}, rhs = {$rhs.st})
    -> {$rhs.tsym.equals(vecType)}? ltIntVec(counter = {counter}, lhs_counter = {$lhs.c}, lhs = {$lhs.st}, rhs_counter = {$rhs.c}, rhs = {$rhs.st})
    -> ltIntInt(counter = {counter}, lhs_counter = {$lhs.c}, lhs = {$lhs.st}, rhs_counter = {$rhs.c}, rhs = {$rhs.st})
  | ^('>' lhs=expression rhs=expression)
{
      counter++;
      $c = counter;
      $tsym = exprType($lhs.tsym, $rhs.tsym);
    }
    -> {$lhs.tsym.equals(vecType) && $rhs.tsym.equals(vecType)}? gtVecVec(counter = {counter}, lhs_counter = {$lhs.c}, lhs = {$lhs.st}, rhs_counter = {$rhs.c}, rhs = {$rhs.st})
    -> {$lhs.tsym.equals(vecType)}? gtVecInt(counter = {counter}, lhs_counter = {$lhs.c}, lhs = {$lhs.st}, rhs_counter = {$rhs.c}, rhs = {$rhs.st})
    -> {$rhs.tsym.equals(vecType)}? gtIntVec(counter = {counter}, lhs_counter = {$lhs.c}, lhs = {$lhs.st}, rhs_counter = {$rhs.c}, rhs = {$rhs.st})
    -> gtIntInt(counter = {counter}, lhs_counter = {$lhs.c}, lhs = {$lhs.st}, rhs_counter = {$rhs.c}, rhs = {$rhs.st})
  | ^('+' lhs=expression rhs=expression)
    {
      counter++;
      $c = counter;
      $tsym = exprType($lhs.tsym, $rhs.tsym);
    }
    -> {$lhs.tsym.equals(vecType) && $rhs.tsym.equals(vecType)}? addVecVec(counter = {counter}, lhs_counter = {$lhs.c}, lhs = {$lhs.st}, rhs_counter = {$rhs.c}, rhs = {$rhs.st})
    -> {$lhs.tsym.equals(vecType)}? addVecInt(counter = {counter}, lhs_counter = {$lhs.c}, lhs = {$lhs.st}, rhs_counter = {$rhs.c}, rhs = {$rhs.st})
{'fraction_non_alphanumeric': 0.19896146488111505, 'fraction_numerical': 0.001366493577480186, 'mean_word_length': 3.1535187287173665, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 61, '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': '3038995', 'n_tokens_mistral': 5367, 'n_tokens_neox': 5455, 'n_words': 1056}