// Copyright (c) 1996-2002 Brian D. Carlstrom

package bdc.scheme.procedure;

import bdc.scheme.Stack;
import bdc.scheme.Scheme;
import bdc.scheme.exception.ArgumentTypeException;
import bdc.scheme.expression.Procedure1;

/**
    (make-string x) (make-string y)
*/
public class MakeString extends Procedure1
{

    public Object apply1 (Stack stack) throws ArgumentTypeException
    {
        try {
            stack.addElement(stack.array[stack.inUse-1]);
            stack.array[stack.inUse-2] = new Character('?');
            return apply2(stack);
        }
        finally {
            stack.inUse -= 1;
        }
    }

    public Object apply2 (Stack stack) throws ArgumentTypeException
    {
        Object o1 = stack.array[stack.inUse-1];
        Object o2 = stack.array[stack.inUse-2];
        int k = Scheme.integer(o1, this);
        char sb[] = new char[k];

        char fill = Scheme.character(o2, this);

        for (int i = 0; i < k; i++) {
            sb[i] = fill;
        }

        return sb;
    }
}
