Add file operations
Some checks failed
Build / build (push) Failing after 6m4s
Test / build (push) Has been cancelled

This commit is contained in:
Chuck Smith
2024-03-28 14:00:21 -04:00
parent 110152a139
commit 67c5b4cd66
7 changed files with 226 additions and 3 deletions

28
builtins/write.go Normal file
View File

@@ -0,0 +1,28 @@
package builtins
import (
"monkey/object"
"monkey/typing"
"syscall"
)
// Write ...
func Write(args ...object.Object) object.Object {
if err := typing.Check(
"write", args,
typing.ExactArgs(2),
typing.WithTypes(object.INTEGER_OBJ, object.INTEGER_OBJ),
); err != nil {
return newError(err.Error())
}
fd := int(args[0].(*object.Integer).Value)
data := []byte(args[1].(*object.String).Value)
n, err := syscall.Write(syscall.Handle(fd), data)
if err != nil {
return newError("IOError: %s", err)
}
return &object.Integer{Value: int64(n)}
}