Code Translator
Prerequisites & Dependencies
- Toolchains of both source and target languages (compilers/interpreters, formatters)
- An executable test suite or reference inputs/outputs to prove behavioral parity after translation
- Standard library / dependency mapping notes (e.g., source packages → target equivalents)
Execution Steps
- Read the source fully and record observable behavior: inputs, outputs, side effects, error semantics, and edge cases (null/empty/overflow handling).
- Build a dependency map: for each library call, select the target-language equivalent; for missing equivalents, plan a shim or wrapper.
- Translate in order: type signatures → function bodies → control flow, preferring idiomatic target constructs (e.g., errors-as-values in Go, exceptions in Python,
Result in Rust) over literal word-for-word substitution.
- Preserve algorithmic complexity and memory semantics; note intentionally diverged locations (integer division, string encoding, hash ordering).
- Port the test suite first, or generate golden tests from source behavior; then run against the translation and diff the outputs.
- Run the target language's formatter/linter and document migration notes: changed APIs, dropped features, replacement libraries needed.
# source (Python)
def top_scores(entries: list[tuple[str, int]], n: int) -> list[str]:
return [name for name, _ in sorted(entries, key=lambda e: -e[1])[:n]]
// target (Go)
type Entry struct { Name string; Score int }
func TopScores(entries []Entry, n int) []string {
sort.SliceStable(entries, func(i, j int) bool { return entries[i].Score > entries[j].Score })
if n > len(entries) { n = len(entries) }
names := make([]string, 0, n)
for _, e := range entries[:n] { names = append(names, e.Name) }
return names
}
1---2name: code-translator3description: Precisely convert code logic from one programming language to another.4---56# Code Translator78## Prerequisites & Dependencies9- Toolchains of both source and target languages (compilers/interpreters, formatters)10- An executable test suite or reference inputs/outputs to prove behavioral parity after translation11- Standard library / dependency mapping notes (e.g., source packages → target equivalents)1213## Execution Steps141. Read the source fully and record observable behavior: inputs, outputs, side effects, error semantics, and edge cases (null/empty/overflow handling).152. Build a dependency map: for each library call, select the target-language equivalent; for missing equivalents, plan a shim or wrapper.163. Translate in order: type signatures → function bodies → control flow, preferring idiomatic target constructs (e.g., errors-as-values in Go, exceptions in Python, `Result` in Rust) over literal word-for-word substitution.174. Preserve algorithmic complexity and memory semantics; note intentionally diverged locations (integer division, string encoding, hash ordering).185. Port the test suite first, or generate golden tests from source behavior; then run against the translation and diff the outputs.196. Run the target language's formatter/linter and document migration notes: changed APIs, dropped features, replacement libraries needed.2021```python22# source (Python)23def top_scores(entries: list[tuple[str, int]], n: int) -> list[str]:24 return [name for name, _ in sorted(entries, key=lambda e: -e[1])[:n]]25```2627```go28// target (Go)29type Entry struct { Name string; Score int }3031func TopScores(entries []Entry, n int) []string {32 sort.SliceStable(entries, func(i, j int) bool { return entries[i].Score > entries[j].Score })33 if n > len(entries) { n = len(entries) }34 names := make([]string, 0, n)35 for _, e := range entries[:n] { names = append(names, e.Name) }36 return names37}38```39```