From 2c94248b167ebdde91f93e4a416d6037a41f38e6 Mon Sep 17 00:00:00 2001 From: Ang Long Date: Sun, 30 Jul 2017 22:10:13 +0800 Subject: [PATCH] Add pow builtin --- runtime/builtin_types.go | 13 +++++++++++++ runtime/builtin_types_test.go | 5 +++++ 2 files changed, 18 insertions(+) diff --git a/runtime/builtin_types.go b/runtime/builtin_types.go index 888951e7..a1933da1 100644 --- a/runtime/builtin_types.go +++ b/runtime/builtin_types.go @@ -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" @@ -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(), diff --git a/runtime/builtin_types_test.go b/runtime/builtin_types_test.go index 732db19d..390a0df2 100644 --- a/runtime/builtin_types_test.go +++ b/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()},