~mna/snow unlisted

snow/pkg/codegen/treebuild.go -rw-r--r-- 5.1 KiB
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
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
package codegen

import (
	"container/list"
	"fmt"
	goast "go/ast"

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

// The treebuilder builds the Go AST from the nodes associated with the
// abstract semantic graph in the translator phase.
type treebuilder struct {
	filesList *list.List

	curGoFile *goast.File
	gofiles   []*goast.File

	curContainer *container
}

func (tb *treebuilder) Visit(n semantic.Node) semantic.Visitor {
	switch n := n.(type) {
	case *semantic.Unit:
		for _, f := range n.Files {
			semantic.Walk(tb, f)
			tb.gofiles = append(tb.gofiles, tb.curGoFile)
		}

	case *semantic.File:
		elem := tb.filesList.Front()
		tb.filesList.Remove(elem)
		cont := elem.Value.(*container)
		if len(cont.nodes) != 1 {
			panic(fmt.Sprintf("file element should have a single node, got %d", len(cont.nodes)))
		}

		tb.curGoFile = cont.nodes[0].(*goast.File)
		for n := cont.children.Front(); n != nil; n = n.Next() {
			switch v := n.Value.(type) {
			case goast.Decl:
				tb.curGoFile.Decls = append(tb.curGoFile.Decls, v)

			case *container:
				if len(v.nodes) > 0 {
					tb.curContainer = v
					semantic.Walk(tb, v.snode)
				}
				for _, node := range v.nodes {
					tb.curGoFile.Decls = append(tb.curGoFile.Decls, node.(goast.Decl))
				}

			default:
				panic(fmt.Sprintf("invalid element value type: %T", v))
			}
		}

	case *semantic.Fn:
		cont := tb.curContainer
		if n.Body == nil {
			if count := cont.children.Len(); count != 0 {
				panic(fmt.Sprintf("external function should have no child, got %d", count))
			}
			break
		}

		// fn children should be a single container with a single Go node, the body block
		if count := cont.children.Len(); count != 1 {
			panic(fmt.Sprintf("function should have a single child, the body block, got %d", count))
		}
		var body *goast.BlockStmt
		for n := cont.children.Front(); n != nil; n = n.Next() {
			switch v := n.Value.(type) {
			case *container:
				if len(v.nodes) > 0 {
					tb.curContainer = v
					semantic.Walk(tb, v.snode)
				}
				if len(v.nodes) != 1 {
					panic(fmt.Sprintf("function body block should have a single Go node, got %d", len(v.nodes)))
				}
				body = v.nodes[0].(*goast.BlockStmt)

			default:
				panic(fmt.Sprintf("invalid element value type: %T", v))
			}
		}

		// find the body target
		for _, node := range cont.nodes {
			switch node := node.(type) {
			case *goast.FuncDecl:
				node.Body = body
			case *goast.AssignStmt:
				node.Rhs[0].(*goast.FuncLit).Body = body
			}
		}

	case *semantic.Struct:
		// a struct container has a single Go node, the struct decl, and it is fully
		// formed, no need to append children, so nothing to do.
	case *semantic.Interface:
		// an interface container has a single Go node, the interface decl, and it is fully
		// formed, no need to append children, so nothing to do.

	case *semantic.Block:
		cont := tb.curContainer
		if len(cont.nodes) != 1 {
			panic(fmt.Sprintf("block should have a single Go node, got %d", len(cont.nodes)))
		}
		bs := cont.nodes[0].(*goast.BlockStmt)

		for n := cont.children.Front(); n != nil; n = n.Next() {
			switch v := n.Value.(type) {
			case goast.Stmt:
				bs.List = append(bs.List, v)

			case *container:
				if len(v.nodes) > 0 {
					tb.curContainer = v
					semantic.Walk(tb, v.snode)
				}
				for _, node := range v.nodes {
					bs.List = append(bs.List, node.(goast.Stmt))
				}

			case *goast.GenDecl:
				bs.List = append(bs.List, &goast.DeclStmt{Decl: v})

			default:
				panic(fmt.Sprintf("invalid element value type: %T", v))
			}
		}

	case *semantic.If:
		cont := tb.curContainer
		if len(cont.nodes) != 1 {
			panic(fmt.Sprintf("if should have a single Go node, got %d", len(cont.nodes)))
		}
		is := cont.nodes[0].(*goast.IfStmt)

		wantChild := 1
		if n.Else != nil {
			wantChild = 2
		}
		if count := cont.children.Len(); count != wantChild {
			panic(fmt.Sprintf("if should have %d child node(s), got %d", wantChild, count))
		}

		for n := cont.children.Front(); n != nil; n = n.Next() {
			switch v := n.Value.(type) {
			case *container:
				if len(v.nodes) > 0 {
					tb.curContainer = v
					semantic.Walk(tb, v.snode)
				}
				if len(v.nodes) != 1 {
					panic(fmt.Sprintf("if block should have a single Go node, got %d", len(v.nodes)))
				}
				if is.Body == nil {
					is.Body = v.nodes[0].(*goast.BlockStmt)
				} else {
					is.Else = v.nodes[0].(goast.Stmt)
				}

			default:
				panic(fmt.Sprintf("invalid element value type: %T", v))
			}
		}

	case *semantic.Guard:
		cont := tb.curContainer
		if len(cont.nodes) != 1 {
			panic(fmt.Sprintf("guard should have a single Go node, got %d", len(cont.nodes)))
		}
		is := cont.nodes[0].(*goast.IfStmt)

		if count := cont.children.Len(); count != 1 {
			panic(fmt.Sprintf("guard should have a single child, got %d", count))
		}

		for n := cont.children.Front(); n != nil; n = n.Next() {
			switch v := n.Value.(type) {
			case *container:
				if len(v.nodes) > 0 {
					tb.curContainer = v
					semantic.Walk(tb, v.snode)
				}
				if len(v.nodes) != 1 {
					panic(fmt.Sprintf("guard block should have a single Go node, got %d", len(v.nodes)))
				}
				is.Else = v.nodes[0].(*goast.BlockStmt)

			default:
				panic(fmt.Sprintf("invalid element value type: %T", v))
			}
		}
	}
	return nil
}