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

Commit

Permalink
Add pow builtin
Browse files Browse the repository at this point in the history
  • Loading branch information
aisk committed Aug 5, 2017
1 parent 6bdfc4b commit 2c94248
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
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")},
{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

0 comments on commit 2c94248

Please sign in to comment.