// Copyright (c) 1996-2002 Brian D. Carlstrom

package bdc.scheme;

import bdc.util.Fmt;
import bdc.util.ObjectArray;
import bdc.util.SystemUtil;
import bdc.util.MultiPrintWriter;
import java.io.PrintWriter;
import java.io.PushbackReader;

/**
    Stack for storing arguments and locals

    Also allows access to the Scheme state for this thread
*/
public class Stack extends ObjectArray
{
    public Scheme scheme;
    public PushbackReader      currentInputPort;
    public PrintWriter         currentOutputPort;
    public PrintWriter         currentErrorPort;
    public MultiPrintWriter    currentTranscriptPort;
    public int frame;

    public Stack (Scheme scheme)
    {
        this(scheme,
             new PushbackReader(SystemUtil.in()),
             new MultiPrintWriter(System.out, false),
             new MultiPrintWriter(System.err, false));
    }
    
    public Stack (Scheme              scheme,
                  PushbackReader      currentInputPort,
                  MultiPrintWriter    currentOutputPort,
                  MultiPrintWriter    currentErrorPort)
    {
        this.scheme = scheme;
        this.currentInputPort = currentInputPort;
        this.currentOutputPort = currentOutputPort;
        this.currentErrorPort = currentErrorPort;
    }

    public String toString ()
    {
        String result = "TOP\n";
        for (int i = inUse-1; i >=0; i--) {
            result = Fmt.S("%s[%s] %s\n",
                           result,
                           Integer.toString(i),
                           Writer.write(array[i]));
        }
        return Fmt.S("%sBOTTOM\n", result);
    }
}
