unilex/example/math_expression/stack.go

27 lines
501 B
Go
Raw Normal View History

2021-03-06 22:30:32 +03:00
package main
// Simple lexem stack implementation.
2024-04-28 02:33:13 +03:00
import "go.neonxp.ru/unilex"
2021-03-06 22:30:32 +03:00
type lexemStack []unilex.Lexem
func (ls *lexemStack) Head() (l unilex.Lexem) {
if len(*ls) == 0 {
return unilex.Lexem{Type: unilex.LexEOF}
2021-03-06 22:30:32 +03:00
}
return (*ls)[len(*ls)-1]
}
func (ls *lexemStack) Push(l unilex.Lexem) {
*ls = append(*ls, l)
}
func (ls *lexemStack) Pop() (l unilex.Lexem) {
if len(*ls) == 0 {
return unilex.Lexem{Type: unilex.LexEOF}
2021-03-06 22:30:32 +03:00
}
*ls, l = (*ls)[:len(*ls)-1], (*ls)[len(*ls)-1]
return l
}