<commit_before>package sizzle.compiler;

import java.io.IOException;
import java.util.*;
import java.util.Map.Entry;
import java.util.regex.Pattern;

import org.antlr.stringtemplate.StringTemplate;
import org.antlr.stringtemplate.StringTemplateGroup;

import sizzle.parser.syntaxtree.*;
import sizzle.parser.visitor.GJDepthFirst;
import sizzle.types.*;

class TableDescription {
	private String aggregator;
	private SizzleType type;
	private List<String> parameters;

	public TableDescription(final String aggregator, final SizzleType type) {
		this(aggregator, type, null);
	}

	public TableDescription(final String aggregator, final SizzleType type, final List<String> parameters) {
		this.aggregator = aggregator;
		this.type = type;
		this.parameters = parameters;
	}

	/**
	 * @return the name
	 */
	public String getAggregator() {
		return this.aggregator;
	}

	/**
	 * @return the parameters
	 */
	public List<String> getParameters() {
		return this.parameters;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setAggregator(final String aggregator) {
		this.aggregator = aggregator;
	}

	/**
	 * @param parameters
	 *            the parameters to set
	 */
	public void setParameters(final List<String> parameters) {
		this.parameters = parameters;
	}

	/**
	 * @return the types
	 */
	public SizzleType getType() {
		return this.type;
	}

	/**
	 * @param types
	 *            the types to set
	 */
	public void setTypes(final SizzleType type) {
		this.type = type;
	}
}

public class CodeGeneratingVisitor extends GJDepthFirst<String, SymbolTable> {
	private final TypeCheckingVisitor typechecker;
	private final NameFindingVisitor namefinder;
	private final IndexeeFindingVisitor indexeefinder;
	private final StaticDeclarationCodeGeneratingVisitor staticdeclarator;
	private final StaticInitializationCodeGeneratingVisitor staticinitializer;

	private final HashMap<String, TableDescription> tables;

	private final String name;
	public final StringTemplateGroup stg;

	private String skipIndex = "";
	private boolean abortGeneration = false;

	public CodeGeneratingVisitor(final TypeCheckingVisitor typechecker, final String name, final StringTemplateGroup stg) throws IOException {
		this.typechecker = typechecker;
		this.namefinder = new NameFindingVisitor();
		this.indexeefinder = new IndexeeFindingVisitor(this, namefinder);
		this.staticdeclarator = new StaticDeclarationCodeGeneratingVisitor(this);
		this.staticinitializer = new StaticInitializationCodeGeneratingVisitor(this);

		this.tables = new HashMap<String, TableDescription>();
		this.tables.put("stdout", new TableDescription("stdout", new SizzleString()));
		this.tables.put("stderr", new TableDescription("stderr", new SizzleString()));

		this.name = name;
		this.stg = stg;
	}

	public void setSkipIndex(final String skipIndex)
	{
		this.skipIndex = skipIndex;
	}

	@Override
	public String visit(final Start n, final SymbolTable argu) {
		return n.f0.accept(this, argu);
	}

	@Override
	public String visit(final Program n, final SymbolTable argu) {
		final StringTemplate st = this.stg.getInstanceOf("Program");

		st.setAttribute("name", this.name);

		st.setAttribute("staticDeclarations", this.staticdeclarator.visit(n, argu));
		st.setAttribute("staticStatements", this.staticinitializer.visit(n, argu));

		final List<String> statements = new ArrayList<String>();
		for (final Node node : n.f0.nodes) {
			final String statement = node.accept(this, argu);
			if (statement != null)
				statements.add(statement);
		}
		st.setAttribute("statements", statements);

		final List<String> tables = new ArrayList<String>();
		for (final Entry<String, TableDescription> entry : this.tables.entrySet()) {
			final String id = entry.getKey();
			final TableDescription description = entry.getValue();
			final String parameters = description.getParameters() == null ? "" : description.getParameters().get(0);
			final SizzleType type = description.getType();

			final StringBuilder src = new StringBuilder();
			for (final Class<?> c : argu.getAggregators(description.getAggregator(), type))
				src.append(", new " + c.getCanonicalName() + "(" + parameters + ")");

			tables.add("this.tables.put(\"" + id + "\", new sizzle.aggregators.Table(" + src.toString().substring(2) + "));");
		}

		st.setAttribute("tables", tables);

		return st.toString();
	}

	@Override
	public String visit(final Declaration n, final SymbolTable argu) {
		return n.f0.choice.accept(this, argu);
	}

	@Override
	public String visit(final TypeDecl n, final SymbolTable argu) {
		throw new RuntimeException("unimplemented");
	}

	@Override
	public String visit(final StaticVarDecl n, final SymbolTable argu) {
		// this is handled by the static code generator
		return null;
	}

