more builtins
This commit is contained in:
24
builtins/divmod.go
Normal file
24
builtins/divmod.go
Normal 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())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user