35 lines
693 B
Go
35 lines
693 B
Go
package builtins
|
|
|
|
import "monkey/object"
|
|
|
|
func pow(x, y int64) int64 {
|
|
p := int64(1)
|
|
for y > 0 {
|
|
if y&1 != 0 {
|
|
p *= x
|
|
}
|
|
y >>= 1
|
|
x *= x
|
|
}
|
|
return p
|
|
}
|
|
|
|
// Pow ...
|
|
func Pow(args ...object.Object) object.Object {
|
|
if len(args) != 2 {
|
|
return newError("wrong number of arguments. got=%d, want=2",
|
|
len(args))
|
|
}
|
|
|
|
if x, ok := args[0].(*object.Integer); ok {
|
|
if y, ok := args[1].(*object.Integer); ok {
|
|
value := pow(x.Value, y.Value)
|
|
return &object.Integer{Value: value}
|
|
} else {
|
|
return newError("expected argument #2 to `pow` to be `int` got=%s", args[1].Type())
|
|
}
|
|
} else {
|
|
return newError("expected argument #1 to `pow` to be `int` got=%s", args[0].Type())
|
|
}
|
|
}
|