<commit_before>package IntermediateCodeGeneration;

import IntermediateCodeGeneration.SymbolTable.SymbolTable;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

/**
 * Generacion de codigo intermedio.
 *
 * @author <NAME>
 * @author <NAME>
 */
public class ICGenerator {

    ArrayList<ICEntry> intermadiateCode;
    private String outputFile; // CeIASM source code output file.
    private int labelNumber;

    public ICGenerator(String outputFile) {
        this.intermadiateCode = new ArrayList<>();
        this.outputFile = outputFile;
        this.labelNumber = 0;
    }

    /*
     * Given a CeIASM instruction, an offset and a comment, generates an ICEntry.
     */
    public ICEntry GEN(String instruction, int offset, String comment) {
        ICEntry newEntry = new ICEntry(instruction, new Integer(offset), comment);
        intermadiateCode.add(newEntry);
        return newEntry;
    }

    /*
     * Given a CeIASM instruction and an offset, generates an ICEntry without comment.
     */
    public ICEntry GEN(String instruction, int offset) {
        ICEntry newEntry = new ICEntry(instruction, new Integer(offset), null);
        intermadiateCode.add(newEntry);
        return newEntry;
    }

    /*
     * Given a CeIASM instruction and a comment, generates an ICEntry without offset.
     */
    public ICEntry GEN(String instruction, String comment) {
        ICEntry newEntry = new ICEntry(instruction, null, comment);
        intermadiateCode.add(newEntry);
        return newEntry;
    }

    /*
     * Given a CeIASM instruction, an offset and a comment, generates an ICEntry without offset nor comment.
     */
    public ICEntry GEN(String instruction) {
        ICEntry newEntry = new ICEntry(instruction, null, null);
        intermadiateCode.add(newEntry);
        return newEntry;
    }

    /*
     * Writes the generated intermadiate code inside the output file.
     */
    public void generateOutput() {
        try {
            FileWriter fstream = new FileWriter(outputFile);
            BufferedWriter bufferedStream = new BufferedWriter(fstream);
            for (ICEntry ice : intermadiateCode) {
                bufferedStream.write(ice.generateInstruction() + "\n");
            }
            bufferedStream.close();
        } catch (IOException e) {
            System.err.println("Intermadiate code generation error: " + e.getMessage());
        }
    }

