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},
|
"cap": &Symbol{kind: Bltn, builtin: _cap},
|
||||||
"close": &Symbol{kind: Bltn, builtin: _close},
|
"close": &Symbol{kind: Bltn, builtin: _close},
|
||||||
"copy": &Symbol{kind: Bltn, builtin: _copy},
|
"copy": &Symbol{kind: Bltn, builtin: _copy},
|
||||||
|
"delete": &Symbol{kind: Bltn, builtin: _delete},
|
||||||
"len": &Symbol{kind: Bltn, builtin: _len},
|
"len": &Symbol{kind: Bltn, builtin: _len},
|
||||||
"make": &Symbol{kind: Bltn, builtin: _make},
|
"make": &Symbol{kind: Bltn, builtin: _make},
|
||||||
"new": &Symbol{kind: Bltn, builtin: _new},
|
"new": &Symbol{kind: Bltn, builtin: _new},
|
||||||
|
|||||||
@@ -1899,6 +1899,29 @@ func main() {
|
|||||||
// hello
|
// 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() {
|
func Example_export0() {
|
||||||
src := `
|
src := `
|
||||||
package main
|
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) {
|
func _len(n *Node) {
|
||||||
i := n.findex
|
i := n.findex
|
||||||
value := genValue(n.child[1])
|
value := genValue(n.child[1])
|
||||||
|
|||||||
Reference in New Issue
Block a user