From ea72b9dbb21ebd249b84bdb17d34cf70d14daf1b Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Wed, 22 Sep 2021 23:34:59 +0200 Subject: Finished implementing the stack stack --- pkg/stack/stack-stack.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'pkg/stack/stack-stack.go') diff --git a/pkg/stack/stack-stack.go b/pkg/stack/stack-stack.go index adc41c6..9b80079 100644 --- a/pkg/stack/stack-stack.go +++ b/pkg/stack/stack-stack.go @@ -43,3 +43,49 @@ func (ss *StackStack) Begin(p *pointer.Pointer) { soss.Push(y) p.CalculateNewStorageOffset() } + +func (ss *StackStack) End(p *pointer.Pointer) (reflect bool) { + if ss.height == 1 { + return true + } + soss := ss.head.next + n := ss.head.Pop() + y := soss.Pop() + x := soss.Pop() + p.SetStorageOffset(x, y) + if n > 0 { + soss.height += n + if soss.size < soss.height { + soss.data = append(soss.data, make([]int, soss.height-soss.size)...) + soss.size = soss.height + } + for i := n; i > 0; i-- { + soss.data[soss.height-i] = ss.head.data[ss.head.height-i] + } + } else { + soss.height += n + if soss.height < 0 { + soss.height = 0 + } + } + ss.height-- + ss.head = ss.head.next + return false +} + +func (ss *StackStack) Under() (reflect bool) { + if ss.height == 1 { + return true + } + n := ss.head.Pop() + if n > 0 { + for i := 0; i < n; i++ { + ss.head.Push(ss.head.next.Pop()) + } + } else { + for i := 0; i < -n; i++ { + ss.head.next.Push(ss.head.Pop()) + } + } + return false +} -- cgit v1.2.3