Now add logs again
This commit is contained in:
26
main.go
26
main.go
@@ -80,20 +80,25 @@ func main() {
|
||||
|
||||
// buildLuaScript creates a complete Lua script from the expression
|
||||
func buildLuaScript(luaExpr string) string {
|
||||
log.Printf("Building Lua script from expression: %s", luaExpr)
|
||||
|
||||
// Check if the expression needs v1 to be prepended
|
||||
if strings.HasPrefix(luaExpr, "*") || strings.HasPrefix(luaExpr, "/") ||
|
||||
strings.HasPrefix(luaExpr, "+") || strings.HasPrefix(luaExpr, "-") ||
|
||||
strings.HasPrefix(luaExpr, "^") || strings.HasPrefix(luaExpr, "%") {
|
||||
luaExpr = "v1" + luaExpr
|
||||
log.Printf("Prepended 'v1' to expression, new expression: %s", luaExpr)
|
||||
} else if strings.HasPrefix(luaExpr, "=") {
|
||||
// Handle direct assignment with = operator
|
||||
luaExpr = "v1 " + luaExpr
|
||||
log.Printf("Handled direct assignment, new expression: %s", luaExpr)
|
||||
}
|
||||
|
||||
// Replace shorthand v1, v2, etc. with their direct variable names (no need for array notation)
|
||||
// Note: we're keeping this regex in case the user writes v[1] style, but we'll convert it to v1
|
||||
shorthandRegex := regexp.MustCompile(`\bv\[(\d+)\]\b`)
|
||||
luaExpr = shorthandRegex.ReplaceAllString(luaExpr, "v$1")
|
||||
log.Printf("Converted shorthand variables, new expression: %s", luaExpr)
|
||||
|
||||
// Add custom script header with helper functions
|
||||
scriptHeader := `
|
||||
@@ -116,6 +121,7 @@ function process(...)
|
||||
return v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12
|
||||
end
|
||||
`, scriptHeader, luaExpr)
|
||||
log.Println("Generated Lua script with assignments.")
|
||||
} else {
|
||||
// Simple expression, assign to v1
|
||||
fullScript = fmt.Sprintf(`%s
|
||||
@@ -125,29 +131,38 @@ function process(...)
|
||||
return v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12
|
||||
end
|
||||
`, scriptHeader, luaExpr)
|
||||
log.Println("Generated Lua script for simple expression.")
|
||||
}
|
||||
|
||||
log.Printf("Final Lua script: %s", fullScript)
|
||||
return fullScript
|
||||
}
|
||||
|
||||
func processFile(filename string, pattern *regexp.Regexp, luaScript string) error {
|
||||
fullPath := filepath.Join(".", filename)
|
||||
log.Printf("Processing file: %s", fullPath)
|
||||
|
||||
content, err := os.ReadFile(fullPath)
|
||||
if err != nil {
|
||||
Error.Printf("Error reading file %s: %v", fullPath, err)
|
||||
return fmt.Errorf("error reading file: %v", err)
|
||||
}
|
||||
log.Printf("Successfully read file: %s", fullPath)
|
||||
|
||||
fileContent := string(content)
|
||||
result, err := process(fileContent, pattern, luaScript)
|
||||
if err != nil {
|
||||
Error.Printf("Error processing content of file %s: %v", fullPath, err)
|
||||
return err
|
||||
}
|
||||
log.Printf("Successfully processed content of file: %s", fullPath)
|
||||
|
||||
err = os.WriteFile(fullPath, []byte(result), 0644)
|
||||
if err != nil {
|
||||
Error.Printf("Error writing file %s: %v", fullPath, err)
|
||||
return fmt.Errorf("error writing file: %v", err)
|
||||
}
|
||||
log.Printf("Successfully wrote modified content to file: %s", fullPath)
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -160,19 +175,24 @@ func process(data string, pattern *regexp.Regexp, luaScript string) (string, err
|
||||
L.Push(L.GetGlobal("require"))
|
||||
L.Push(lua.LString("math"))
|
||||
if err := L.PCall(1, 1, nil); err != nil {
|
||||
Error.Printf("Error loading Lua math library: %v", err)
|
||||
return data, fmt.Errorf("error loading Lua math library: %v", err)
|
||||
}
|
||||
log.Println("Lua math library loaded successfully.")
|
||||
|
||||
// Load the Lua script
|
||||
if err := L.DoString(luaScript); err != nil {
|
||||
Error.Printf("Error in Lua script: %v", err)
|
||||
return data, fmt.Errorf("error in Lua script: %v", err)
|
||||
}
|
||||
log.Println("Lua script executed successfully.")
|
||||
|
||||
// Process all regex matches
|
||||
result := pattern.ReplaceAllStringFunc(data, func(match string) string {
|
||||
captures := pattern.FindStringSubmatch(match)
|
||||
if len(captures) <= 1 {
|
||||
// No capture groups, return unchanged
|
||||
log.Printf("No capture groups found for match: %s", match)
|
||||
return match
|
||||
}
|
||||
|
||||
@@ -182,8 +202,10 @@ func process(data string, pattern *regexp.Regexp, luaScript string) (string, err
|
||||
floatVal, err := strconv.ParseFloat(capture, 64)
|
||||
if err == nil {
|
||||
L.SetGlobal(fmt.Sprintf("v%d", i+1), lua.LNumber(floatVal))
|
||||
log.Printf("Set v%d to %f", i+1, floatVal)
|
||||
} else {
|
||||
L.SetGlobal(fmt.Sprintf("v%d", i+1), lua.LString(capture))
|
||||
log.Printf("Set v%d to %s", i+1, capture)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -193,6 +215,7 @@ func process(data string, pattern *regexp.Regexp, luaScript string) (string, err
|
||||
Error.Printf("Error executing Lua script: %v", err)
|
||||
return match // Return unchanged on error
|
||||
}
|
||||
log.Println("Lua function 'process' called successfully.")
|
||||
|
||||
// Get the return values (up to 12)
|
||||
returnValues := make([]lua.LValue, 12)
|
||||
@@ -211,6 +234,7 @@ func process(data string, pattern *regexp.Regexp, luaScript string) (string, err
|
||||
result := match
|
||||
for i := 0; i < len(captures)-1 && i < 12; i++ {
|
||||
if returnValues[i] == lua.LNil {
|
||||
log.Printf("Return value v%d is nil, skipping replacement.", i+1)
|
||||
continue // Skip if nil return
|
||||
}
|
||||
|
||||
@@ -228,6 +252,7 @@ func process(data string, pattern *regexp.Regexp, luaScript string) (string, err
|
||||
|
||||
// If we have a value, replace it
|
||||
if newVal != "" {
|
||||
log.Printf("Replacing %s with %s", oldVal, newVal)
|
||||
result = strings.Replace(result, oldVal, newVal, 1)
|
||||
}
|
||||
}
|
||||
@@ -235,5 +260,6 @@ func process(data string, pattern *regexp.Regexp, luaScript string) (string, err
|
||||
return result
|
||||
})
|
||||
|
||||
log.Println("Processing completed successfully.")
|
||||
return result, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user