diff options
Diffstat (limited to 'example/math_expression/stack.go')
-rw-r--r-- | example/math_expression/stack.go | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/example/math_expression/stack.go b/example/math_expression/stack.go new file mode 100644 index 0000000..2ca8acc --- /dev/null +++ b/example/math_expression/stack.go @@ -0,0 +1,26 @@ +package main + +// Simple lexem stack implementation. + +import "github.com/neonxp/unilex" + +type lexemStack []unilex.Lexem + +func (ls *lexemStack) Head() (l unilex.Lexem) { + if len(*ls) == 0 { + return unilex.Lexem{Type: unilex.LEOF} + } + 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.LEOF} + } + *ls, l = (*ls)[:len(*ls)-1], (*ls)[len(*ls)-1] + return l +} |