// Copyright (c) 1996-2002 Brian D. Carlstrom

package bdc.scheme.procedure;

import bdc.scheme.Scheme;
import bdc.scheme.SchemeException;
import bdc.scheme.Stack;
import bdc.scheme.expression.Procedure1;
import java.util.ArrayList;
import java.util.List;

/**
    (make-vector x) (make-vector x y)
*/
public class MakeVector extends Procedure1
{
    public Object apply1 (Stack stack) throws SchemeException
    {
        try {
            stack.addElement(stack.array[stack.inUse-1]);
            stack.array[stack.inUse-2] = Scheme.Unspecified;
            return apply2(stack);
        }
        finally {
            stack.inUse -= 1;
        }
    }

    public Object apply2 (Stack stack) throws SchemeException
    {
        Object o1 = stack.array[stack.inUse-1];
        Object o2 = stack.array[stack.inUse-2];
        int k = Scheme.integer(o1, this);
        List l = new ArrayList(k);
        for (int i = 0; i < k ; i++) {
            l.add(o2);
        }
        return l;
    }
}
