// Copyright (c) 1996-2002 Brian D. Carlstrom

package bdc.scheme.procedure;

import bdc.scheme.Scheme;
import bdc.scheme.Stack;
import bdc.scheme.Writer;
import bdc.scheme.exception.ArgumentTypeException;
import bdc.scheme.expression.Procedure1;

/**
    (write x) (write x y)
*/
public class Write extends Procedure1
{

    public Object apply1 (Stack stack) throws ArgumentTypeException
    {
        try {
            stack.addElement(stack.array[stack.inUse-1]);
            stack.array[stack.inUse-2] = stack.currentOutputPort;
            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];
        Scheme.printWriter(o2, this).print(Writer.write(o1));
        return Scheme.Unspecified;
    }

}
