Files
monkey/internal/builtins/pow.go
Charles Smith aebbe43999
Some checks failed
Build / build (push) Successful in 10m25s
Publish Image / publish (push) Failing after 39s
Test / build (push) Successful in 11m19s
Fix VM memory allocation optimizations by reducing what we allocate on the heap
2024-03-31 20:44:50 -04:00

35 lines
571 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}
}