-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Will take in a string a string and return the length of said string. This function only accepts one argument. This function only accepts strings. Any other type will result in an error.
- Loading branch information
Showing
4 changed files
with
81 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
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 new_error("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 new_error("argument to `len` not supported, got %s", args[0].Type()) | ||
} | ||
}, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters