Skip to content

Commit f570f95

Browse files
committed
vm: tests for comprehensinos, SET_ADD and MAP_ADD opcodes
1 parent 7ac88d9 commit f570f95

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

vm/eval.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ objects so they can be GCed
3333

3434
import (
3535
// "fmt"
36-
"github.com/ncw/gpython/py"
3736
"runtime/debug"
37+
38+
"github.com/ncw/gpython/py"
3839
)
3940

4041
const (
@@ -536,7 +537,9 @@ func do_UNPACK_EX(vm *Vm, counts int32) {
536537
// Calls set.add(TOS1[-i], TOS). Used to implement set comprehensions.
537538
func do_SET_ADD(vm *Vm, i int32) {
538539
defer vm.CheckException()
539-
vm.NotImplemented("SET_ADD", i)
540+
w := vm.POP()
541+
v := vm.PEEK(int(i))
542+
v.(*py.Set).Add(w)
540543
}
541544

542545
// Calls list.append(TOS[-i], TOS). Used to implement list
@@ -553,7 +556,13 @@ func do_LIST_APPEND(vm *Vm, i int32) {
553556
// Calls dict.setitem(TOS1[-i], TOS, TOS1). Used to implement dict comprehensions.
554557
func do_MAP_ADD(vm *Vm, i int32) {
555558
defer vm.CheckException()
556-
vm.NotImplemented("MAP_ADD", i)
559+
key := vm.TOP()
560+
value := vm.SECOND()
561+
vm.DROPN(2)
562+
dict := vm.PEEK(int(i))
563+
// FIXME assert(PyDict_CheckExact(dict));
564+
// err = PyDict_SetItem(map, key, value); /* v[w] = u */
565+
py.SetItem(dict, key, value)
557566
}
558567

559568
// Returns with TOS to the caller of the function.

vm/tests/comprehensions.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env python3.4
2+
3+
# List comprehensions
4+
A = [1,2,3,4]
5+
B = [ 2*a for a in A ]
6+
assert tuple(B) == tuple([2,4,6,8])
7+
B = [ 2*a for a in A if a != 2]
8+
assert tuple(B) == tuple([2,6,8])
9+
10+
# Generator expressions
11+
A = (1,2,3,4)
12+
B = ( 2*a for a in A )
13+
assert tuple(B) == (2,4,6,8)
14+
B = [ 2*a for a in A if a != 2]
15+
assert tuple(B) == (2,6,8)
16+
17+
# Set comprehensions
18+
A = {1,2,3,4}
19+
B = { 2*a for a in A }
20+
assert B == {2,4,6,8}
21+
B = { 2*a for a in A if a != 2}
22+
assert B == {2,6,8}
23+
24+
# Dict comprehensions
25+
A = {"a":1, "b":2, "c":3}
26+
B = { k:k for k in ("a","b","c") }
27+
assert B["b"] == "b"

0 commit comments

Comments
 (0)