~mna/snow

ref: f403078a7866d9f7107b7c666ea16f424050c5c6 snow/pkg/semantic/visitor.go -rw-r--r-- 3.3 KiB
f403078aMartin Angers pkg/codegen: done with generating nodes for struct generics and all 2 years 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
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package semantic

import "fmt"

type Visitor interface {
	Visit(n Node) (w Visitor)
}

func Walk(v Visitor, node Node) {
	if v = v.Visit(node); v == nil {
		return
	}

	switch n := node.(type) {
	case *Unit:
		// n.Main is part of the functions that will be visited, don't visit it
		// separately here.
		for _, f := range n.Files {
			Walk(v, f)
		}

	case *File:
		for _, vv := range n.Vars {
			Walk(v, vv)
		}
		for _, fn := range n.Fns {
			Walk(v, fn)
		}
		for _, str := range n.Structs {
			Walk(v, str)
		}

	case *Fn:
		for _, attr := range n.Attrs {
			Walk(v, attr)
		}
		if n.GenericParams != nil {
			for _, elem := range n.GenericParams.Elems {
				Walk(v, elem)
			}
		}
		for _, p := range n.Params {
			Walk(v, p)
		}
		if n.ReturnExpr != nil {
			Walk(v, n.ReturnExpr)
		}
		if n.Body != nil {
			Walk(v, n.Body)
		}
		// n.MethodOf is a reference to a struct that exists elsewhere, don't visit
		// it separately here.

	case *Var:
		if n.TypeExpr != nil {
			Walk(v, n.TypeExpr)
		}
		if n.Value != nil {
			Walk(v, n.Value)
		}

	case *Struct:
		if n.GenericParams != nil {
			for _, elem := range n.GenericParams.Elems {
				Walk(v, elem)
			}
		}
		for _, vv := range n.Vars {
			Walk(v, vv)
		}
		for _, fn := range n.Fns {
			Walk(v, fn)
		}
		for _, str := range n.Structs {
			Walk(v, str)
		}

	case *Block:
		for _, stmt := range n.Stmts {
			Walk(v, stmt)
		}

	case *Return:
		if n.Value != nil {
			Walk(v, n.Value)
		}

	case *Assign:
		if n.Left != nil {
			Walk(v, n.Left)
		}
		if n.Right != nil {
			Walk(v, n.Right)
		}

	case *ExprStmt:
		if n.Value != nil {
			Walk(v, n.Value)
		}

	case *If:
		for _, cond := range n.Conds {
			Walk(v, cond)
		}
		if n.Body != nil {
			Walk(v, n.Body)
		}
		if n.Else != nil {
			Walk(v, n.Else)
		}

	case *Guard:
		for _, cond := range n.Conds {
			Walk(v, cond)
		}
		if n.Else != nil {
			Walk(v, n.Else)
		}

	case *FnTypeExpr:
		for _, p := range n.Params {
			Walk(v, p)
		}
		if n.Return != nil {
			Walk(v, n.Return)
		}

	case *TupleTypeExpr:
		for _, field := range n.Fields {
			Walk(v, field)
		}

	case *TupleVal:
		for _, val := range n.Values {
			Walk(v, val)
		}

	case *Binary:
		if n.Left != nil {
			Walk(v, n.Left)
		}
		if n.Right != nil {
			Walk(v, n.Right)
		}

	case *Unary:
		if n.Right != nil {
			Walk(v, n.Right)
		}

	case *Paren:
		if n.Value != nil {
			Walk(v, n.Value)
		}

	case *Call:
		if n.Fun != nil {
			Walk(v, n.Fun)
		}
		for _, arg := range n.Args {
			Walk(v, arg)
		}

	case *Selector:
		if n.Left != nil {
			Walk(v, n.Left)
		}
		if n.Sel != nil {
			Walk(v, n.Sel)
		}

	case *GenericInst:
		if n.GenericDecl != nil {
			Walk(v, n.GenericDecl)
		}
		for _, typ := range n.TypeExprs {
			Walk(v, typ)
		}

	case *ImplicitConv:
		if n.Value != nil {
			Walk(v, n.Value)
		}

	case *LitString, *LitInt, *Ident, *GenericElem:
		// nothing to visit

	default:
		panic(fmt.Sprintf("Walk: unexpected node type %T", n))
	}
	v.Visit(nil)
}

type inspector func(Node) bool

func (f inspector) Visit(node Node) Visitor {
	if f(node) {
		return f
	}
	return nil
}

// Inspect traverses an AST in depth-first order: It starts by calling
// f(node); node must not be nil. If f returns true, Inspect invokes f
// recursively for each of the non-nil children of node, followed by a
// call of f(nil).
func Inspect(node Node, f func(Node) bool) {
	Walk(inspector(f), node)
}