VM: Add stackItems; Array, Boolean, Int and ByteArray

This commit is contained in:
BlockChainDev 2019-02-27 20:55:48 +00:00
parent e29b85d0d7
commit d8d27761ae
4 changed files with 77 additions and 0 deletions

39
pkg/vm/stack/Int.go Normal file
View file

@ -0,0 +1,39 @@
package stack
import "math/big"
// Int represents an integer on the stack
type Int struct {
*abstractItem
val *big.Int
}
// NewInt will convert a big integer into
// a StackInteger
func NewInt(val *big.Int) (*Int, error) {
return &Int{
abstractItem: &abstractItem{},
val: val,
}, nil
}
// Equal will check if two integers hold equal value
func (i *Int) Equal(s *Int) bool {
if i.val.Cmp(s.val) != 0 {
return false
}
return true
}
// Add will add two stackIntegers together
func (i *Int) Add(s *Int) (*Int, error) {
return &Int{
val: new(big.Int).Sub(i.val, s.val),
}, nil
}
// Integer will overwrite the default implementation
// to allow go to cast this item as an integer.
func (i *Int) Integer() (*Int, error) {
return i, nil
}

13
pkg/vm/stack/array.go Normal file
View file

@ -0,0 +1,13 @@
package stack
// Array represents an Array of stackItems on the stack
type Array struct {
*abstractItem
val []Item
}
// Array overrides the default implementation
// by the abstractItem, returning an Array struct
func (a *Array) Array() (*Array, error) {
return a, nil
}

13
pkg/vm/stack/boolean.go Normal file
View file

@ -0,0 +1,13 @@
package stack
// Boolean represents a boolean value on the stack
type Boolean struct {
*abstractItem
val bool
}
// Boolean overrides the default implementation
// by the abstractItem, returning a Boolean struct
func (b *Boolean) Boolean() (*Boolean, error) {
return b, nil
}

12
pkg/vm/stack/bytearray.go Normal file
View file

@ -0,0 +1,12 @@
package stack
// ByteArray represents a slice of bytes on the stack
type ByteArray struct {
*abstractItem
val []byte
}
//ByteArray overrides the default abstractItem Bytes array method
func (ba *ByteArray) ByteArray() (*ByteArray, error) {
return ba, nil
}