    public void setup(SymbolTable symbolTable) {
        GEN(".DATA");

        // Object class doesn't have methods so it isn't necessary to create a VT for it.
        GEN("VT_Object: NOP");

        // System class VT.
        GEN("VT_SYSTEM: DW L_SYSTEM_CTOR, L_SYSTEM_PRINTI, "
                + "L_SYSTEM_READ, L_SYSTEM_PRINTC, L_SYSTEM_PRINTB, "
                + "L_SYSTEM_PRINTS, L_SYSTEM_PRINTLN, L_SYSTEM_PRINTBLN, "
                + "L_SYSTEM_PRINTCLN, L_SYSTEM_PRINTILN, L_SYSTEM_PRINTSLN ");

        GEN(".CODE");
        GEN("PUSH L_SIMPLE_INIT_HEAP", "Inicializamos el heap");
        GEN("CALL");

        // Main method.
        String mainClass = symbolTable.getMainClass();
        GEN("RMEM", 1, "Reservamos memoria para el this");
        GEN("PUSH L_" + mainClass + "_MAIN", "Apilamos el label del main de la Clase Principal del Programa"); //PUSH VT_A (Si A es la clase Ppal del programa)
        GEN("CALL");
        GEN("HALT");

        // System class constructor.
        GEN("L_SYSTEM_CTOR: NOP", "Constructor de system");
        GEN("LOADFP");
        GEN("LOADSP");
        GEN("STOREFP");
        GEN("STOREFP");
        GEN("RET", 1);

        // System methods.
        // System.read()
        GEN("L_SYSTEM_READ: NOP");
        GEN("LOADFP");
        GEN("LOADSP");
        GEN("STOREFP");
        GEN("READ");
        GEN("STORE", 4);
        GEN("STOREFP");
        GEN("RET", 1);

        // System.printi()
        GEN("L_SYSTEM_PRINTI: NOP");
        GEN("LOADFP");
        GEN("LOADSP");
        GEN("STOREFP");
        GEN("LOAD", 4);
        GEN("IPRINT");
        GEN("STOREFP");
        GEN("RET", 2);

        // System.printc()
        GEN("L_SYSTEM_PRINTC: NOP");
        GEN("LOADFP");
        GEN("LOADSP");
        GEN("STOREFP");<commit_msg>Fixs ICGenerator<commit_after>package IntermediateCodeGeneration;

import IntermediateCodeGeneration.SymbolTable.SymbolTable;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

/**
 * Generacion de codigo intermedio.
 *
 * @author <NAME>
 * @author <NAME>
 */
public class ICGenerator {

    ArrayList<ICEntry> intermadiateCode;
    private String outputFile; // CeIASM source code output file.
    private int labelNumber;

    public ICGenerator(String outputFile) {
        this.intermadiateCode = new ArrayList<>();
        this.outputFile = outputFile;
        this.labelNumber = 0;
    }

    /*
     * Given a CeIASM instruction, an offset and a comment, generates an ICEntry.
     */
    public ICEntry GEN(String instruction, int offset, String comment) {
        ICEntry newEntry = new ICEntry(instruction, new Integer(offset), comment);
        intermadiateCode.add(newEntry);
        return newEntry;
    }

    /*
     * Given a CeIASM instruction and an offset, generates an ICEntry without comment.
     */
    public ICEntry GEN(String instruction, int offset) {
        ICEntry newEntry = new ICEntry(instruction, new Integer(offset), null);
        intermadiateCode.add(newEntry);
        return newEntry;
    }

    /*
     * Given a CeIASM instruction and a comment, generates an ICEntry without offset.
     */
    public ICEntry GEN(String instruction, String comment) {
        ICEntry newEntry = new ICEntry(instruction, null, comment);
        intermadiateCode.add(newEntry);
        return newEntry;
    }

    /*
     * Given a CeIASM instruction, an offset and a comment, generates an ICEntry without offset nor comment.
     */
    public ICEntry GEN(String instruction) {
        ICEntry newEntry = new ICEntry(instruction, null, null);
        intermadiateCode.add(newEntry);
        return newEntry;
    }

    /*
     * Writes the generated intermadiate code inside the output file.
     */
    public void generateOutput() {
        try {
            FileWriter fstream = new FileWriter(outputFile);
            BufferedWriter bufferedStream = new BufferedWriter(fstream);
            for (ICEntry ice : intermadiateCode) {
                bufferedStream.write(ice.generateInstruction() + "\n");
            }
            bufferedStream.close();
        } catch (IOException e) {
            System.err.println("Intermadiate code generation error: " + e.getMessage());
        }
    }

    public void setup(SymbolTable symbolTable) {
        GEN(".DATA");

        // Object class doesn't have methods so it isn't necessary to create a VT for it.
        GEN("VT_Object: NOP");

        // System class VT.
        GEN("VT_SYSTEM: DW L_SYSTEM_CTOR, L_SYSTEM_PRINTI, "
                + "L_SYSTEM_READ, L_SYSTEM_PRINTC, L_SYSTEM_PRINTB, "
                + "L_SYSTEM_PRINTS, L_SYSTEM_PRINTLN, L_SYSTEM_PRINTBLN, "
                + "L_SYSTEM_PRINTCLN, L_SYSTEM_PRINTILN, L_SYSTEM_PRINTSLN ");

        GEN(".CODE");
        GEN("PUSH L_SIMPLE_INIT_HEAP", "Inicializamos el heap");
        GEN("CALL");

        // Main method.
        String mainClass = symbolTable.getMainClass();
        GEN("RMEM", 1, "Reservamos memoria para el this");
        GEN("PUSH L_" + mainClass + "_MAIN", "Apilamos el label del main de la Clase Principal del Programa"); //PUSH VT_A (Si A es la clase Ppal del programa)
        GEN("CALL");
        GEN("HALT");

        // System class constructor.
        GEN("L_SYSTEM_CTOR: NOP", "Constructor de system");
        GEN("LOADFP");
        GEN("LOADSP");
        GEN("STOREFP");
        GEN("STOREFP");
        GEN("RET", 1);

        // System methods.
        
        // System.printi()
        GEN("L_SYSTEM_PRINTI: NOP");
        GEN("LOADFP");
        GEN("LOADSP");
        GEN("STOREFP");
        GEN("LOAD", 4);
        GEN("IPRINT");
        GEN("STOREFP");
        GEN("RET", 2);
        
        // System.read()
        GEN("L_SYSTEM_READ: NOP");
        GEN("LOADFP");
        GEN("LOADSP");
        GEN("STOREFP");
        GEN("READ");
        GEN("STORE", 4);
        GEN("STOREFP");
        GEN("RET", 1);

        // System.printc()
        GEN("L_SYSTEM_PRINTC: NOP");
        GEN("LOADFP");
        GEN("LOADSP");
        GEN("STOREFP");
{'fraction_non_alphanumeric': 0.11409866729015665, 'fraction_numerical': 0.0016366612111292963, 'mean_word_length': 2.3457176378568634, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '3022525', 'n_tokens_mistral': 2525, 'n_tokens_neox': 2455, 'n_words': 652}