Commit 5f2572f1 authored by Dylan Trotter's avatar Dylan Trotter

Implement list.reverse.

parent b308355b
...@@ -304,6 +304,21 @@ func listRepr(f *Frame, o *Object) (*Object, *BaseException) { ...@@ -304,6 +304,21 @@ func listRepr(f *Frame, o *Object) (*Object, *BaseException) {
return NewStr(fmt.Sprintf("[%s]", repr)).ToObject(), nil return NewStr(fmt.Sprintf("[%s]", repr)).ToObject(), nil
} }
func listReverse(f *Frame, args Args, _ KWArgs) (*Object, *BaseException) {
if raised := checkMethodArgs(f, "reverse", args, ListType); raised != nil {
return nil, raised
}
l := toListUnsafe(args[0])
l.mutex.Lock()
halfLen := len(l.elems) / 2
for i := 0; i < halfLen; i++ {
j := len(l.elems) - i - 1
l.elems[i], l.elems[j] = l.elems[j], l.elems[i]
}
l.mutex.Unlock()
return None, nil
}
func listSetItem(f *Frame, o, key, value *Object) *BaseException { func listSetItem(f *Frame, o, key, value *Object) *BaseException {
l := toListUnsafe(o) l := toListUnsafe(o)
if key.typ.slots.Int != nil { if key.typ.slots.Int != nil {
...@@ -322,6 +337,7 @@ func listSetItem(f *Frame, o, key, value *Object) *BaseException { ...@@ -322,6 +337,7 @@ func listSetItem(f *Frame, o, key, value *Object) *BaseException {
func initListType(dict map[string]*Object) { func initListType(dict map[string]*Object) {
dict["append"] = newBuiltinFunction("append", listAppend).ToObject() dict["append"] = newBuiltinFunction("append", listAppend).ToObject()
dict["insert"] = newBuiltinFunction("insert", listInsert).ToObject() dict["insert"] = newBuiltinFunction("insert", listInsert).ToObject()
dict["reverse"] = newBuiltinFunction("reverse", listReverse).ToObject()
ListType.slots.Add = &binaryOpSlot{listAdd} ListType.slots.Add = &binaryOpSlot{listAdd}
ListType.slots.Contains = &binaryOpSlot{listContains} ListType.slots.Contains = &binaryOpSlot{listContains}
ListType.slots.Eq = &binaryOpSlot{listEq} ListType.slots.Eq = &binaryOpSlot{listEq}
......
...@@ -246,6 +246,27 @@ func TestListNew(t *testing.T) { ...@@ -246,6 +246,27 @@ func TestListNew(t *testing.T) {
} }
} }
func TestListReverse(t *testing.T) {
reverse := mustNotRaise(GetAttr(newFrame(nil), ListType.ToObject(), NewStr("reverse"), nil))
fun := wrapFuncForTest(func(f *Frame, o *Object, args ...*Object) (*Object, *BaseException) {
_, raised := reverse.Call(f, append(Args{o}, args...), nil)
if raised != nil {
return nil, raised
}
return o, nil
})
cases := []invokeTestCase{
{args: wrapArgs(NewList()), want: NewList().ToObject()},
{args: wrapArgs(newTestList(1, 2, 3)), want: newTestList(3, 2, 1).ToObject()},
{args: wrapArgs(NewList(), 123), wantExc: mustCreateException(TypeErrorType, "'reverse' of 'list' requires 1 arguments")},
}
for _, cas := range cases {
if err := runInvokeTestCase(fun, &cas); err != "" {
t.Error(err)
}
}
}
func TestListStrRepr(t *testing.T) { func TestListStrRepr(t *testing.T) {
recursiveList := newTestList("foo").ToObject() recursiveList := newTestList("foo").ToObject()
listAppend(newFrame(nil), []*Object{recursiveList, recursiveList}, nil) listAppend(newFrame(nil), []*Object{recursiveList, recursiveList}, nil)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment