aboutsummaryrefslogtreecommitdiff
path: root/example/math_expression/stack.go
blob: eebd476cdd9421d169b648893989012f6b9a04ee (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
28
29
//go:build example
// +build example

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.LexEOF}
	}
	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}
	}
	*ls, l = (*ls)[:len(*ls)-1], (*ls)[len(*ls)-1]
	return l
}