From 08b234429b999b48686f487f8c534965de10dd58 Mon Sep 17 00:00:00 2001 From: Vibhav Pant Date: Wed, 9 Aug 2017 12:45:25 +0530 Subject: [PATCH] internal/stack: New package. stack implements a growable uint64 stack --- internal/stack/stack.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 internal/stack/stack.go diff --git a/internal/stack/stack.go b/internal/stack/stack.go new file mode 100644 index 00000000..4481da02 --- /dev/null +++ b/internal/stack/stack.go @@ -0,0 +1,40 @@ +// Copyright 2017 The go-interpreter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Package stack implements a growable uint64 stack +package stack + +type Stack struct { + slice []uint64 +} + +func (s *Stack) Push(b uint64) { + s.slice = append(s.slice, b) +} + +func (s *Stack) Pop() uint64 { + v := s.Top() + s.slice = s.slice[:len(s.slice)-1] + return v +} + +func (s *Stack) SetTop(v uint64) { + s.slice[len(s.slice)-1] = v +} + +func (s *Stack) Top() uint64 { + return s.slice[len(s.slice)-1] +} + +func (s *Stack) Get(i int) uint64 { + return s.slice[i] +} + +func (s *Stack) Set(i int, v uint64) { + s.slice[i] = v +} + +func (s *Stack) Len() int { + return len(s.slice) +}