This commit is contained in:
Chuck Smith
2024-01-21 11:41:17 -05:00
parent 13c9062fed
commit 6bb06370bb
4 changed files with 87 additions and 11 deletions

21
evaluator/builtins.go Normal file
View File

@@ -0,0 +1,21 @@
package evaluator
import "monkey/object"
var builtins = map[string]*object.Builtin{
"len": &object.Builtin{
Fn: func(args ...object.Object) object.Object {
if len(args) != 1 {
return newError("wrong number of arguments. got=%d, want=1", len(args))
}
switch arg := args[0].(type) {
case *object.String:
return &object.Integer{Value: int64(len(arg.Value))}
default:
return newError("argument to `len` not supported, got %s", args[0].Type())
}
},
},
}