more builtins
This commit is contained in:
19
builtins/ord.go
Normal file
19
builtins/ord.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package builtins
|
||||
|
||||
import "monkey/object"
|
||||
|
||||
// Ord ...
|
||||
func Ord(args ...object.Object) object.Object {
|
||||
if len(args) != 1 {
|
||||
return newError("wrong number of arguments. got=%d, want=1",
|
||||
len(args))
|
||||
}
|
||||
|
||||
if s, ok := args[0].(*object.String); ok {
|
||||
if len(s.Value) == 1 {
|
||||
return &object.Integer{Value: int64(s.Value[0])}
|
||||
}
|
||||
return newError("`ord()` expected a character but got string of length %d", len(s.Value))
|
||||
}
|
||||
return newError("argument to `ord` not supported, got %s", args[0].Type())
|
||||
}
|
||||
Reference in New Issue
Block a user