Skip to content

Commit 374509b

Browse files
authored
Merge pull request #44 from moeshin/replace-iota
Replace `iota` variable in constant
2 parents 72ed956 + e2ca378 commit 374509b

File tree

2 files changed

+13
-44
lines changed

2 files changed

+13
-44
lines changed

tygo/iota.go

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,17 @@
11
package tygo
22

33
import (
4-
"log"
4+
"regexp"
55
"strconv"
66
"strings"
77
)
88

9-
func isProbablyIotaType(groupType string) bool {
10-
groupType = strings.Trim(groupType, "()")
11-
return groupType == "iota" || strings.HasPrefix(groupType, "iota +") || strings.HasSuffix(groupType, "+ iota")
12-
}
13-
14-
// Note: this could be done so much more elegantly, but this probably covers 99.9% of iota usecases
15-
func basicIotaOffsetValueParse(groupType string) int {
16-
if !isProbablyIotaType(groupType) {
17-
panic("can't parse non-iota type")
18-
}
19-
groupType = strings.Trim(groupType, "()")
20-
if groupType == "iota" {
21-
return 0
22-
}
23-
parts := strings.Split(groupType, " + ")
9+
var iotaRegexp = regexp.MustCompile(`\biota\b`)
2410

25-
var numPart string
26-
if parts[0] == "iota" {
27-
numPart = parts[1]
28-
} else {
29-
numPart = parts[0]
30-
}
11+
func isProbablyIotaType(valueString string) bool {
12+
return !strings.ContainsAny(valueString, "\"'`") && iotaRegexp.MatchString(valueString)
13+
}
3114

32-
addValue, err := strconv.ParseInt(numPart, 10, 64)
33-
if err != nil {
34-
log.Panicf("Failed to guesstimate initial iota value for \"%s\": %v", groupType, err)
35-
}
36-
return int(addValue)
15+
func replaceIotaValue(valueString string, iotaValue int) string {
16+
return iotaRegexp.ReplaceAllLiteralString(valueString, strconv.Itoa(iotaValue))
3717
}

tygo/write_toplevel.go

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ type groupContext struct {
1515
groupValue string
1616
groupType string
1717
iotaValue int
18-
iotaOffset int
1918
}
2019

2120
func (g *PackageGenerator) writeGroupDecl(s *strings.Builder, decl *ast.GenDecl) {
@@ -202,24 +201,14 @@ func (g *PackageGenerator) writeValueSpec(
202201
val := vs.Values[i]
203202
tempSB := &strings.Builder{}
204203
g.writeType(tempSB, val, 0, true)
205-
valueString := tempSB.String()
206-
207-
if isProbablyIotaType(valueString) {
208-
group.iotaOffset = basicIotaOffsetValueParse(valueString)
209-
group.groupValue = "iota"
210-
valueString = fmt.Sprint(group.iotaValue + group.iotaOffset)
211-
} else {
212-
group.groupValue = valueString
213-
}
214-
s.WriteString(valueString)
204+
group.groupValue = tempSB.String()
205+
}
215206

216-
} else { // We must use the previous value or +1 in case of iota
217-
valueString := group.groupValue
218-
if group.groupValue == "iota" {
219-
valueString = fmt.Sprint(group.iotaValue + group.iotaOffset)
220-
}
221-
s.WriteString(valueString)
207+
valueString := group.groupValue
208+
if isProbablyIotaType(valueString) {
209+
valueString = replaceIotaValue(valueString, group.iotaValue)
222210
}
211+
s.WriteString(valueString)
223212

224213
s.WriteByte(';')
225214

0 commit comments

Comments
 (0)