mvdan/goreduce

Reduce Go programs

Go

214

277 commits

updated Mar 19, 2024

See the code

README

goreduce

Reduce a program to its simplest form as long as it produces a compiler error or any output (such as a panic) matching a regular expression.

go get -u mvdan.cc/goreduce

Note that this project isn't being actively developed right now. If you are interested in continuing the work, feel free to fork the repository, or email me to discuss transferring the entire repository over.

Example

func main() {
        a := []int{1, 2, 3}
        if true {
                a = append(a, 4)
        }
        a[1] = -2
        println(a[10])
}
goreduce -match 'index out of range' .
func main() {
        a := []int{}
        println(a[0])
}

For more usage information, see goreduce -h.

Design

  • The tool should be reproducible, giving the same output for an input program as long as external factors don't modify its behavior
  • The rules should be as simple and composable as possible
  • Rules should avoid generating changes that they can know won't compile

Rules

Removing

BeforeAfter
statementa; ba or b
indexa[1]a
slicea[:2]a or a[:]
binary parta + b, a && ba or b
unary op-a, !aa
star*aa
parentheses(a)a
if/elseif a { b } else cb or c
deferdefer f()f()
gogo f()f()
basic value123, "foo"0, ""
composite valueT{a, b}T{}

Inlining

BeforeAfter
constconst c = 0; f(c)f(0)
varv := false; f(v)f(false)
casecase x: aa
block{ a }a
simple callf(){ body }

Resolving

BeforeAfter
integer op2 * 36
string op"foo" + "bar""foobar"
slice"foo"[1:]"oo"
index"foo"[0]'f'
builtinlen("foo")3
go
reduce
reproduce
simplify

Contributors

mvdan

275 commits

AlekSi

1 commits

mvdan/goreduce

Reduce Go programs

Go

214

277 commits

updated Mar 19, 2024

See the code

README

goreduce

Reduce a program to its simplest form as long as it produces a compiler error or any output (such as a panic) matching a regular expression.

go get -u mvdan.cc/goreduce

Note that this project isn't being actively developed right now. If you are interested in continuing the work, feel free to fork the repository, or email me to discuss transferring the entire repository over.

Example

func main() {
        a := []int{1, 2, 3}
        if true {
                a = append(a, 4)
        }
        a[1] = -2
        println(a[10])
}
goreduce -match 'index out of range' .
func main() {
        a := []int{}
        println(a[0])
}

For more usage information, see goreduce -h.

Design

  • The tool should be reproducible, giving the same output for an input program as long as external factors don't modify its behavior
  • The rules should be as simple and composable as possible
  • Rules should avoid generating changes that they can know won't compile

Rules

Removing

BeforeAfter
statementa; ba or b
indexa[1]a
slicea[:2]a or a[:]
binary parta + b, a && ba or b
unary op-a, !aa
star*aa
parentheses(a)a
if/elseif a { b } else cb or c
deferdefer f()f()
gogo f()f()
basic value123, "foo"0, ""
composite valueT{a, b}T{}

Inlining

BeforeAfter
constconst c = 0; f(c)f(0)
varv := false; f(v)f(false)
casecase x: aa
block{ a }a
simple callf(){ body }

Resolving

BeforeAfter
integer op2 * 36
string op"foo" + "bar""foobar"
slice"foo"[1:]"oo"
index"foo"[0]'f'
builtinlen("foo")3
go
reduce
reproduce
simplify

Contributors

mvdan

275 commits

AlekSi

1 commits

Languages

Go

100.0%