Initial upload
This commit is contained in:
24
go/forth/stack.go
Normal file
24
go/forth/stack.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package forth
|
||||
|
||||
import "fmt"
|
||||
|
||||
|
||||
|
||||
type Stack struct {
|
||||
stack []int
|
||||
ptr int
|
||||
}
|
||||
|
||||
func (s *Stack) Push(k int) {
|
||||
s.stack = append(s.stack, k)
|
||||
}
|
||||
|
||||
func (s *Stack) Pop() (int, error) {
|
||||
if len(s.stack) == 0 {
|
||||
return 0, fmt.Errorf("cannot pop empty stack")
|
||||
}
|
||||
|
||||
el := s.stack[len(s.stack)-1]
|
||||
s.stack = s.stack[:len(s.stack)-1]
|
||||
return el, nil
|
||||
}
|
||||
Reference in New Issue
Block a user