type checking and error handling for builtins improved.
This commit is contained in:
@@ -1,18 +1,20 @@
|
||||
package builtins
|
||||
|
||||
import "monkey/object"
|
||||
|
||||
import "strings"
|
||||
import (
|
||||
"monkey/object"
|
||||
"monkey/typing"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Upper ...
|
||||
func Upper(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(
|
||||
"upper", args,
|
||||
typing.ExactArgs(1),
|
||||
typing.WithTypes(object.STRING_OBJ),
|
||||
); err != nil {
|
||||
return newError(err.Error())
|
||||
}
|
||||
|
||||
if str, ok := args[0].(*object.String); ok {
|
||||
return &object.String{Value: strings.ToUpper(str.Value)}
|
||||
}
|
||||
return newError("expected `str` argument to `upper` got=%T", args[0])
|
||||
return &object.String{Value: strings.ToUpper(args[0].(*object.String).Value)}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user