diff --git a/grumpy-runtime-src/runtime/builtin_types.go b/grumpy-runtime-src/runtime/builtin_types.go index 888951e7..1e13b9ea 100644 --- a/grumpy-runtime-src/runtime/builtin_types.go +++ b/grumpy-runtime-src/runtime/builtin_types.go @@ -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" @@ -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(), diff --git a/grumpy-runtime-src/runtime/builtin_types_test.go b/grumpy-runtime-src/runtime/builtin_types_test.go index 732db19d..390a0df2 100644 --- a/grumpy-runtime-src/runtime/builtin_types_test.go +++ b/grumpy-runtime-src/runtime/builtin_types_test.go @@ -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()},