Fix the god damn shorthands

This commit is contained in:
2025-03-22 03:56:37 +01:00
parent 16b6156cd3
commit 0c56724429
2 changed files with 18 additions and 2 deletions

18
main.go
View File

@@ -55,16 +55,21 @@ func main() {
luaExpr := args[1] luaExpr := args[1]
files := args[2:] files := args[2:]
log.Printf("Parsed arguments: regexPattern=%s, luaExpr=%s, files=%v", regexPattern, luaExpr, files)
// Prepare the Lua expression // Prepare the Lua expression
luaExpr = buildLuaScript(luaExpr) luaExpr = buildLuaScript(luaExpr)
log.Printf("Built Lua expression: %s", luaExpr)
if strings.Contains(regexPattern, "!num") { if strings.Contains(regexPattern, "!num") {
regexPattern = strings.ReplaceAll(regexPattern, "!num", "(\\d*\\.?\\d+)") regexPattern = strings.ReplaceAll(regexPattern, "!num", "(\\d*\\.?\\d+)")
log.Printf("Updated regexPattern after replacing !num: %s", regexPattern)
} }
// Make sure the regex can match across multiple lines by adding (?s) flag // Make sure the regex can match across multiple lines by adding (?s) flag
if !strings.HasPrefix(regexPattern, "(?s)") { if !strings.HasPrefix(regexPattern, "(?s)") {
regexPattern = "(?s)" + regexPattern regexPattern = "(?s)" + regexPattern
log.Printf("Updated regexPattern to match across multiple lines: %s", regexPattern)
} }
// Compile the pattern for file processing // Compile the pattern for file processing
@@ -76,6 +81,7 @@ func main() {
// Process each file // Process each file
for _, file := range files { for _, file := range files {
log.Printf("Processing file: %s", file)
err := processFile(file, pattern, luaExpr) err := processFile(file, pattern, luaExpr)
if err != nil { if err != nil {
Error.Printf("Error processing file %s: %v", file, err) Error.Printf("Error processing file %s: %v", file, err)
@@ -85,7 +91,6 @@ func main() {
// buildLuaScript creates a complete Lua script from the expression // buildLuaScript creates a complete Lua script from the expression
func buildLuaScript(luaExpr string) string { func buildLuaScript(luaExpr string) string {
// Check if the expression starts with an operator that needs special handling
if strings.HasPrefix(luaExpr, "*") { if strings.HasPrefix(luaExpr, "*") {
luaExpr = "v1 = v1" + luaExpr luaExpr = "v1 = v1" + luaExpr
} else if strings.HasPrefix(luaExpr, "/") { } else if strings.HasPrefix(luaExpr, "/") {
@@ -102,11 +107,16 @@ func buildLuaScript(luaExpr string) string {
// Handle direct assignment with = operator // Handle direct assignment with = operator
luaExpr = "v1 " + luaExpr luaExpr = "v1 " + luaExpr
} }
// Check if the expression starts with an operator that needs special handling
if !strings.Contains(luaExpr, "=") {
luaExpr = "v1 = " + luaExpr
}
// Replace shorthand v1, v2, etc. with their direct variable names // Replace shorthand v1, v2, etc. with their direct variable names
shorthandRegex := regexp.MustCompile(`\bv\[(\d+)\]\b`) shorthandRegex := regexp.MustCompile(`\bv\[(\d+)\]\b`)
luaExpr = shorthandRegex.ReplaceAllString(luaExpr, "v$1") luaExpr = shorthandRegex.ReplaceAllString(luaExpr, "v$1")
log.Printf("Final Lua expression after processing: %s", luaExpr)
return luaExpr return luaExpr
} }
@@ -170,6 +180,7 @@ function ceil(x) return math.ceil(x) end
captures := pattern.FindStringSubmatch(match) captures := pattern.FindStringSubmatch(match)
if len(captures) <= 1 { if len(captures) <= 1 {
// No capture groups, return unchanged // No capture groups, return unchanged
log.Printf("No capture groups found for match: %s", match)
return match return match
} }
@@ -179,8 +190,10 @@ function ceil(x) return math.ceil(x) end
floatVal, err := strconv.ParseFloat(capture, 64) floatVal, err := strconv.ParseFloat(capture, 64)
if err == nil { if err == nil {
L.SetGlobal(fmt.Sprintf("v%d", i+1), lua.LNumber(floatVal)) L.SetGlobal(fmt.Sprintf("v%d", i+1), lua.LNumber(floatVal))
log.Printf("Set global v%d to float value: %f", i+1, floatVal)
} else { } else {
L.SetGlobal(fmt.Sprintf("v%d", i+1), lua.LString(capture)) L.SetGlobal(fmt.Sprintf("v%d", i+1), lua.LString(capture))
log.Printf("Set global v%d to string value: %s", i+1, capture)
} }
} }
@@ -195,12 +208,14 @@ function ceil(x) return math.ceil(x) end
for i := 0; i < 12; i++ { for i := 0; i < 12; i++ {
varName := fmt.Sprintf("v%d", i+1) varName := fmt.Sprintf("v%d", i+1)
returnValues[i] = L.GetGlobal(varName) returnValues[i] = L.GetGlobal(varName)
log.Printf("Retrieved value for %s: %v", varName, returnValues[i])
} }
// Replace each capture group with its new value (if available) // Replace each capture group with its new value (if available)
result := match result := match
for i := 0; i < len(captures)-1 && i < 12; i++ { for i := 0; i < len(captures)-1 && i < 12; i++ {
if returnValues[i] == lua.LNil { if returnValues[i] == lua.LNil {
log.Printf("Skipping replacement for %s as it is nil", captures[i+1])
continue // Skip if nil continue // Skip if nil
} }
@@ -218,6 +233,7 @@ function ceil(x) return math.ceil(x) end
// If we have a value, replace it // If we have a value, replace it
if newVal != "" { if newVal != "" {
log.Printf("Replacing %s with %s in match: %s", oldVal, newVal, match)
result = strings.Replace(result, oldVal, newVal, 1) result = strings.Replace(result, oldVal, newVal, 1)
} }
} }

View File

@@ -65,7 +65,7 @@ func TestShorthandNotation(t *testing.T) {
` `
regex := regexp.MustCompile(`(?s)<value>(\d+)</value>`) regex := regexp.MustCompile(`(?s)<value>(\d+)</value>`)
luaExpr := `v1 = v1 * 1.5` // Use direct assignment syntax luaExpr := `v1 * 1.5` // Use direct assignment syntax
luaScript := buildLuaScript(luaExpr) luaScript := buildLuaScript(luaExpr)
modifiedContent, err := process(fileContents, regex, luaScript) modifiedContent, err := process(fileContents, regex, luaScript)