Skip to content
This repository has been archived by the owner on Mar 23, 2023. It is now read-only.

Add pow builtin #373

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions runtime/builtin_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,18 @@ func builtinOrd(f *Frame, args Args, _ KWArgs) (*Object, *BaseException) {
return NewInt(result).ToObject(), nil
}

func builtinPower(f *Frame, args Args, kwargs KWArgs) (*Object, *BaseException) {
argc := len(args)
expectedTypes := []*Type{ObjectType, ObjectType}
if argc == 3 {
return nil, f.RaiseType(NotImplementedErrorType, "third parameter is not supported now")
}
if raised := checkFunctionArgs(f, "pow", args, expectedTypes...); raised != nil {
return nil, raised
}
return Pow(f, args[0], args[1])
}

func builtinPrint(f *Frame, args Args, kwargs KWArgs) (*Object, *BaseException) {
sep := " "
end := "\n"
Expand Down Expand Up @@ -787,6 +799,7 @@ func init() {
"oct": newBuiltinFunction("oct", builtinOct).ToObject(),
"open": newBuiltinFunction("open", builtinOpen).ToObject(),
"ord": newBuiltinFunction("ord", builtinOrd).ToObject(),
"pow": newBuiltinFunction("pow", builtinPower).ToObject(),
"print": newBuiltinFunction("print", builtinPrint).ToObject(),
"range": newBuiltinFunction("range", builtinRange).ToObject(),
"raw_input": newBuiltinFunction("raw_input", builtinRawInput).ToObject(),
Expand Down
5 changes: 5 additions & 0 deletions runtime/builtin_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,11 @@ func TestBuiltinFuncs(t *testing.T) {
{f: "ord", args: wrapArgs("foo"), wantExc: mustCreateException(ValueErrorType, "ord() expected a character, but string of length 3 found")},
{f: "ord", args: wrapArgs(NewUnicode("волн")), wantExc: mustCreateException(ValueErrorType, "ord() expected a character, but string of length 4 found")},
{f: "ord", args: wrapArgs(1, 2, 3), wantExc: mustCreateException(TypeErrorType, "'ord' requires 1 arguments")},
{f: "pow", args: wrapArgs(1), wantExc: mustCreateException(TypeErrorType, "'pow' requires 2 arguments")},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to have a couple more test cases, e.g. a negative exponent and a complex number

{f: "pow", args: wrapArgs(2, 3), want: NewInt(8).ToObject()},
{f: "pow", args: wrapArgs(3.0, 3.0), want: NewFloat(27.0).ToObject()},
{f: "pow", args: wrapArgs(2, -2), want: NewFloat(0.25).ToObject()},
{f: "pow", args: wrapArgs(0i, 0i), want: NewComplex(complex(1, 0i)).ToObject()},
{f: "range", args: wrapArgs(), wantExc: mustCreateException(TypeErrorType, "'__new__' of 'int' requires 3 arguments")},
{f: "range", args: wrapArgs(3), want: newTestList(0, 1, 2).ToObject()},
{f: "range", args: wrapArgs(10, 0), want: NewList().ToObject()},
Expand Down