feat: add multiple type support for inc, dec (#39)
This commit is contained in:
committed by
Ludovic Fernandez
parent
2fe2e8e754
commit
9c310f12ed
@@ -122,6 +122,40 @@ func {{$name}}Assign(n *Node) {
|
||||
}
|
||||
}
|
||||
{{end}}
|
||||
{{range $name, $op := .IncDec}}
|
||||
func {{$name}}(n *Node) {
|
||||
next := getExec(n.tnext)
|
||||
typ := n.typ.TypeOf()
|
||||
value := genValue(n.child[0])
|
||||
|
||||
switch typ.Kind() {
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
v0 := genValueInt(n.child[0])
|
||||
n.exec = func(f *Frame) Builtin {
|
||||
value(f).SetInt(v0(f) {{$op.Name}} 1)
|
||||
return next
|
||||
}
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||
v0 := genValueUint(n.child[0])
|
||||
n.exec = func(f *Frame) Builtin {
|
||||
value(f).SetUint(v0(f) {{$op.Name}} 1)
|
||||
return next
|
||||
}
|
||||
case reflect.Float32, reflect.Float64:
|
||||
v0 := genValueFloat(n.child[0])
|
||||
n.exec = func(f *Frame) Builtin {
|
||||
value(f).SetFloat(v0(f) {{$op.Name}} 1)
|
||||
return next
|
||||
}
|
||||
case reflect.Complex64, reflect.Complex128:
|
||||
v0 := genValue(n.child[0])
|
||||
n.exec = func(f *Frame) Builtin {
|
||||
value(f).SetComplex(v0(f).Complex() {{$op.Name}} 1)
|
||||
return next
|
||||
}
|
||||
}
|
||||
}
|
||||
{{end}}
|
||||
`
|
||||
|
||||
// Op define operator name and properties
|
||||
@@ -154,6 +188,10 @@ func main() {
|
||||
"xor": {"^", false, false, false},
|
||||
"andnot": {"&^", false, false, false},
|
||||
},
|
||||
"IncDec": map[string]Op{
|
||||
"inc": {Name: "+"},
|
||||
"dec": {Name: "-"},
|
||||
},
|
||||
}
|
||||
if err = parse.Execute(b, data); err != nil {
|
||||
log.Fatal(err)
|
||||
|
||||
66
interp/op.go
66
interp/op.go
@@ -639,3 +639,69 @@ func xorAssign(n *Node) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func dec(n *Node) {
|
||||
next := getExec(n.tnext)
|
||||
typ := n.typ.TypeOf()
|
||||
value := genValue(n.child[0])
|
||||
|
||||
switch typ.Kind() {
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
v0 := genValueInt(n.child[0])
|
||||
n.exec = func(f *Frame) Builtin {
|
||||
value(f).SetInt(v0(f) - 1)
|
||||
return next
|
||||
}
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||
v0 := genValueUint(n.child[0])
|
||||
n.exec = func(f *Frame) Builtin {
|
||||
value(f).SetUint(v0(f) - 1)
|
||||
return next
|
||||
}
|
||||
case reflect.Float32, reflect.Float64:
|
||||
v0 := genValueFloat(n.child[0])
|
||||
n.exec = func(f *Frame) Builtin {
|
||||
value(f).SetFloat(v0(f) - 1)
|
||||
return next
|
||||
}
|
||||
case reflect.Complex64, reflect.Complex128:
|
||||
v0 := genValue(n.child[0])
|
||||
n.exec = func(f *Frame) Builtin {
|
||||
value(f).SetComplex(v0(f).Complex() - 1)
|
||||
return next
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func inc(n *Node) {
|
||||
next := getExec(n.tnext)
|
||||
typ := n.typ.TypeOf()
|
||||
value := genValue(n.child[0])
|
||||
|
||||
switch typ.Kind() {
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
v0 := genValueInt(n.child[0])
|
||||
n.exec = func(f *Frame) Builtin {
|
||||
value(f).SetInt(v0(f) + 1)
|
||||
return next
|
||||
}
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||
v0 := genValueUint(n.child[0])
|
||||
n.exec = func(f *Frame) Builtin {
|
||||
value(f).SetUint(v0(f) + 1)
|
||||
return next
|
||||
}
|
||||
case reflect.Float32, reflect.Float64:
|
||||
v0 := genValueFloat(n.child[0])
|
||||
n.exec = func(f *Frame) Builtin {
|
||||
value(f).SetFloat(v0(f) + 1)
|
||||
return next
|
||||
}
|
||||
case reflect.Complex64, reflect.Complex128:
|
||||
v0 := genValue(n.child[0])
|
||||
n.exec = func(f *Frame) Builtin {
|
||||
value(f).SetComplex(v0(f).Complex() + 1)
|
||||
return next
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ var builtin = [...]BuiltinGenerator{
|
||||
Call: call,
|
||||
Case: _case,
|
||||
CompositeLit: arrayLit,
|
||||
Dec: nop,
|
||||
Dec: dec,
|
||||
Defer: _defer,
|
||||
Equal: equal,
|
||||
GetFunc: getFunc,
|
||||
@@ -819,17 +819,6 @@ func notEqual(n *Node) {
|
||||
}
|
||||
}
|
||||
|
||||
func inc(n *Node) {
|
||||
value := genValue(n)
|
||||
value0 := genValue(n.child[0])
|
||||
next := getExec(n.tnext)
|
||||
|
||||
n.exec = func(f *Frame) Builtin {
|
||||
value(f).SetInt(value0(f).Int() + 1)
|
||||
return next
|
||||
}
|
||||
}
|
||||
|
||||
func greater(n *Node) {
|
||||
i := n.findex
|
||||
value0 := genValue(n.child[0])
|
||||
|
||||
Reference in New Issue
Block a user