<commit_before>package codegen;

import java.util.*;

import syntaxtree.*;
import visitor.*;
import typecheck.*;

/**
 * For intermediate code generation.
 */
public class CodeGenVisitor extends GJDepthFirst<VariableLabel, CodeGenPair> {
    private boolean helperFunctionCalled = false;

    /*
     * f0 -> MainClass()
     * f1 -> ( TypeDeclaration() )*
     * f2 -> <EOF>
     */
    @Override
    public VariableLabel visit(Goal n, CodeGenPair p) {
        Translator t = p.getTranslator();

        // Generate vtable
        for (Iterator<Binder> it = p.getScope().symbolIterator(); it.hasNext(); ) {
            Binder b = it.next();

            // We don't generate class record for MainClass
            if (!(b.getType() instanceof MainClass)) {
                t.generateClassRecord(b);
                t.outputVTable(b);
                t.getOutput().writeLine();
            }
        }

        n.f0.accept(this, p);
        n.f1.accept(this, p);

        if (helperFunctionCalled) {
            t.outputHelperFunction();
            t.getOutput().writeLine();
        }
        return null;
    }

    /*
     * f0 -> "class"
     * f1 -> Identifier()
     * f2 -> "{"
     * f3 -> "public"
     * f4 -> "static"
     * f5 -> "void"
     * f6 -> "main"
     * f7 -> "("
     * f8 -> "String"
     * f9 -> "["
     * f10 -> "]"
     * f11 -> Identifier()
     * f12 -> ")"
     * f13 -> "{"
     * f14 -> ( VarDeclaration() )*
     * f15 -> ( Statement() )*
     * f16 -> "}"
     * f17 -> "}"
     */
    @Override
    public VariableLabel visit(MainClass n, CodeGenPair p) {
        Symbol id = Symbol.fromString(TypeCheckHelper.className(n));
        Binder b = p.getScope().lookup(id);
        Scope ns = b.getScope();
        Translator t = p.getTranslator();

        t.outputMethodSignature(b);
        t.getOutput().increaseIndent();
        t.getLabelManager().beginMethod();

        n.f15.accept(this, new CodeGenPair(ns, t));
        t.outputReturn();

        t.getLabelManager().endMethod();
        t.getOutput().decreaseIndent();
        t.getOutput().writeLine();
        return null;
    }

    /*
     * f0 -> "class"
     * f1 -> Identifier()
     * f2 -> "{"
     * f3 -> ( VarDeclaration() )*
     * f4 -> ( MethodDeclaration() )*
     * f5 -> "}"
     */
    @Override
    public VariableLabel visit(ClassDeclaration n, CodeGenPair p) {
        Symbol id = Symbol.fromString(TypeCheckHelper.className(n));
        Binder b = p.getScope().lookup(id);

        n.f4.accept(this, new CodeGenPair(b.getScope(), p.getTranslator()));
        return null;
    }

    /*
     * f0 -> "class"
     * f1 -> Identifier()
     * f2 -> "extends"
     * f3 -> Identifier()
     * f4 -> "{"
     * f5 -> ( VarDeclaration() )*
     * f6 -> ( MethodDeclaration() )*
     * f7 -> "}"
     */
    @Override
    public VariableLabel visit(ClassExtendsDeclaration n, CodeGenPair p) {
        Symbol id = Symbol.fromString(TypeCheckHelper.className(n));
        Binder b = p.getScope().lookup(id);

        n.f6.accept(this, new CodeGenPair(b.getScope(), p.getTranslator()));
        return null;
    }

    /*
     * f0 -> "public"
     * f1 -> Type()
     * f2 -> Identifier()
     * f3 -> "("
     * f4 -> ( FormalParameterList() )?
     * f5 -> ")"
     * f6 -> "{"
     * f7 -> ( VarDeclaration() )*
     * f8 -> ( Statement() )*
     * f9 -> "return"
     * f10 -> Expression()
     * f11 -> ";"
     * f12 -> "}"
     */
    @Override
    public VariableLabel visit(MethodDeclaration n, CodeGenPair p) {
        Symbol id = Symbol.fromString(TypeCheckHelper.methodName(n));
        Binder b = p.getScope().lookup(id);
        Scope ns = b.getScope();
        Translator t = p.getTranslator();

        t.outputMethodSignature(b);
        t.getOutput().increaseIndent();
        t.getLabelManager().beginMethod();

        CodeGenPair np = new CodeGenPair(ns, t);
        n.f8.accept(this, np);
        VariableLabel ret = CodeGenHelper.retrieveDerefOrFuncCall(n.f10.accept(this, np), np);
        t.outputReturn(ret);

        t.getLabelManager().endMethod();
        t.getOutput().decreaseIndent();
        t.getOutput().writeLine();
        return null;
    }

    /*
     * f0 -> Identifier()
     * f1 -> "="<commit_msg>Fix an extra line in the output.<commit_after>package codegen;

import java.util.*;

import syntaxtree.*;
import visitor.*;
import typecheck.*;

/**
 * For intermediate code generation.
 */
public class CodeGenVisitor extends GJDepthFirst<VariableLabel, CodeGenPair> {
    private boolean helperFunctionCalled = false;

