Commit f2f26155 authored by Long Ang's avatar Long Ang Committed by Dylan Trotter

implement a simple builtin sum

parent f943e9e0
...@@ -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(),
......
...@@ -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)")},
......
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