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

Commit 2c94248

Browse files
committed
Add pow builtin
1 parent 6bdfc4b commit 2c94248

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

runtime/builtin_types.go

+13
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,18 @@ func builtinOrd(f *Frame, args Args, _ KWArgs) (*Object, *BaseException) {
546546
return NewInt(result).ToObject(), nil
547547
}
548548

549+
func builtinPower(f *Frame, args Args, kwargs KWArgs) (*Object, *BaseException) {
550+
argc := len(args)
551+
expectedTypes := []*Type{ObjectType, ObjectType}
552+
if argc == 3 {
553+
return nil, f.RaiseType(NotImplementedErrorType, "third parameter is not supported now")
554+
}
555+
if raised := checkFunctionArgs(f, "pow", args, expectedTypes...); raised != nil {
556+
return nil, raised
557+
}
558+
return Pow(f, args[0], args[1])
559+
}
560+
549561
func builtinPrint(f *Frame, args Args, kwargs KWArgs) (*Object, *BaseException) {
550562
sep := " "
551563
end := "\n"
@@ -787,6 +799,7 @@ func init() {
787799
"oct": newBuiltinFunction("oct", builtinOct).ToObject(),
788800
"open": newBuiltinFunction("open", builtinOpen).ToObject(),
789801
"ord": newBuiltinFunction("ord", builtinOrd).ToObject(),
802+
"pow": newBuiltinFunction("pow", builtinPower).ToObject(),
790803
"print": newBuiltinFunction("print", builtinPrint).ToObject(),
791804
"range": newBuiltinFunction("range", builtinRange).ToObject(),
792805
"raw_input": newBuiltinFunction("raw_input", builtinRawInput).ToObject(),

runtime/builtin_types_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,11 @@ func TestBuiltinFuncs(t *testing.T) {
294294
{f: "ord", args: wrapArgs("foo"), wantExc: mustCreateException(ValueErrorType, "ord() expected a character, but string of length 3 found")},
295295
{f: "ord", args: wrapArgs(NewUnicode("волн")), wantExc: mustCreateException(ValueErrorType, "ord() expected a character, but string of length 4 found")},
296296
{f: "ord", args: wrapArgs(1, 2, 3), wantExc: mustCreateException(TypeErrorType, "'ord' requires 1 arguments")},
297+
{f: "pow", args: wrapArgs(1), wantExc: mustCreateException(TypeErrorType, "'pow' requires 2 arguments")},
298+
{f: "pow", args: wrapArgs(2, 3), want: NewInt(8).ToObject()},
299+
{f: "pow", args: wrapArgs(3.0, 3.0), want: NewFloat(27.0).ToObject()},
300+
{f: "pow", args: wrapArgs(2, -2), want: NewFloat(0.25).ToObject()},
301+
{f: "pow", args: wrapArgs(0i, 0i), want: NewComplex(complex(1, 0i)).ToObject()},
297302
{f: "range", args: wrapArgs(), wantExc: mustCreateException(TypeErrorType, "'__new__' of 'int' requires 3 arguments")},
298303
{f: "range", args: wrapArgs(3), want: newTestList(0, 1, 2).ToObject()},
299304
{f: "range", args: wrapArgs(10, 0), want: NewList().ToObject()},

0 commit comments

Comments
 (0)