2019-03-15 22:33:32 +00:00
|
|
|
package stack
|
|
|
|
|
|
|
|
import "errors"
|
|
|
|
|
|
|
|
// Invocation embeds a Random Access stack
|
|
|
|
// Providing helper methods for the context object
|
|
|
|
type Invocation struct{ RandomAccess }
|
|
|
|
|
|
|
|
//NewInvocation will return a new
|
|
|
|
// Invocation stack
|
|
|
|
func NewInvocation() *Invocation {
|
|
|
|
return &Invocation{
|
|
|
|
RandomAccess{
|
|
|
|
vals: make([]Item, 0, StackAverageSize),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Invocation) peekContext(n uint16) (*Context, error) {
|
|
|
|
item, err := i.Peek(n)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-16 21:45:48 +00:00
|
|
|
return item.Context()
|
2019-03-15 22:33:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CurrentContext returns the current context on the invocation stack
|
|
|
|
func (i *Invocation) CurrentContext() (*Context, error) {
|
|
|
|
return i.peekContext(0)
|
|
|
|
}
|
|
|
|
|
2019-03-16 21:45:48 +00:00
|
|
|
// RemoveCurrentContext removes the context on the top of the invocation stack
|
|
|
|
// This is a convenience method for Pop
|
|
|
|
func (i *Invocation) RemoveCurrentContext() error {
|
|
|
|
_, err := i.Pop()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-15 22:33:32 +00:00
|
|
|
// CallingContext will return the cntext item
|
|
|
|
// that will be called next.
|
|
|
|
func (i *Invocation) CallingContext() (*Context, error) {
|
|
|
|
if i.Len() < 1 {
|
|
|
|
return nil, errors.New("Length of invocation stack is < 1, no calling context")
|
|
|
|
}
|
|
|
|
return i.peekContext(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// EntryContext will return the context item that
|
|
|
|
// started the program
|
|
|
|
func (i *Invocation) EntryContext() (*Context, error) {
|
|
|
|
|
|
|
|
// firstItemIndex refers to the first item
|
|
|
|
// that was popped on the stack
|
|
|
|
firstItemIndex := uint16(i.Len() - 1) // N.B. if this overflows because len is zero, then an error will be returned
|
|
|
|
return i.peekContext(firstItemIndex)
|
|
|
|
}
|