Files
monkey/internal/builtins/input.go
Charles Smith aebbe43999
Some checks failed
Build / build (push) Successful in 10m25s
Publish Image / publish (push) Failing after 39s
Test / build (push) Successful in 11m19s
Fix VM memory allocation optimizations by reducing what we allocate on the heap
2024-03-31 20:44:50 -04:00

35 lines
663 B
Go

package builtins
import (
"bufio"
"fmt"
"io"
"monkey/internal/object"
"monkey/internal/typing"
"os"
)
// Input ...
func Input(args ...object.Object) object.Object {
if err := typing.Check(
"input", args,
typing.RangeOfArgs(0, 1),
typing.WithTypes(object.StringType),
); err != nil {
return newError(err.Error())
}
if len(args) == 1 {
prompt := args[0].(object.String).Value
fmt.Fprintf(os.Stdout, prompt)
}
buffer := bufio.NewReader(os.Stdin)
line, _, err := buffer.ReadLine()
if err != nil && err != io.EOF {
return newError(fmt.Sprintf("error reading input from stdin: %s", err))
}
return object.String{Value: string(line)}
}