mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2025-05-06 19:55:10 +00:00
vm: implement REVERSE* opcodes
Use new opcodes in the compiler instead of XSWAP/ROLL.
This commit is contained in:
parent
c8a1188ee1
commit
d18199ce42
6 changed files with 161 additions and 81 deletions
|
@ -353,9 +353,31 @@ func (s *Stack) Swap(n1, n2 int) error {
|
|||
if n1 == n2 {
|
||||
return nil
|
||||
}
|
||||
s.swap(n1, n2)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Stack) swap(n1, n2 int) {
|
||||
a := s.Peek(n1)
|
||||
b := s.Peek(n2)
|
||||
a.value, b.value = b.value, a.value
|
||||
}
|
||||
|
||||
// ReverseTop reverses top n items of the stack.
|
||||
func (s *Stack) ReverseTop(n int) error {
|
||||
if n < 0 {
|
||||
return errors.New("negative index")
|
||||
} else if n > s.len {
|
||||
return errors.New("too big index")
|
||||
} else if n <= 1 {
|
||||
return nil
|
||||
}
|
||||
|
||||
for i, j := 0, n-1; i < j; {
|
||||
s.swap(i, j)
|
||||
i++
|
||||
j--
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue