feat: add support for builtin delete (#127)
This commit is contained in:
committed by
Ludovic Fernandez
parent
b8927e3ca6
commit
70fab221de
12
_test/delete0.go
Normal file
12
_test/delete0.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
a := map[string]int{"hello": 1, "world": 3}
|
||||
delete(a, "hello")
|
||||
fmt.Println(a)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// map[world:3]
|
||||
@@ -155,6 +155,7 @@ func initUniverse() *Scope {
|
||||
"cap": &Symbol{kind: Bltn, builtin: _cap},
|
||||
"close": &Symbol{kind: Bltn, builtin: _close},
|
||||
"copy": &Symbol{kind: Bltn, builtin: _copy},
|
||||
"delete": &Symbol{kind: Bltn, builtin: _delete},
|
||||
"len": &Symbol{kind: Bltn, builtin: _len},
|
||||
"make": &Symbol{kind: Bltn, builtin: _make},
|
||||
"new": &Symbol{kind: Bltn, builtin: _new},
|
||||
|
||||
@@ -1899,6 +1899,29 @@ func main() {
|
||||
// hello
|
||||
}
|
||||
|
||||
func Example_delete0() {
|
||||
src := `
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
a := map[string]int{"hello": 1, "world": 3}
|
||||
delete(a, "hello")
|
||||
fmt.Println(a)
|
||||
}
|
||||
`
|
||||
i := interp.New(interp.Opt{Entry: "main"})
|
||||
i.Use(stdlib.Value)
|
||||
_, err := i.Eval(src)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// map[world:3]
|
||||
}
|
||||
|
||||
func Example_export0() {
|
||||
src := `
|
||||
package main
|
||||
|
||||
@@ -1322,6 +1322,18 @@ func _close(n *Node) {
|
||||
}
|
||||
}
|
||||
|
||||
func _delete(n *Node) {
|
||||
value0 := genValue(n.child[1]) // map
|
||||
value1 := genValue(n.child[2]) // key
|
||||
next := getExec(n.tnext)
|
||||
var z reflect.Value
|
||||
|
||||
n.exec = func(f *Frame) Builtin {
|
||||
value0(f).SetMapIndex(value1(f), z)
|
||||
return next
|
||||
}
|
||||
}
|
||||
|
||||
func _len(n *Node) {
|
||||
i := n.findex
|
||||
value := genValue(n.child[1])
|
||||
|
||||
Reference in New Issue
Block a user