aboutsummaryrefslogtreecommitdiff
path: root/stack.go
blob: c0850d4438ae6700f4a2bab0d1ed3a486acb0fc9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package expression

type Stack []Token

func (s *Stack) Push(item Token) {
	*s = append(*s, item)
}

func (s *Stack) Pop() (item Token) {
	if len(*s) == 0 {
		return
	}

	*s, item = (*s)[:len(*s)-1], (*s)[len(*s)-1]
	return item
}

func (s *Stack) Empty() bool {
	return len(*s) == 0
}

func (s *Stack) Head() (item *Token) {
	if s.Empty() {
		return nil
	}
	return &((*s)[len(*s)-1])
}