Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
grumpy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
grumpy
Commits
5f2572f1
Commit
5f2572f1
authored
Dec 24, 2016
by
Dylan Trotter
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement list.reverse.
parent
b308355b
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
0 deletions
+37
-0
runtime/list.go
runtime/list.go
+16
-0
runtime/list_test.go
runtime/list_test.go
+21
-0
No files found.
runtime/list.go
View file @
5f2572f1
...
...
@@ -304,6 +304,21 @@ func listRepr(f *Frame, o *Object) (*Object, *BaseException) {
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
{
l
:=
toListUnsafe
(
o
)
if
key
.
typ
.
slots
.
Int
!=
nil
{
...
...
@@ -322,6 +337,7 @@ func listSetItem(f *Frame, o, key, value *Object) *BaseException {
func
initListType
(
dict
map
[
string
]
*
Object
)
{
dict
[
"append"
]
=
newBuiltinFunction
(
"append"
,
listAppend
)
.
ToObject
()
dict
[
"insert"
]
=
newBuiltinFunction
(
"insert"
,
listInsert
)
.
ToObject
()
dict
[
"reverse"
]
=
newBuiltinFunction
(
"reverse"
,
listReverse
)
.
ToObject
()
ListType
.
slots
.
Add
=
&
binaryOpSlot
{
listAdd
}
ListType
.
slots
.
Contains
=
&
binaryOpSlot
{
listContains
}
ListType
.
slots
.
Eq
=
&
binaryOpSlot
{
listEq
}
...
...
runtime/list_test.go
View file @
5f2572f1
...
...
@@ -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
)
{
recursiveList
:=
newTestList
(
"foo"
)
.
ToObject
()
listAppend
(
newFrame
(
nil
),
[]
*
Object
{
recursiveList
,
recursiveList
},
nil
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment