Files
monkey/internal/builtins/pow.go
Chuck Smith 12d43c9835
Some checks failed
Publish Image / publish (push) Waiting to run
Test / build (push) Waiting to run
Build / build (push) Has been cancelled
Refactor Type to be an int
2024-03-29 11:01:15 -04:00

35 lines
574 B
Go

package builtins
import (
"monkey/internal/object"
"monkey/internal/typing"
)
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 err := typing.Check(
"pow", args,
typing.ExactArgs(2),
typing.WithTypes(object.IntegerType, object.IntegerType),
); err != nil {
return newError(err.Error())
}
x := args[0].(*object.Integer)
y := args[1].(*object.Integer)
value := pow(x.Value, y.Value)
return &object.Integer{Value: value}
}