rearrange builtins
This commit is contained in:
28
builtins/read.go
Normal file
28
builtins/read.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package builtins
|
||||
|
||||
import "monkey/object"
|
||||
|
||||
import (
|
||||
"os"
|
||||
)
|
||||
|
||||
// Read ...
|
||||
func Read(args ...object.Object) object.Object {
|
||||
if len(args) != 1 {
|
||||
return newError("wrong number of arguments. got=%d, want=1",
|
||||
len(args))
|
||||
}
|
||||
|
||||
arg, ok := args[0].(*object.String)
|
||||
if !ok {
|
||||
return newError("argument to `read` expected to be `str` got=%T", args[0].Type())
|
||||
}
|
||||
|
||||
filename := arg.Value
|
||||
data, err := os.ReadFile(filename)
|
||||
if err != nil {
|
||||
return newError("error reading file: %s", err)
|
||||
}
|
||||
|
||||
return &object.String{Value: string(data)}
|
||||
}
|
||||
Reference in New Issue
Block a user