type checking and error handling for builtins improved.
This commit is contained in:
@@ -1,13 +1,19 @@
|
||||
package builtins
|
||||
|
||||
import "monkey/object"
|
||||
import (
|
||||
"monkey/object"
|
||||
"monkey/typing"
|
||||
)
|
||||
|
||||
import "strconv"
|
||||
|
||||
// Int ...
|
||||
func Int(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(
|
||||
"int", args,
|
||||
typing.ExactArgs(1),
|
||||
); err != nil {
|
||||
return newError(err.Error())
|
||||
}
|
||||
|
||||
switch arg := args[0].(type) {
|
||||
@@ -23,9 +29,8 @@ func Int(args ...object.Object) object.Object {
|
||||
if err != nil {
|
||||
return newError("could not parse string to int: %s", err)
|
||||
}
|
||||
|
||||
return &object.Integer{Value: n}
|
||||
default:
|
||||
return newError("argument to `int` not supported, got=%s", args[0].Type())
|
||||
return &object.Integer{}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user