Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
Pyston
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
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
Pyston
Commits
e43a3743
Commit
e43a3743
authored
Nov 19, 2014
by
Joris Vankerschaver
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add tuple repeat.
parent
2f601718
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
48 additions
and
0 deletions
+48
-0
src/runtime/tuple.cpp
src/runtime/tuple.cpp
+27
-0
test/tests/tuples.py
test/tests/tuples.py
+21
-0
No files found.
src/runtime/tuple.cpp
View file @
e43a3743
...
...
@@ -14,6 +14,7 @@
#include "runtime/tuple.h"
#include <algorithm>
#include <sstream>
#include "core/ast.h"
...
...
@@ -127,6 +128,30 @@ Box* tupleAdd(BoxedTuple* self, Box* rhs) {
return
new
BoxedTuple
(
std
::
move
(
velts
));
}
Box
*
tupleMul
(
BoxedTuple
*
self
,
Box
*
rhs
)
{
if
(
rhs
->
cls
!=
int_cls
)
{
raiseExcHelper
(
TypeError
,
"can't multiply sequence by non-int of type '%s'"
,
getTypeName
(
rhs
)
->
c_str
());
}
int
n
=
static_cast
<
BoxedInt
*>
(
rhs
)
->
n
;
int
s
=
self
->
elts
.
size
();
if
(
n
<
0
)
n
=
0
;
if
(
s
==
0
||
n
==
1
)
{
return
self
;
}
else
{
BoxedTuple
::
GCVector
velts
(
n
*
s
);
auto
iter
=
velts
.
begin
();
for
(
int
i
=
0
;
i
<
n
;
++
i
)
{
std
::
copy
(
self
->
elts
.
begin
(),
self
->
elts
.
end
(),
iter
);
iter
+=
s
;
}
return
new
BoxedTuple
(
std
::
move
(
velts
));
}
}
Box
*
tupleLen
(
BoxedTuple
*
t
)
{
assert
(
t
->
cls
==
tuple_cls
);
return
boxInt
(
t
->
elts
.
size
());
...
...
@@ -389,6 +414,8 @@ void setupTuple() {
tuple_cls
->
giveAttr
(
"__repr__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
tupleRepr
,
STR
,
1
)));
tuple_cls
->
giveAttr
(
"__str__"
,
tuple_cls
->
getattr
(
"__repr__"
));
tuple_cls
->
giveAttr
(
"__add__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
tupleAdd
,
BOXED_TUPLE
,
2
)));
tuple_cls
->
giveAttr
(
"__mul__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
tupleMul
,
BOXED_TUPLE
,
2
)));
tuple_cls
->
giveAttr
(
"__rmul__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
tupleMul
,
BOXED_TUPLE
,
2
)));
tuple_cls
->
freeze
();
...
...
test/tests/tuples.py
View file @
e43a3743
...
...
@@ -177,3 +177,24 @@ print bool((0,))
print
bool
((
0
,
0
))
print
(
65
,
(
1
,
2
,
3
),
65
)
# Multiplying a tuple by an integer
x
=
(
1
,
2
,
3
)
print
x
*
-
1
print
x
*
0
print
x
*
1
print
x
*
5
print
-
1
*
x
print
0
*
x
print
1
*
x
print
5
*
x
x
=
()
print
x
*
-
1
print
x
*
0
print
x
*
1
print
x
*
5
print
-
1
*
x
print
0
*
x
print
1
*
x
print
5
*
x
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