-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract.go
209 lines (195 loc) · 4.87 KB
/
extract.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
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
// Copyright (c) 2022, The Goki Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"slices"
)
func ReadFileLines(fn string) ([][]byte, error) {
nl := []byte("\n")
buf, err := os.ReadFile(fn)
if err != nil {
fmt.Println(err)
return nil, err
}
lines := bytes.Split(buf, nl)
return lines, nil
}
// Extracts comment-directive tagged regions from .go files
func ExtractGoFiles(files []string) map[string][]byte {
sls := map[string][][]byte{}
key := []byte("//gosl: ")
start := []byte("start")
hlsl := []byte("hlsl")
nohlsl := []byte("nohlsl")
end := []byte("end")
nl := []byte("\n")
include := []byte("#include")
for _, fn := range files {
if !strings.HasSuffix(fn, ".go") {
continue
}
lines, err := ReadFileLines(fn)
if err != nil {
continue
}
inReg := false
inHlsl := false
inNoHlsl := false
var outLns [][]byte
slFn := ""
for _, ln := range lines {
tln := bytes.TrimSpace(ln)
isKey := bytes.HasPrefix(tln, key)
var keyStr []byte
if isKey {
keyStr = tln[len(key):]
// fmt.Printf("key: %s\n", string(keyStr))
}
switch {
case inReg && isKey && bytes.HasPrefix(keyStr, end):
if inHlsl || inNoHlsl {
outLns = append(outLns, ln)
}
sls[slFn] = outLns
inReg = false
inHlsl = false
inNoHlsl = false
case inReg:
for pkg := range LoadedPackageNames { // remove package prefixes
if !bytes.Contains(ln, include) {
ln = bytes.ReplaceAll(ln, []byte(pkg+"."), []byte{})
}
}
outLns = append(outLns, ln)
case isKey && bytes.HasPrefix(keyStr, start):
inReg = true
slFn = string(keyStr[len(start)+1:])
outLns = sls[slFn]
case isKey && bytes.HasPrefix(keyStr, nohlsl):
inReg = true
inNoHlsl = true
slFn = string(keyStr[len(nohlsl)+1:])
outLns = sls[slFn]
outLns = append(outLns, ln) // key to include self here
case isKey && bytes.HasPrefix(keyStr, hlsl):
inReg = true
inHlsl = true
slFn = string(keyStr[len(hlsl)+1:])
outLns = sls[slFn]
outLns = append(outLns, ln)
}
}
}
rsls := make(map[string][]byte)
for fn, lns := range sls {
outfn := filepath.Join(*outDir, fn+".go")
olns := [][]byte{}
olns = append(olns, []byte("package main"))
olns = append(olns, []byte(`import "math"`))
olns = append(olns, lns...)
res := bytes.Join(olns, nl)
ioutil.WriteFile(outfn, res, 0644)
cmd := exec.Command("goimports", "-w", fn+".go") // get imports
cmd.Dir, _ = filepath.Abs(*outDir)
out, err := cmd.CombinedOutput()
_ = out
// fmt.Printf("\n################\ngoimports output for: %s\n%s\n", outfn, out)
if err != nil {
log.Println(err)
}
rsls[fn] = bytes.Join(lns, nl)
}
return rsls
}
// ExtractHLSL extracts the HLSL code embedded within .Go files.
// Returns true if HLSL contains a void main( function.
func ExtractHLSL(buf []byte) ([]byte, bool) {
key := []byte("//gosl: ")
hlsl := []byte("hlsl")
nohlsl := []byte("nohlsl")
end := []byte("end")
nl := []byte("\n")
stComment := []byte("/*")
edComment := []byte("*/")
comment := []byte("// ")
pack := []byte("package")
imp := []byte("import")
main := []byte("void main(")
lparen := []byte("(")
rparen := []byte(")")
lines := bytes.Split(buf, nl)
mx := min(10, len(lines))
stln := 0
gotImp := false
for li := 0; li < mx; li++ {
ln := lines[li]
switch {
case bytes.HasPrefix(ln, pack):
stln = li + 1
case bytes.HasPrefix(ln, imp):
if bytes.HasSuffix(ln, lparen) {
gotImp = true
} else {
stln = li + 1
}
case gotImp && bytes.HasPrefix(ln, rparen):
stln = li + 1
}
}
lines = lines[stln:] // get rid of package, import
hasMain := false
inHlsl := false
inNoHlsl := false
noHlslStart := 0
for li := 0; li < len(lines); li++ {
ln := lines[li]
isKey := bytes.HasPrefix(ln, key)
var keyStr []byte
if isKey {
keyStr = ln[len(key):]
// fmt.Printf("key: %s\n", string(keyStr))
}
switch {
case inNoHlsl && isKey && bytes.HasPrefix(keyStr, end):
lines = slices.Delete(lines, noHlslStart, li+1)
li -= ((li + 1) - noHlslStart)
inNoHlsl = false
case inHlsl && isKey && bytes.HasPrefix(keyStr, end):
lines = slices.Delete(lines, li, li+1)
li--
inHlsl = false
case inHlsl:
del := false
switch {
case bytes.HasPrefix(ln, stComment) || bytes.HasPrefix(ln, edComment):
lines = slices.Delete(lines, li, li+1)
li--
del = true
case bytes.HasPrefix(ln, comment):
lines[li] = ln[3:]
}
if !del {
if bytes.HasPrefix(lines[li], main) {
hasMain = true
}
}
case isKey && bytes.HasPrefix(keyStr, hlsl):
inHlsl = true
lines = slices.Delete(lines, li, li+1)
li--
case isKey && bytes.HasPrefix(keyStr, nohlsl):
inNoHlsl = true
noHlslStart = li
}
}
return bytes.Join(lines, nl), hasMain
}