aboutsummaryrefslogtreecommitdiff
path: root/example/math_expression/stack.go
diff options
context:
space:
mode:
authorAlexander Kiryukhin <a.kiryukhin@mail.ru>2021-03-06 22:30:32 +0300
committerAlexander Kiryukhin <a.kiryukhin@mail.ru>2021-03-06 22:30:32 +0300
commit93740d2d153b3da3a1be5707db1400106e3f6491 (patch)
treeddf705b638434710fa6304201f560f018f4864fe /example/math_expression/stack.go
Initial
Diffstat (limited to 'example/math_expression/stack.go')
-rw-r--r--example/math_expression/stack.go26
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
+}