From d8d27761aed3adf71d30635eb18c4ef07525ae27 Mon Sep 17 00:00:00 2001 From: BlockChainDev Date: Wed, 27 Feb 2019 20:55:48 +0000 Subject: [PATCH] VM: Add stackItems; Array, Boolean, Int and ByteArray --- pkg/vm/stack/Int.go | 39 +++++++++++++++++++++++++++++++++++++++ pkg/vm/stack/array.go | 13 +++++++++++++ pkg/vm/stack/boolean.go | 13 +++++++++++++ pkg/vm/stack/bytearray.go | 12 ++++++++++++ 4 files changed, 77 insertions(+) create mode 100644 pkg/vm/stack/Int.go create mode 100644 pkg/vm/stack/array.go create mode 100644 pkg/vm/stack/boolean.go create mode 100644 pkg/vm/stack/bytearray.go diff --git a/pkg/vm/stack/Int.go b/pkg/vm/stack/Int.go new file mode 100644 index 000000000..6ba864b90 --- /dev/null +++ b/pkg/vm/stack/Int.go @@ -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 +} diff --git a/pkg/vm/stack/array.go b/pkg/vm/stack/array.go new file mode 100644 index 000000000..96fe876a4 --- /dev/null +++ b/pkg/vm/stack/array.go @@ -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 +} diff --git a/pkg/vm/stack/boolean.go b/pkg/vm/stack/boolean.go new file mode 100644 index 000000000..d6a2c12ac --- /dev/null +++ b/pkg/vm/stack/boolean.go @@ -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 +} diff --git a/pkg/vm/stack/bytearray.go b/pkg/vm/stack/bytearray.go new file mode 100644 index 000000000..23b7fc805 --- /dev/null +++ b/pkg/vm/stack/bytearray.go @@ -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 +}