more builtins
This commit is contained in:
20
builtins/abs.go
Normal file
20
builtins/abs.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package builtins
|
||||
|
||||
import "monkey/object"
|
||||
|
||||
// Abs ...
|
||||
func Abs(args ...object.Object) object.Object {
|
||||
if len(args) != 1 {
|
||||
return newError("wrong number of arguments. got=%d, want=1",
|
||||
len(args))
|
||||
}
|
||||
|
||||
if i, ok := args[0].(*object.Integer); ok {
|
||||
value := i.Value
|
||||
if value < 0 {
|
||||
value = value * -1
|
||||
}
|
||||
return &object.Integer{Value: value}
|
||||
}
|
||||
return newError("argument to `abs` not supported, got %s", args[0].Type())
|
||||
}
|
||||
Reference in New Issue
Block a user