-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
153 lines (125 loc) · 3.61 KB
/
main.go
File metadata and controls
153 lines (125 loc) · 3.61 KB
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
package main
import (
"bufio"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
)
func indent(level int) string {
return strings.Repeat(" ", level)
}
// Track variable assignment mappings
var varExprs = make(map[string]string)
func isAssignment(line string) (name string, value string, ok bool) {
if strings.Contains(line, " be ") {
parts := strings.Split(line, " be ")
if len(parts) == 2 {
return parts[0], parts[1], true
}
}
return "", "", false
}
func convertFString(content string) (string, []string) {
vars := []string{}
// Replace {var} with {}, and collect var names
formatted := regexp.MustCompile(`{([^}]+)}`).ReplaceAllStringFunc(content, func(m string) string {
varName := m[1 : len(m)-1]
vars = append(vars, varName)
return "{}"
})
return formatted, vars
}
func handleFString(line string) string {
// Handle: x = f"{n}"
assignRe := regexp.MustCompile(`(\w+)\s*=\s*f\"{(\w+)}\"`)
if matches := assignRe.FindStringSubmatch(line); len(matches) == 3 {
varName, expr := matches[1], matches[2]
varExprs[varName] = expr
return fmt.Sprintf("let %s = format!(\"{}\", %s);", varName, expr)
}
// Handle: println!(f"Hello {n}")
fStrRe := regexp.MustCompile(`f\"([^\"]*)\"`)
matches := fStrRe.FindStringSubmatch(line)
if len(matches) < 2 {
return line
}
content := matches[1]
formatted, vars := convertFString(content)
formatCall := fmt.Sprintf("format!(\"%s\"%s)", formatted, ifNotEmpty(strings.Join(vars, ", ")))
if strings.HasPrefix(line, "println!(") {
return fmt.Sprintf("println!(\"{}\", %s);", formatCall)
}
// General fallback: replace f-string with format!(...)
return strings.Replace(line, matches[0], formatCall, 1)
}
func ifNotEmpty(s string) string {
if s == "" {
return ""
}
return ", " + s
}
func translateLine(line string) string {
line = strings.TrimSpace(line)
// Handle: println!(x) → println!("{}", x);
printVarRe := regexp.MustCompile(`^println!\((\w+)\)$`)
if matches := printVarRe.FindStringSubmatch(line); len(matches) == 2 {
varName := matches[1]
return fmt.Sprintf("println!(\"{}\", %s);", varName)
}
if strings.Contains(line, `f"`) || strings.HasPrefix(line, "print(f") {
line = handleFString(line)
}
if name, value, ok := isAssignment(line); ok {
return fmt.Sprintf("let %s = %s;", name, value)
}
if strings.HasSuffix(line, "()") {
return line + ";"
}
return line
}
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: go run main.go <source-file.lang>")
return
}
inputFile := os.Args[1]
file, err := os.Open(inputFile)
if err != nil {
fmt.Println("Error opening file:", err)
return
}
defer file.Close()
outputFile := strings.TrimSuffix(filepath.Base(inputFile), filepath.Ext(inputFile)) + ".rs"
output, err := os.Create(outputFile)
if err != nil {
fmt.Println("Error creating output file:", err)
return
}
defer output.Close()
scanner := bufio.NewScanner(file)
var rustCode []string
inFunction := false
for scanner.Scan() {
line := scanner.Text()
trimmed := strings.TrimSpace(line)
switch {
case strings.HasSuffix(trimmed, "{") && strings.HasSuffix(strings.Split(trimmed, " ")[0], "()"):
funcName := strings.Split(trimmed, "(")[0]
rustCode = append(rustCode, fmt.Sprintf("fn %s() {", funcName))
inFunction = true
case inFunction && trimmed == "}":
rustCode = append(rustCode, "}")
inFunction = false
case inFunction:
rustCode = append(rustCode, indent(1)+translateLine(trimmed))
default:
rustCode = append(rustCode, translateLine(trimmed))
}
}
for _, line := range rustCode {
fmt.Fprintln(output, line)
}
fmt.Printf("✅ Transpilation complete. Output written to: %s\n", outputFile)
}