Files
monkey/internal/builtins/abs.go
Chuck Smith 12d43c9835
Some checks failed
Publish Image / publish (push) Waiting to run
Test / build (push) Waiting to run
Build / build (push) Has been cancelled
Refactor Type to be an int
2024-03-29 11:01:15 -04:00

25 lines
427 B
Go

package builtins
import (
"monkey/internal/object"
"monkey/internal/typing"
)
// Abs ...
func Abs(args ...object.Object) object.Object {
if err := typing.Check(
"abs", args,
typing.ExactArgs(1),
typing.WithTypes(object.IntegerType),
); err != nil {
return newError(err.Error())
}
i := args[0].(*object.Integer)
value := i.Value
if value < 0 {
value = value * -1
}
return &object.Integer{Value: value}
}