From cc4c01c69c238c92602b393ed87575ba5edad352 Mon Sep 17 00:00:00 2001 From: Alexander Kiryukhin Date: Fri, 1 Jul 2022 02:16:07 +0300 Subject: first commit --- stack.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 stack.go (limited to 'stack.go') diff --git a/stack.go b/stack.go new file mode 100644 index 0000000..c0850d4 --- /dev/null +++ b/stack.go @@ -0,0 +1,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]) +} -- cgit v1.2.3