    /*
     * f0 -> MainClass()
     * f1 -> ( TypeDeclaration() )*
     * f2 -> <EOF>
     */
    @Override
    public VariableLabel visit(Goal n, CodeGenPair p) {
        Translator t = p.getTranslator();

        // Generate vtable
        for (Iterator<Binder> it = p.getScope().symbolIterator(); it.hasNext(); ) {
            Binder b = it.next();

            // We don't generate class record for MainClass
            if (!(b.getType() instanceof MainClass)) {
                t.generateClassRecord(b);
                t.outputVTable(b);
                t.getOutput().writeLine();
            }
        }

        n.f0.accept(this, p);
        n.f1.accept(this, p);

        if (helperFunctionCalled) {
            t.getOutput().writeLine();
            t.outputHelperFunction();
        }
        return null;
    }

    /*
     * f0 -> "class"
     * f1 -> Identifier()
     * f2 -> "{"
     * f3 -> "public"
     * f4 -> "static"
     * f5 -> "void"
     * f6 -> "main"
     * f7 -> "("
     * f8 -> "String"
     * f9 -> "["
     * f10 -> "]"
     * f11 -> Identifier()
     * f12 -> ")"
     * f13 -> "{"
     * f14 -> ( VarDeclaration() )*
     * f15 -> ( Statement() )*
     * f16 -> "}"
     * f17 -> "}"
     */
    @Override
    public VariableLabel visit(MainClass n, CodeGenPair p) {
        Symbol id = Symbol.fromString(TypeCheckHelper.className(n));
        Binder b = p.getScope().lookup(id);
        Scope ns = b.getScope();
        Translator t = p.getTranslator();

        t.outputMethodSignature(b);
        t.getOutput().increaseIndent();
        t.getLabelManager().beginMethod();

        n.f15.accept(this, new CodeGenPair(ns, t));
        t.outputReturn();

        t.getLabelManager().endMethod();
        t.getOutput().decreaseIndent();
        return null;
    }

    /*
     * f0 -> "class"
     * f1 -> Identifier()
     * f2 -> "{"
     * f3 -> ( VarDeclaration() )*
     * f4 -> ( MethodDeclaration() )*
     * f5 -> "}"
     */
    @Override
    public VariableLabel visit(ClassDeclaration n, CodeGenPair p) {
        Symbol id = Symbol.fromString(TypeCheckHelper.className(n));
        Binder b = p.getScope().lookup(id);

        n.f4.accept(this, new CodeGenPair(b.getScope(), p.getTranslator()));
        return null;
    }

    /*
     * f0 -> "class"
     * f1 -> Identifier()
     * f2 -> "extends"
     * f3 -> Identifier()
     * f4 -> "{"
     * f5 -> ( VarDeclaration() )*
     * f6 -> ( MethodDeclaration() )*
     * f7 -> "}"
     */
    @Override
    public VariableLabel visit(ClassExtendsDeclaration n, CodeGenPair p) {
        Symbol id = Symbol.fromString(TypeCheckHelper.className(n));
        Binder b = p.getScope().lookup(id);

        n.f6.accept(this, new CodeGenPair(b.getScope(), p.getTranslator()));
        return null;
    }

    /*
     * f0 -> "public"
     * f1 -> Type()
     * f2 -> Identifier()
     * f3 -> "("
     * f4 -> ( FormalParameterList() )?
     * f5 -> ")"
     * f6 -> "{"
     * f7 -> ( VarDeclaration() )*
     * f8 -> ( Statement() )*
     * f9 -> "return"
     * f10 -> Expression()
     * f11 -> ";"
     * f12 -> "}"
     */
    @Override
    public VariableLabel visit(MethodDeclaration n, CodeGenPair p) {
        Symbol id = Symbol.fromString(TypeCheckHelper.methodName(n));
        Binder b = p.getScope().lookup(id);
        Scope ns = b.getScope();
        Translator t = p.getTranslator();

        t.getOutput().writeLine();
        t.outputMethodSignature(b);
        t.getOutput().increaseIndent();
        t.getLabelManager().beginMethod();

        CodeGenPair np = new CodeGenPair(ns, t);
        n.f8.accept(this, np);
        VariableLabel ret = CodeGenHelper.retrieveDerefOrFuncCall(n.f10.accept(this, np), np);
        t.outputReturn(ret);

        t.getLabelManager().endMethod();
        t.getOutput().decreaseIndent();
        return null;
    }

    /*
     * f0 -> Identifier()
     * f1 -> "="
{'fraction_non_alphanumeric': 0.16462264150943395, 'fraction_numerical': 0.01650943396226415, 'mean_word_length': 1.8994871794871795, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 109, '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': '4909100', 'n_tokens_mistral': 2809, 'n_tokens_neox': 2716, 'n_words': 525}