Skip to content

Builtin pow (from Add pow builtin ) #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 23, 2018
Merged
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
12 changes: 12 additions & 0 deletions grumpy-runtime-src/runtime/builtin_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,17 @@ func builtinOrd(f *Frame, args Args, _ KWArgs) (*Object, *BaseException) {
return NewInt(result).ToObject(), nil
}

func builtinPower(f *Frame, args Args, kwargs KWArgs) (*Object, *BaseException) {
expectedTypes := []*Type{ObjectType, ObjectType}
if len(args) == 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 +798,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 grumpy-runtime-src/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")},
{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