github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/chain/core/vm/stack.go (about)

     1  package vm
     2  
     3  import (
     4  	"fmt"
     5  	"math/big"
     6  )
     7  
     8  type Stack struct {
     9  	data []*big.Int
    10  }
    11  
    12  func newstack() *Stack {
    13  	return &Stack{data: make([]*big.Int, 0, 1024)}
    14  }
    15  
    16  func (st *Stack) Data() []*big.Int {
    17  	return st.data
    18  }
    19  
    20  func (st *Stack) push(d *big.Int) {
    21  
    22  	st.data = append(st.data, d)
    23  }
    24  func (st *Stack) pushN(ds ...*big.Int) {
    25  	st.data = append(st.data, ds...)
    26  }
    27  
    28  func (st *Stack) pop() (ret *big.Int) {
    29  	ret = st.data[len(st.data)-1]
    30  	st.data = st.data[:len(st.data)-1]
    31  	return
    32  }
    33  
    34  func (st *Stack) len() int {
    35  	return len(st.data)
    36  }
    37  
    38  func (st *Stack) swap(n int) {
    39  	st.data[st.len()-n], st.data[st.len()-1] = st.data[st.len()-1], st.data[st.len()-n]
    40  }
    41  
    42  func (st *Stack) dup(pool *intPool, n int) {
    43  	st.push(pool.get().Set(st.data[st.len()-n]))
    44  }
    45  
    46  func (st *Stack) peek() *big.Int {
    47  	return st.data[st.len()-1]
    48  }
    49  
    50  func (st *Stack) Back(n int) *big.Int {
    51  	return st.data[st.len()-n-1]
    52  }
    53  
    54  func (st *Stack) Print() {
    55  	fmt.Println("### stack ###")
    56  	if len(st.data) > 0 {
    57  		for i, val := range st.data {
    58  			fmt.Printf("%-3d  %v\n", i, val)
    59  		}
    60  	} else {
    61  		fmt.Println("-- empty --")
    62  	}
    63  	fmt.Println("#############")
    64  }