<commit_before>/* DataStack.java */

/* $Id$ */

import java.util.List;
import java.util.ArrayList;
import java.util.Stack;

/**
 * An abstract data type representing the PAL data stack.
 *
 * @version $Revision$
 */
public class DataStack {
    /** A container for the <code>Data</code> objects. */
    private List data;

    /** The address of the current frame's base. */
    private int frameBase;

    /** Reference to the next free space on top of the stack (TOS). */
    private int top;

    /** Maximum stack size. A value of 0 indicates no limit.*/
    private int maxSize;

    /**
     * Default constructor.  Assumes no limit on stack size.
     */
    public DataStack() {
        this(0);
    }

    /**
     * Constructor.  Allows user to specify a limit on maximum size.
     *
     * @param max Maximum stack size.
     */
    public DataStack(int max) {
        top = 0;

        data = new ArrayList();

        //Set up mark stack part for main program activation record.
        markStack(0, 0);

        setBase(top);

        maxSize = max;
    }

    /**
     * Put a data object onto the top of the stack.
     *
     * @param datum A <code>Data</code> object to be pushed onto the
     * stack.
     * @exception java.lang.OutOfMemoryError if there is insufficient
     * free stack space.
     */
    public void push(Data datum) throws OutOfMemoryError {
        top++;

        data.add(datum);

        if(maxSize == 0 || top < maxSize)
            return;

        throw new OutOfMemoryError("PAL Stack out of memory.");
    }

    /**
     * Pop the top value from the stack.
     *
     * @return The <code>Data</code> object removed from the top of
     * the stack.
     */
    public Data pop() {
        return (Data)data.remove(--top);
    }

    /**
     * Peek at the top of the stack.
     * 
     * @return The <code>Data</code> object remaining on the top of
     * the stack.
     */
    public Data peek() {
        return (Data)data.get(top - 1);
    }

    /**
     * Read a data location elsewhere in the stack.  The location is
     * given as an absolute stack address.
     *
     * @param address The absolute address for the target location.
     * @return The <code>Data</code> object at the target location.
     * @exception java.lang.IndexOutOfBoundsException if the supplied
     * address is out of bounds.
     */
    public Data get(int address) throws IndexOutOfBoundsException {
        if(address < 0 || address >= top)
            throw new IndexOutOfBoundsException("PAL address out of bounds.");

        return (Data)data.get(address);
    }

    /**
     * Read a data location elsewhere in the stack.  The location is
     * given as a level difference and offset.
     *
     * @param levelDiff The difference in static scope level between
     * the current activation record, and the activation record of the
     * target location.
     * @param offset The offset into the target stack frame.
     * @return The <code>Data</code> object at the target address.
     * @exception java.lang.IndexOutOfBoundsException if the supplied
     * address is out of bounds.
     */
    public Data get(int levelDiff, int offset) throws IndexOutOfBoundsException {
        int address = getAddress(levelDiff, offset);

        return get(address);
    }

    /**
     * Advance the TOS pointer by a given amount, initialising memory
     * to type UNDEF as we go.
     *
     * @param amount The number of location to advance the TOS
     * pointer.
     * @exception java.lang.OutOfMemoryError if an attempt is made to
     * advance the TOS pointer beyond the limit of the stack memory.
     */
    public void incTop(int amount) throws OutOfMemoryError {
        for(int i = 0;i < amount;i++) {
            data.add(new Data(Data.UNDEF, null));
            top++;
        }

        if(maxSize == 0 || top < maxSize)
            return;

        throw new OutOfMemoryError("PAL Stack out of memory");
    }

