2014-10-24 00:46:05 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/mediocregopher/ginger/seq"
|
|
|
|
"github.com/mediocregopher/ginger/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type mathReduceFn func(types.Elem, types.Elem) types.Elem
|
|
|
|
|
|
|
|
func mathReduce(fn mathReduceFn, zero types.Elem, s seq.Seq) types.Elem {
|
|
|
|
reduceFn := func(acc, el types.Elem) (types.Elem, bool) {
|
|
|
|
return fn(acc, el), false
|
|
|
|
}
|
|
|
|
return seq.Reduce(reduceFn, zero, s)
|
|
|
|
}
|
2014-10-24 01:00:07 +00:00
|
|
|
{{range .}}{{$mathOp := .}}{{range .OpTypes}}
|
2014-10-24 00:46:05 +00:00
|
|
|
func {{$mathOp.Private}}{{.CastFn}}(a, b types.Elem) types.Elem {
|
2014-10-24 01:00:07 +00:00
|
|
|
return types.GoType{{`{`}}{{.CastFn}}(a) {{$mathOp.Op}} {{.CastFn}}(b)}
|
2014-10-24 00:46:05 +00:00
|
|
|
}
|
2014-10-24 01:00:07 +00:00
|
|
|
{{end}}{{if $mathOp.IncludeString}}
|
2014-10-24 00:46:05 +00:00
|
|
|
func {{$mathOp.Private}}String(a, b types.Elem) types.Elem {
|
2014-10-24 01:00:07 +00:00
|
|
|
return types.GoType{String(a) {{$mathOp.Op}} String(b)}
|
2014-10-24 00:46:05 +00:00
|
|
|
}
|
|
|
|
{{end}}
|
|
|
|
func {{$mathOp.Public}}(s seq.Seq) types.Elem {
|
|
|
|
var first, zero types.Elem
|
2014-10-24 01:00:07 +00:00
|
|
|
{{if (eq $mathOp.Unit "")}}
|
2014-10-24 00:46:05 +00:00
|
|
|
if seq.Empty(s) {
|
|
|
|
panic("{{$mathOp.Public}} cannot be called with no arguments")
|
|
|
|
}
|
|
|
|
zero, s, _ = s.FirstRest()
|
|
|
|
first = zero
|
2014-10-24 01:00:07 +00:00
|
|
|
{{else}}
|
2014-10-24 00:46:05 +00:00
|
|
|
if seq.Empty(s) {
|
2014-10-24 01:00:07 +00:00
|
|
|
return types.GoType{{`{`}}{{$mathOp.Unit}}}
|
2014-10-24 00:46:05 +00:00
|
|
|
}
|
|
|
|
first, _, _ = s.FirstRest()
|
2014-10-24 01:00:07 +00:00
|
|
|
{{end}}
|
2014-10-24 00:46:05 +00:00
|
|
|
var fn mathReduceFn
|
|
|
|
switch first.(types.GoType).V.(type) {
|
2014-10-24 01:00:07 +00:00
|
|
|
{{range .OpTypes}}
|
2014-10-24 00:46:05 +00:00
|
|
|
case {{.Type}}:
|
2014-10-24 01:00:07 +00:00
|
|
|
fn = {{$mathOp.Private}}{{.CastFn}}{{if (ne $mathOp.Unit "")}}
|
|
|
|
zero = types.GoType{{`{`}}{{.Type}}({{$mathOp.Unit}})}{{end}}
|
|
|
|
{{end}}{{if $mathOp.IncludeString}}
|
2014-10-24 00:46:05 +00:00
|
|
|
case string:
|
|
|
|
fn = {{$mathOp.Private}}String
|
|
|
|
zero = types.GoType{string("")}
|
2014-10-24 01:00:07 +00:00
|
|
|
{{end}}
|
2014-10-24 00:46:05 +00:00
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("$#v cannot have {{$mathOp.Public}} called on it", first))
|
|
|
|
}
|
|
|
|
|
|
|
|
return mathReduce(fn, zero, s)
|
2014-10-24 01:00:07 +00:00
|
|
|
}{{end}}
|