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
f2f26155
Commit
f2f26155
authored
Mar 26, 2017
by
Long Ang
Committed by
Dylan Trotter
Apr 04, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
implement a simple builtin sum
parent
f943e9e0
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
42 additions
and
0 deletions
+42
-0
runtime/builtin_types.go
runtime/builtin_types.go
+30
-0
runtime/builtin_types_test.go
runtime/builtin_types_test.go
+12
-0
No files found.
runtime/builtin_types.go
View file @
f2f26155
...
@@ -674,6 +674,35 @@ func builtinSorted(f *Frame, args Args, _ KWArgs) (*Object, *BaseException) {
...
@@ -674,6 +674,35 @@ func builtinSorted(f *Frame, args Args, _ KWArgs) (*Object, *BaseException) {
return
result
,
nil
return
result
,
nil
}
}
func
builtinSum
(
f
*
Frame
,
args
Args
,
_
KWArgs
)
(
*
Object
,
*
BaseException
)
{
argc
:=
len
(
args
)
expectedTypes
:=
[]
*
Type
{
ObjectType
,
ObjectType
}
if
argc
==
1
{
expectedTypes
=
expectedTypes
[
:
1
]
}
if
raised
:=
checkFunctionArgs
(
f
,
"sum"
,
args
,
expectedTypes
...
);
raised
!=
nil
{
return
nil
,
raised
}
var
result
*
Object
if
argc
>
1
{
if
args
[
1
]
.
typ
==
StrType
{
return
nil
,
f
.
RaiseType
(
TypeErrorType
,
"sum() can't sum strings [use ''.join(seq) instead]"
)
}
result
=
args
[
1
]
}
else
{
result
=
NewInt
(
0
)
.
ToObject
()
}
raised
:=
seqForEach
(
f
,
args
[
0
],
func
(
o
*
Object
)
(
raised
*
BaseException
)
{
result
,
raised
=
Add
(
f
,
result
,
o
)
return
raised
})
if
raised
!=
nil
{
return
nil
,
raised
}
return
result
,
nil
}
func
builtinUniChr
(
f
*
Frame
,
args
Args
,
_
KWArgs
)
(
*
Object
,
*
BaseException
)
{
func
builtinUniChr
(
f
*
Frame
,
args
Args
,
_
KWArgs
)
(
*
Object
,
*
BaseException
)
{
if
raised
:=
checkFunctionArgs
(
f
,
"unichr"
,
args
,
IntType
);
raised
!=
nil
{
if
raised
:=
checkFunctionArgs
(
f
,
"unichr"
,
args
,
IntType
);
raised
!=
nil
{
return
nil
,
raised
return
nil
,
raised
...
@@ -756,6 +785,7 @@ func init() {
...
@@ -756,6 +785,7 @@ func init() {
"round"
:
newBuiltinFunction
(
"round"
,
builtinRound
)
.
ToObject
(),
"round"
:
newBuiltinFunction
(
"round"
,
builtinRound
)
.
ToObject
(),
"setattr"
:
newBuiltinFunction
(
"setattr"
,
builtinSetAttr
)
.
ToObject
(),
"setattr"
:
newBuiltinFunction
(
"setattr"
,
builtinSetAttr
)
.
ToObject
(),
"sorted"
:
newBuiltinFunction
(
"sorted"
,
builtinSorted
)
.
ToObject
(),
"sorted"
:
newBuiltinFunction
(
"sorted"
,
builtinSorted
)
.
ToObject
(),
"sum"
:
newBuiltinFunction
(
"sum"
,
builtinSum
)
.
ToObject
(),
"True"
:
True
.
ToObject
(),
"True"
:
True
.
ToObject
(),
"unichr"
:
newBuiltinFunction
(
"unichr"
,
builtinUniChr
)
.
ToObject
(),
"unichr"
:
newBuiltinFunction
(
"unichr"
,
builtinUniChr
)
.
ToObject
(),
"zip"
:
newBuiltinFunction
(
"zip"
,
builtinZip
)
.
ToObject
(),
"zip"
:
newBuiltinFunction
(
"zip"
,
builtinZip
)
.
ToObject
(),
...
...
runtime/builtin_types_test.go
View file @
f2f26155
...
@@ -90,6 +90,11 @@ func TestBuiltinFuncs(t *testing.T) {
...
@@ -90,6 +90,11 @@ func TestBuiltinFuncs(t *testing.T) {
return
newObject
(
badNextType
),
nil
return
newObject
(
badNextType
),
nil
})
.
ToObject
(),
})
.
ToObject
(),
}))
}))
addType
:=
newTestClass
(
"Add"
,
[]
*
Type
{
ObjectType
},
newStringDict
(
map
[
string
]
*
Object
{
"__add__"
:
newBuiltinFunction
(
"__add__"
,
func
(
f
*
Frame
,
_
Args
,
_
KWArgs
)
(
*
Object
,
*
BaseException
)
{
return
NewInt
(
1
)
.
ToObject
(),
nil
})
.
ToObject
(),
}))
fooBuiltinFunc
:=
newBuiltinFunction
(
"foo"
,
func
(
f
*
Frame
,
args
Args
,
kwargs
KWArgs
)
(
*
Object
,
*
BaseException
)
{
fooBuiltinFunc
:=
newBuiltinFunction
(
"foo"
,
func
(
f
*
Frame
,
args
Args
,
kwargs
KWArgs
)
(
*
Object
,
*
BaseException
)
{
return
newTestTuple
(
NewTuple
(
args
.
makeCopy
()
...
),
kwargs
.
makeDict
())
.
ToObject
(),
nil
return
newTestTuple
(
NewTuple
(
args
.
makeCopy
()
...
),
kwargs
.
makeDict
())
.
ToObject
(),
nil
})
.
ToObject
()
})
.
ToObject
()
...
@@ -313,6 +318,13 @@ func TestBuiltinFuncs(t *testing.T) {
...
@@ -313,6 +318,13 @@ func TestBuiltinFuncs(t *testing.T) {
{
f
:
"sorted"
,
args
:
wrapArgs
(
newTestDict
(
"foo"
,
1
,
"bar"
,
2
)),
want
:
newTestList
(
"bar"
,
"foo"
)
.
ToObject
()},
{
f
:
"sorted"
,
args
:
wrapArgs
(
newTestDict
(
"foo"
,
1
,
"bar"
,
2
)),
want
:
newTestList
(
"bar"
,
"foo"
)
.
ToObject
()},
{
f
:
"sorted"
,
args
:
wrapArgs
(
1
),
wantExc
:
mustCreateException
(
TypeErrorType
,
"'int' object is not iterable"
)},
{
f
:
"sorted"
,
args
:
wrapArgs
(
1
),
wantExc
:
mustCreateException
(
TypeErrorType
,
"'int' object is not iterable"
)},
{
f
:
"sorted"
,
args
:
wrapArgs
(
newTestList
(
"foo"
,
"bar"
),
2
),
wantExc
:
mustCreateException
(
TypeErrorType
,
"'sorted' requires 1 arguments"
)},
{
f
:
"sorted"
,
args
:
wrapArgs
(
newTestList
(
"foo"
,
"bar"
),
2
),
wantExc
:
mustCreateException
(
TypeErrorType
,
"'sorted' requires 1 arguments"
)},
{
f
:
"sum"
,
args
:
wrapArgs
(
newTestList
(
1
,
2
,
3
,
4
)),
want
:
NewInt
(
10
)
.
ToObject
()},
{
f
:
"sum"
,
args
:
wrapArgs
(
newTestList
(
1
,
2
),
3
),
want
:
NewFloat
(
6
)
.
ToObject
()},
{
f
:
"sum"
,
args
:
wrapArgs
(
newTestList
(
2
,
1.1
)),
want
:
NewFloat
(
3.1
)
.
ToObject
()},
{
f
:
"sum"
,
args
:
wrapArgs
(
newTestList
(
2
,
1.1
,
2
)),
want
:
NewFloat
(
5.1
)
.
ToObject
()},
{
f
:
"sum"
,
args
:
wrapArgs
(
newTestList
(
2
,
1.1
,
2.0
)),
want
:
NewFloat
(
5.1
)
.
ToObject
()},
{
f
:
"sum"
,
args
:
wrapArgs
(
newTestList
(
1
),
newObject
(
addType
)),
want
:
NewInt
(
1
)
.
ToObject
()},
{
f
:
"sum"
,
args
:
wrapArgs
(
newTestList
(
newObject
(
addType
)),
newObject
(
addType
)),
want
:
NewInt
(
1
)
.
ToObject
()},
{
f
:
"unichr"
,
args
:
wrapArgs
(
0
),
want
:
NewUnicode
(
"
\x00
"
)
.
ToObject
()},
{
f
:
"unichr"
,
args
:
wrapArgs
(
0
),
want
:
NewUnicode
(
"
\x00
"
)
.
ToObject
()},
{
f
:
"unichr"
,
args
:
wrapArgs
(
65
),
want
:
NewStr
(
"A"
)
.
ToObject
()},
{
f
:
"unichr"
,
args
:
wrapArgs
(
65
),
want
:
NewStr
(
"A"
)
.
ToObject
()},
{
f
:
"unichr"
,
args
:
wrapArgs
(
0x120000
),
wantExc
:
mustCreateException
(
ValueErrorType
,
"unichr() arg not in range(0x10ffff)"
)},
{
f
:
"unichr"
,
args
:
wrapArgs
(
0x120000
),
wantExc
:
mustCreateException
(
ValueErrorType
,
"unichr() arg not in range(0x10ffff)"
)},
...
...
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