type checking and error handling for builtins improved.
This commit is contained in:
@@ -3,18 +3,20 @@ package builtins
|
||||
import (
|
||||
"fmt"
|
||||
"monkey/object"
|
||||
"monkey/typing"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// Bin ...
|
||||
// Bin ...
|
||||
func Bin(args ...object.Object) object.Object {
|
||||
if len(args) != 1 {
|
||||
return newError("wrong number of arguments. got=%d, want=1",
|
||||
len(args))
|
||||
if err := typing.Check(
|
||||
"bin", args,
|
||||
typing.ExactArgs(1),
|
||||
typing.WithTypes(object.INTEGER_OBJ),
|
||||
); err != nil {
|
||||
return newError(err.Error())
|
||||
}
|
||||
|
||||
if i, ok := args[0].(*object.Integer); ok {
|
||||
return &object.String{Value: fmt.Sprintf("0b%s", strconv.FormatInt(i.Value, 2))}
|
||||
}
|
||||
return newError("argument to `bin` not supported, got %s", args[0].Type())
|
||||
i := args[0].(*object.Integer)
|
||||
return &object.String{Value: fmt.Sprintf("0b%s", strconv.FormatInt(i.Value, 2))}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user