https://github.com/bouk/gotre
Go Tail Recursion Eliminator
https://github.com/bouk/gotre
Last synced: over 1 year ago
JSON representation
Go Tail Recursion Eliminator
- Host: GitHub
- URL: https://github.com/bouk/gotre
- Owner: bouk
- Created: 2015-02-25T18:35:25.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2015-02-25T18:36:18.000Z (over 11 years ago)
- Last Synced: 2025-01-23T01:11:24.316Z (over 1 year ago)
- Language: Go
- Homepage:
- Size: 117 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# gotre: Golang Tail Recursion Eliminator
Gotre takes a `.go` files and rewrites any functions that do tail recursion to prevent allocating unneeded stackframes. The go compiler currently doesn't do this automatically.
## Example
```go
// cat fib.go
package fib
func fibRecurse(a, b, n int) int {
if n <= 0 {
return a
}
return fibRecurse(b, a+b, n-1)
}
func Fib(n int) int {
return fibRecurse(0, 1, n)
}
// ./gotre test/fib.go
package fib
func fibRecurse(a, b, n int) int {
__tre__:
if n <= 0 {
return a
}
a, b, n = b, a+b, n-1
goto __tre__
}
func Fib(n int) int {
return fibRecurse(0, 1, n)
}
```