/
symstack.go
76 lines (66 loc) · 1.61 KB
/
symstack.go
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright (c) 2018, Cogent Core. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package syms
import (
"cogentcore.org/core/pi/lex"
"cogentcore.org/core/pi/token"
)
// SymStack is a simple stack (slice) of symbols
type SymStack []*Symbol
// Top returns the state at the top of the stack (could be nil)
func (ss *SymStack) Top() *Symbol {
sz := len(*ss)
if sz == 0 {
return nil
}
return (*ss)[sz-1]
}
// Push appends symbol to stack
func (ss *SymStack) Push(sy *Symbol) {
*ss = append(*ss, sy)
}
// PushNew adds a new symbol to the stack with the basic info
func (ss *SymStack) PushNew(name string, kind token.Tokens, fname string, reg lex.Reg) *Symbol {
sy := NewSymbol(name, kind, fname, reg)
ss.Push(sy)
return sy
}
// Pop takes symbol off the stack and returns it
func (ss *SymStack) Pop() *Symbol {
sz := len(*ss)
if sz == 0 {
return nil
}
sy := (*ss)[sz-1]
*ss = (*ss)[:sz-1]
return sy
}
// Reset resets the stack
func (ss *SymStack) Reset() {
*ss = nil
}
// FindNameScoped searches top-down in the stack for something with the given name
// in symbols that are of subcategory token.NameScope (i.e., namespace, module, package, library)
func (ss *SymStack) FindNameScoped(nm string) (*Symbol, bool) {
sz := len(*ss)
if sz == 0 {
return nil, false
}
for i := sz - 1; i >= 0; i-- {
sy := (*ss)[i]
if sy.Name == nm {
return sy, true
}
ssy, has := sy.Children.FindNameScoped(nm)
if has {
return ssy, true
}
}
return nil, false
}
func (ss *SymStack) ClearAst() {
for _, sy := range *ss {
sy.ClearAst()
}
}