~mna/snow unlisted

snow/pkg/codegen/visit_scopes.go -rw-r--r-- 806 bytes
424066c5Martin Angers doc: v0.0.5 1 year, 7 months ago
                                                                                
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
package codegen

import "git.sr.ht/~mna/snow/pkg/semantic"

type ScopeVisitor interface {
	VisitScope(s *semantic.Scope) (w ScopeVisitor)
}

func WalkScope(v ScopeVisitor, s *semantic.Scope) {
	if v = v.VisitScope(s); v == nil {
		return
	}
	for _, child := range s.Children {
		WalkScope(v, child)
	}
	v.VisitScope(nil)
}

type inspector func(s *semantic.Scope) bool

func (f inspector) VisitScope(s *semantic.Scope) ScopeVisitor {
	if f(s) {
		return f
	}
	return nil
}

// InspectScope traverses the scopes in breadth-first order: It starts by calling
// f(s); s must not be nil. If f returns true, Inspect invokes f
// recursively for each of the non-nil children of s, followed by a
// call of f(nil).
func InspectScope(s *semantic.Scope, f func(*semantic.Scope) bool) {
	WalkScope(inspector(f), s)
}