diff options
author | Alexander Kiryukhin <i@neonxp.dev> | 2022-07-01 02:16:07 +0300 |
---|---|---|
committer | Alexander Kiryukhin <i@neonxp.dev> | 2022-07-01 02:16:07 +0300 |
commit | cc4c01c69c238c92602b393ed87575ba5edad352 (patch) | |
tree | 12416835a04c98dba8450d6e799754fb32c7ca0f /stack.go |
first commit
Diffstat (limited to 'stack.go')
-rw-r--r-- | stack.go | 27 |
1 files changed, 27 insertions, 0 deletions
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]) +} |