2019-01-10 04:18:37 +03:00
|
|
|
package objects
|
|
|
|
|
|
|
|
func builtinString(args ...Object) (Object, error) {
|
|
|
|
if len(args) != 1 {
|
2019-01-17 12:56:05 +03:00
|
|
|
return nil, ErrWrongNumArguments
|
2019-01-10 04:18:37 +03:00
|
|
|
}
|
|
|
|
|
2019-01-18 03:52:07 +03:00
|
|
|
if _, ok := args[0].(*String); ok {
|
|
|
|
return args[0], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
v, ok := ToString(args[0])
|
|
|
|
if ok {
|
|
|
|
return &String{Value: v}, nil
|
2019-01-10 04:18:37 +03:00
|
|
|
}
|
2019-01-18 03:52:07 +03:00
|
|
|
|
|
|
|
return UndefinedValue, nil
|
2019-01-10 04:18:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func builtinInt(args ...Object) (Object, error) {
|
|
|
|
if len(args) != 1 {
|
2019-01-17 12:56:05 +03:00
|
|
|
return nil, ErrWrongNumArguments
|
2019-01-10 04:18:37 +03:00
|
|
|
}
|
|
|
|
|
2019-01-18 03:52:07 +03:00
|
|
|
if _, ok := args[0].(*Int); ok {
|
|
|
|
return args[0], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
v, ok := ToInt64(args[0])
|
|
|
|
if ok {
|
|
|
|
return &Int{Value: v}, nil
|
2019-01-10 04:18:37 +03:00
|
|
|
}
|
|
|
|
|
2019-01-14 01:24:39 +03:00
|
|
|
return UndefinedValue, nil
|
2019-01-10 04:18:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func builtinFloat(args ...Object) (Object, error) {
|
|
|
|
if len(args) != 1 {
|
2019-01-17 12:56:05 +03:00
|
|
|
return nil, ErrWrongNumArguments
|
2019-01-10 04:18:37 +03:00
|
|
|
}
|
|
|
|
|
2019-01-18 03:52:07 +03:00
|
|
|
if _, ok := args[0].(*Float); ok {
|
|
|
|
return args[0], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
v, ok := ToFloat64(args[0])
|
|
|
|
if ok {
|
|
|
|
return &Float{Value: v}, nil
|
2019-01-10 04:18:37 +03:00
|
|
|
}
|
|
|
|
|
2019-01-14 01:24:39 +03:00
|
|
|
return UndefinedValue, nil
|
2019-01-10 04:18:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func builtinBool(args ...Object) (Object, error) {
|
|
|
|
if len(args) != 1 {
|
2019-01-17 12:56:05 +03:00
|
|
|
return nil, ErrWrongNumArguments
|
2019-01-10 04:18:37 +03:00
|
|
|
}
|
|
|
|
|
2019-01-18 03:52:07 +03:00
|
|
|
if _, ok := args[0].(*Bool); ok {
|
|
|
|
return args[0], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
v, ok := ToBool(args[0])
|
|
|
|
if ok {
|
|
|
|
return &Bool{Value: v}, nil
|
2019-01-10 04:18:37 +03:00
|
|
|
}
|
2019-01-18 03:52:07 +03:00
|
|
|
|
|
|
|
return UndefinedValue, nil
|
2019-01-10 04:18:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func builtinChar(args ...Object) (Object, error) {
|
|
|
|
if len(args) != 1 {
|
2019-01-17 12:56:05 +03:00
|
|
|
return nil, ErrWrongNumArguments
|
2019-01-10 04:18:37 +03:00
|
|
|
}
|
|
|
|
|
2019-01-18 03:52:07 +03:00
|
|
|
if _, ok := args[0].(*Char); ok {
|
|
|
|
return args[0], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
v, ok := ToRune(args[0])
|
|
|
|
if ok {
|
|
|
|
return &Char{Value: v}, nil
|
2019-01-10 04:18:37 +03:00
|
|
|
}
|
|
|
|
|
2019-01-14 01:24:39 +03:00
|
|
|
return UndefinedValue, nil
|
2019-01-10 04:18:37 +03:00
|
|
|
}
|