// Copyright (c) 1996-2002 Brian D. Carlstrom

package bdc.scheme.procedure;

import bdc.scheme.Stack;
import bdc.scheme.Scheme;
import bdc.scheme.SchemeException;
import bdc.scheme.exception.BoundsException;
import bdc.scheme.expression.Procedure2;
import java.util.List;

/**
    (vector-ref x y)
*/
public class VectorRef extends Procedure2
{
    public Object apply2 (Stack stack) throws SchemeException
    {
        Object o1 = stack.array[stack.inUse-1];
        Object o2 = stack.array[stack.inUse-2];
        List list = Scheme.list(o1, this);
        int size  = list.size();
        int k     = Scheme.integer(o2, this);

        if ((k < 0) || (k >= size)) {
            throw new BoundsException(o1, size, k);
        }
        return list.get(k);
    }
}
