// 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;

/**
    (string-ref x y)
*/
public class StringRef extends Procedure2
{
    public Object apply2 (Stack stack) throws SchemeException
    {
        Object o1 = stack.array[stack.inUse-1];
        Object o2 = stack.array[stack.inUse-2];
        String string = Scheme.string(o1, this);
        int size      = string.length();
        int k         = Scheme.integer(o2, this);

        if ((k < 0) || (k >= size)) {
            throw new BoundsException(o1, size, k);
        }

        return new Character(string.charAt(k));
    }
}
