forked from TrueCloudLab/neoneo-go
Make Next() method on Context failable
refactor peekContext and Peek
This commit is contained in:
parent
48413900ca
commit
9eb11d2822
3 changed files with 23 additions and 11 deletions
|
@ -41,12 +41,12 @@ func (c *Context) Context() (*Context, error) {
|
|||
}
|
||||
|
||||
// Next return the next instruction to execute.
|
||||
func (c *Context) Next() Instruction {
|
||||
func (c *Context) Next() (Instruction, error) {
|
||||
c.ip++
|
||||
if c.ip >= len(c.prog) {
|
||||
return RET
|
||||
return RET, errors.New("program pointer is more than the length of program. RETURNING")
|
||||
}
|
||||
return Instruction(c.prog[c.ip])
|
||||
return Instruction(c.prog[c.ip]), nil
|
||||
}
|
||||
|
||||
// IP returns the absolute instruction without taking 0 into account.
|
||||
|
|
|
@ -21,11 +21,7 @@ func (i *Invocation) peekContext(n uint16) (*Context, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ctx, err := item.Context()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ctx, nil
|
||||
return item.Context()
|
||||
}
|
||||
|
||||
// CurrentContext returns the current context on the invocation stack
|
||||
|
@ -33,6 +29,13 @@ func (i *Invocation) CurrentContext() (*Context, error) {
|
|||
return i.peekContext(0)
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// CallingContext will return the cntext item
|
||||
// that will be called next.
|
||||
func (i *Invocation) CallingContext() (*Context, error) {
|
||||
|
|
|
@ -106,15 +106,14 @@ func (ras *RandomAccess) Peek(n uint16) (Item, error) {
|
|||
return ras.vals[index], nil
|
||||
}
|
||||
|
||||
if ras.vals == nil {
|
||||
return nil, errors.New("Cannot peak at a nil stack")
|
||||
if ras.Len() < 1 {
|
||||
return nil, fmt.Errorf("cannot peak at a stack with no item, length of stack is %d", ras.Len())
|
||||
}
|
||||
|
||||
// Check that we are not peeking out of the bounds
|
||||
if n > stackSize-1 {
|
||||
return nil, fmt.Errorf("Tried to peek at index %d when length of stack is %d", n, len(ras.vals))
|
||||
}
|
||||
|
||||
index := stackSize - n - 1
|
||||
|
||||
return ras.vals[index], nil
|
||||
|
@ -131,3 +130,13 @@ func (ras *RandomAccess) PopInt() (*Int, error) {
|
|||
}
|
||||
return item.Integer()
|
||||
}
|
||||
|
||||
// PopByteArray will remove the last stack item that was added
|
||||
// And cast it to an ByteArray
|
||||
func (ras *RandomAccess) PopByteArray() (*ByteArray, error) {
|
||||
item, err := ras.Pop()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return item.ByteArray()
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue