more builtins

This commit is contained in:
Chuck Smith
2024-03-24 16:03:08 -04:00
parent b4ba660704
commit a08fc1520c
12 changed files with 243 additions and 3 deletions

34
builtins/pow.go Normal file
View File

@@ -0,0 +1,34 @@
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())
}
}