	@Override
	public String visit(final VarDecl n, final SymbolTable argu) {
		final SizzleType type = argu.get(n.f0.f0.tokenImage);

		final SizzleType lhsType;
		if (n.f2.present()) {
			argu.setId(n.f0.f0.tokenImage);
			lhsType = n.f2.node.accept(typechecker, argu);
			argu.setId(null);
		} else {
			lhsType = null;
		}

		if (type instanceof SizzleTable)
			return null;

		final StringTemplate idSt = this.stg.getInstanceOf("Identifier");

		idSt.setAttribute("id", n.f0.f0.tokenImage);<commit_msg>last commit broke output variables<commit_after>package sizzle.compiler;

import java.io.IOException;
import java.util.*;
import java.util.Map.Entry;
import java.util.regex.Pattern;

import org.antlr.stringtemplate.StringTemplate;
import org.antlr.stringtemplate.StringTemplateGroup;

import sizzle.parser.syntaxtree.*;
import sizzle.parser.visitor.GJDepthFirst;
import sizzle.types.*;

class TableDescription {
	private String aggregator;
	private SizzleType type;
	private List<String> parameters;

	public TableDescription(final String aggregator, final SizzleType type) {
		this(aggregator, type, null);
	}

	public TableDescription(final String aggregator, final SizzleType type, final List<String> parameters) {
		this.aggregator = aggregator;
		this.type = type;
		this.parameters = parameters;
	}

	/**
	 * @return the name
	 */
	public String getAggregator() {
		return this.aggregator;
	}

	/**
	 * @return the parameters
	 */
	public List<String> getParameters() {
		return this.parameters;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setAggregator(final String aggregator) {
		this.aggregator = aggregator;
	}

	/**
	 * @param parameters
	 *            the parameters to set
	 */
	public void setParameters(final List<String> parameters) {
		this.parameters = parameters;
	}

	/**
	 * @return the types
	 */
	public SizzleType getType() {
		return this.type;
	}

	/**
	 * @param types
	 *            the types to set
	 */
	public void setTypes(final SizzleType type) {
		this.type = type;
	}
}

public class CodeGeneratingVisitor extends GJDepthFirst<String, SymbolTable> {
	private final TypeCheckingVisitor typechecker;
	private final NameFindingVisitor namefinder;
	private final IndexeeFindingVisitor indexeefinder;
	private final StaticDeclarationCodeGeneratingVisitor staticdeclarator;
	private final StaticInitializationCodeGeneratingVisitor staticinitializer;

	private final HashMap<String, TableDescription> tables;

	private final String name;
	public final StringTemplateGroup stg;

	private String skipIndex = "";
	private boolean abortGeneration = false;

	public CodeGeneratingVisitor(final TypeCheckingVisitor typechecker, final String name, final StringTemplateGroup stg) throws IOException {
		this.typechecker = typechecker;
		this.namefinder = new NameFindingVisitor();
		this.indexeefinder = new IndexeeFindingVisitor(this, namefinder);
		this.staticdeclarator = new StaticDeclarationCodeGeneratingVisitor(this);
		this.staticinitializer = new StaticInitializationCodeGeneratingVisitor(this);

		this.tables = new HashMap<String, TableDescription>();
		this.tables.put("stdout", new TableDescription("stdout", new SizzleString()));
		this.tables.put("stderr", new TableDescription("stderr", new SizzleString()));

		this.name = name;
		this.stg = stg;
	}

	public void setSkipIndex(final String skipIndex)
	{
		this.skipIndex = skipIndex;
	}

	@Override
	public String visit(final Start n, final SymbolTable argu) {
		return n.f0.accept(this, argu);
	}

	@Override
	public String visit(final Program n, final SymbolTable argu) {
		final StringTemplate st = this.stg.getInstanceOf("Program");

		st.setAttribute("name", this.name);

		st.setAttribute("staticDeclarations", this.staticdeclarator.visit(n, argu));
		st.setAttribute("staticStatements", this.staticinitializer.visit(n, argu));

		final List<String> statements = new ArrayList<String>();
		for (final Node node : n.f0.nodes) {
			final String statement = node.accept(this, argu);
			if (statement != null)
				statements.add(statement);
		}
		st.setAttribute("statements", statements);

		final List<String> tables = new ArrayList<String>();
		for (final Entry<String, TableDescription> entry : this.tables.entrySet()) {
			final String id = entry.getKey();
			final TableDescription description = entry.getValue();
			final String parameters = description.getParameters() == null ? "" : description.getParameters().get(0);
			final SizzleType type = description.getType();

			final StringBuilder src = new StringBuilder();
			for (final Class<?> c : argu.getAggregators(description.getAggregator(), type))
				src.append(", new " + c.getCanonicalName() + "(" + parameters + ")");

			tables.add("this.tables.put(\"" + id + "\", new sizzle.aggregators.Table(" + src.toString().substring(2) + "));");
		}

		st.setAttribute("tables", tables);

		return st.toString();
	}

	@Override
	public String visit(final Declaration n, final SymbolTable argu) {
		return n.f0.choice.accept(this, argu);
	}

	@Override
	public String visit(final TypeDecl n, final SymbolTable argu) {
		throw new RuntimeException("unimplemented");
	}

	@Override
	public String visit(final StaticVarDecl n, final SymbolTable argu) {
		// this is handled by the static code generator
		return null;
	}

	@Override
	public String visit(final VarDecl n, final SymbolTable argu) {
		final SizzleType type = argu.get(n.f0.f0.tokenImage);

		final SizzleType lhsType;
		if (n.f2.present()) {
			argu.setId(n.f0.f0.tokenImage);
			lhsType = n.f2.node.accept(typechecker, argu);
			n.f2.node.accept(this, argu);
			argu.setId(null);
		} else {
			lhsType = null;
		}

		if (type instanceof SizzleTable)
			return null;

		final StringTemplate idSt = this.stg.getInstanceOf("Identifier");

		idSt.setAttribute("id", n.f0.f0.tokenImage);
{'fraction_non_alphanumeric': 0.11246458923512748, 'fraction_numerical': 0.0025495750708215297, 'mean_word_length': 4.553749344520189, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, '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': '2140799', 'n_tokens_mistral': 3415, 'n_tokens_neox': 3123, 'n_words': 817}