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

24
builtins/divmod.go Normal file
View File

@@ -0,0 +1,24 @@
package builtins
import "monkey/object"
// Divmod ...
func Divmod(args ...object.Object) object.Object {
if len(args) != 2 {
return newError("wrong number of arguments. got=%d, want=2",
len(args))
}
if a, ok := args[0].(*object.Integer); ok {
if b, ok := args[1].(*object.Integer); ok {
elements := make([]object.Object, 2)
elements[0] = &object.Integer{Value: a.Value / b.Value}
elements[1] = &object.Integer{Value: a.Value % b.Value}
return &object.Array{Elements: elements}
} else {
return newError("expected argument #2 to `divmod` to be `int` got=%s", args[1].Type())
}
} else {
return newError("expected argument #1 to `divmod` to be `int` got=%s", args[0].Type())
}
}