    /**
     * Mark the stack.
     *
     * @param staticLink A pointer to the activation record one level
     * below the current level in terms of <em>lexical scope</em>.
     * @param dynamicLink A pointer to the activation record one level
     * below the current level in terms of <em>dynamic scope</em>.
     * @exception java.lang.OutOfMemoryError if the TOS pointer is
     * advanced beyond the limit of stack memory.
     */
    public void markStack(int staticLink, int dynamicLink) throws OutOfMemoryError {
        push(new Data(Data.INT, new Integer(staticLink)));
        push(new Data(Data.INT, new Integer(dynamicLink)));
<commit_msg>Optimised the maxSize checking in incTop; Made it legal to give a negative maxSize (means no limit).<commit_after>/* DataStack.java */

/* $Id$ */

import java.util.List;
import java.util.ArrayList;
import java.util.Stack;

/**
 * An abstract data type representing the PAL data stack.
 *
 * @version $Revision$
 */
public class DataStack {
    /** A container for the <code>Data</code> objects. */
    private List data;

    /** The address of the current frame's base. */
    private int frameBase;

    /** Reference to the next free space on top of the stack (TOS). */
    private int top;

    /** Maximum stack size. A value <= 0 indicates no limit.*/
    private int maxSize;

    /**
     * Default constructor.  Assumes no limit on stack size.
     */
    public DataStack() {
        this(0);
    }

    /**
     * Constructor.  Allows user to specify a limit on maximum size.
     *
     * @param max Maximum stack size.
     */
    public DataStack(int max) {
        top = 0;

        data = new ArrayList();

        //Set up mark stack part for main program activation record.
        markStack(0, 0);

        setBase(top);

        maxSize = (max > 0 ? max : 0);
    }

    /**
     * Put a data object onto the top of the stack.
     *
     * @param datum A <code>Data</code> object to be pushed onto the
     * stack.
     * @exception java.lang.OutOfMemoryError if there is insufficient
     * free stack space.
     */
    public void push(Data datum) throws OutOfMemoryError {
        top++;

        data.add(datum);

        if(maxSize == 0 || top < maxSize)
            return;

        throw new OutOfMemoryError("PAL Stack out of memory.");
    }

    /**
     * Pop the top value from the stack.
     *
     * @return The <code>Data</code> object removed from the top of
     * the stack.
     */
    public Data pop() {
        return (Data)data.remove(--top);
    }

    /**
     * Peek at the top of the stack.
     * 
     * @return The <code>Data</code> object remaining on the top of
     * the stack.
     */
    public Data peek() {
        return (Data)data.get(top - 1);
    }

    /**
     * Read a data location elsewhere in the stack.  The location is
     * given as an absolute stack address.
     *
     * @param address The absolute address for the target location.
     * @return The <code>Data</code> object at the target location.
     * @exception java.lang.IndexOutOfBoundsException if the supplied
     * address is out of bounds.
     */
    public Data get(int address) throws IndexOutOfBoundsException {
        if(address < 0 || address >= top)
            throw new IndexOutOfBoundsException("PAL address out of bounds.");

        return (Data)data.get(address);
    }

    /**
     * Read a data location elsewhere in the stack.  The location is
     * given as a level difference and offset.
     *
     * @param levelDiff The difference in static scope level between
     * the current activation record, and the activation record of the
     * target location.
     * @param offset The offset into the target stack frame.
     * @return The <code>Data</code> object at the target address.
     * @exception java.lang.IndexOutOfBoundsException if the supplied
     * address is out of bounds.
     */
    public Data get(int levelDiff, int offset) throws IndexOutOfBoundsException {
        int address = getAddress(levelDiff, offset);

        return get(address);
    }

    /**
     * Advance the TOS pointer by a given amount, initialising memory
     * to type UNDEF as we go.
     *
     * @param amount The number of location to advance the TOS
     * pointer.
     * @exception java.lang.OutOfMemoryError if an attempt is made to
     * advance the TOS pointer beyond the limit of the stack memory.
     */
    public void incTop(int amount) throws OutOfMemoryError {
        if((maxSize != 0) && (amount + top > maxSize))
            throw new OutOfMemoryError("PAL Stack out of memory");

        for(int i = 0;i < amount;i++) {
            data.add(new Data(Data.UNDEF, null));
            top++;
        }
    }

    /**
     * Mark the stack.
     *
     * @param staticLink A pointer to the activation record one level
     * below the current level in terms of <em>lexical scope</em>.
     * @param dynamicLink A pointer to the activation record one level
     * below the current level in terms of <em>dynamic scope</em>.
     * @exception java.lang.OutOfMemoryError if the TOS pointer is
     * advanced beyond the limit of stack memory.
     */
    public void markStack(int staticLink, int dynamicLink) throws OutOfMemoryError {
        push(new Data(Data.INT, new Integer(staticLink)));
        push(new Data(Data.INT, new Integer(dynamicLink)));

{'fraction_non_alphanumeric': 0.09458159666559794, 'fraction_numerical': 0.002351180934060062, 'mean_word_length': 2.231353591160221, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 39, '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': '1721948', 'n_tokens_mistral': 2636, 'n_tokens_neox': 2553, 'n_words': 1022}