/**
 * Class that defines the memory space for the SILLY interpreter. 
 *   @author Dave Reed 
 *   @version 2/3/18
 */
public class MemorySpace {
    private StackSegment stack;
    private HeapSegment heap;
    
    /**
     * Constructs an empty memory space.
     */
    public MemorySpace() {
    	this.stack = new StackSegment();
    	this.heap = new HeapSegment();
    }
    
    /**
     * Stores a variable/value in the stack segment.
     *   @param token the variable name
     *   @param val the value to be stored under that name
     */
    public void storeVariable(Token token, DataValue val) {
    	this.stack.store(token,  val);
    }

    /**
     * Retrieves the value for a variable (from the stack segment).
     *   @param token the variable name
     *   @return the value stored under that name
     */
    public DataValue lookupVariable(Token token) throws Exception {
    	return this.stack.lookup(token);
    }

    /**
     * Stores a dynamic value in the heap segment.
     *   @param obj the value to be stored
     *   @return the heap index where that string constant is stored
     */
    public int reference(Object obj) {
    	return this.heap.store(obj);
    }

    /**
     * Retrieves a dynamic value from the heap segment.
     * @param n the heap index of the value
     * @return the value at index n
     */
    public Object dereference(int n) {
    	return this.heap.lookup(n);
    }
}
