Documentation
¶
Overview ¶
Package io provides I/O primitives for reading and writing.
Textual Read (R7RS 6.13.2) ¶
- read, read-char, peek-char, char-ready?, read-line, read-string
- read-syntax, read-token
Textual Write (R7RS 6.13.3) ¶
- write, display, newline, write-char, write-string
- write-simple, write-shared
- flush-output-port
Binary I/O ¶
- read-u8, peek-u8, u8-ready?, write-u8
- read-bytevector, read-bytevector!, write-bytevector
Port Predicates ¶
- port?, input-port?, output-port?
- textual-port?, binary-port?
- input-port-open?, output-port-open?
- eof-object, eof-object?
String and Bytevector Ports ¶
- open-input-string, open-output-string, get-output-string
- open-input-bytevector, open-output-bytevector, get-output-bytevector
Port State ¶
- current-input-port, current-output-port, current-error-port
- close-port, close-input-port, close-output-port
- call-with-port
Use Extension or AddToRegistry to register all primitives.
Package io provides I/O primitives for reading and writing.
Index ¶
- Constants
- Variables
- func GetCurrentErrorPortParam() *machine.Parameter
- func GetCurrentInputPort() values.TextualReader
- func GetCurrentInputPortParam() *machine.Parameter
- func GetCurrentOutputPort() values.OutputPort
- func GetCurrentOutputPortParam() *machine.Parameter
- func InitState()
- func PrimBinaryPortQ(mc machine.CallContext) error
- func PrimCharReadyQ(mc machine.CallContext) error
- func PrimCloseInputPort(mc machine.CallContext) error
- func PrimCloseOutputPort(mc machine.CallContext) error
- func PrimClosePort(mc machine.CallContext) error
- func PrimDisplay(mc machine.CallContext) error
- func PrimEofObject(mc machine.CallContext) error
- func PrimEofObjectQ(mc machine.CallContext) error
- func PrimFlushOutputPort(mc machine.CallContext) error
- func PrimGetOutputBytevector(mc machine.CallContext) error
- func PrimGetOutputString(mc machine.CallContext) error
- func PrimInputPortOpenQ(mc machine.CallContext) error
- func PrimNewline(mc machine.CallContext) error
- func PrimOpenInputBytevector(mc machine.CallContext) error
- func PrimOpenInputString(mc machine.CallContext) error
- func PrimOpenOutputBytevector(mc machine.CallContext) error
- func PrimOpenOutputString(mc machine.CallContext) error
- func PrimOutputPortOpenQ(mc machine.CallContext) error
- func PrimPeekChar(mc machine.CallContext) error
- func PrimPeekU8(mc machine.CallContext) error
- func PrimRead(mc machine.CallContext) error
- func PrimReadBytevector(mc machine.CallContext) error
- func PrimReadBytevectorBang(mc machine.CallContext) error
- func PrimReadChar(mc machine.CallContext) error
- func PrimReadLine(mc machine.CallContext) error
- func PrimReadString(mc machine.CallContext) error
- func PrimReadSyntax(mc machine.CallContext) error
- func PrimReadToken(mc machine.CallContext) error
- func PrimReadU8(mc machine.CallContext) error
- func PrimTextualPortQ(mc machine.CallContext) error
- func PrimU8ReadyQ(mc machine.CallContext) error
- func PrimWrite(mc machine.CallContext) error
- func PrimWriteBytevector(mc machine.CallContext) error
- func PrimWriteChar(mc machine.CallContext) error
- func PrimWriteShared(mc machine.CallContext) error
- func PrimWriteSimple(mc machine.CallContext) error
- func PrimWriteString(mc machine.CallContext) error
- func PrimWriteU8(mc machine.CallContext) error
- func ResetCurrentInputPort()
- func ResetCurrentOutputPort()
- func ResetState()
- func SetCurrentInputPort(port values.TextualReader)
- func SetCurrentOutputPort(port values.OutputPort)
- func StringValue(o values.Value) string
Constants ¶
const ( // MaxReadBytevectorBytes is the maximum size of bytevector that // read-bytevector can allocate (100 MB). MaxReadBytevectorBytes = 100 * 1024 * 1024 // 100 MB )
const ( // MaxReadStringBytes is the maximum memory that read-string can allocate // for the character buffer (100 MB). Assumes 4 bytes per rune (worst case). MaxReadStringBytes = 100 * 1024 * 1024 // 100 MB )
Variables ¶
var ( // Tokenizers caches tokenizers per input port. // // Entries are evicted via evictPortCache() on port close or EOF. // Ports that are abandoned without close or EOF will retain their // cache entries until the process exits. If that becomes a problem // in long-running programs, switch to weak.Pointer[T] (Go 1.24+). // // Thread safety: All access must be protected by cacheMu. Tokenizers map[values.Value]*tokenizer.Tokenizer // Parsers caches parsers per input port. // Same retention caveat as Tokenizers above. // // Thread safety: All access must be protected by cacheMu. Parsers map[values.Value]*parser.Parser // ProgramStartTime is used for current-jiffy to measure elapsed time. ProgramStartTime = time.Now() )
Package-level state for I/O ports. These are lazily initialized on first access.
var AddToRegistry = Builder.AddToRegistry
AddToRegistry registers all I/O primitives.
var Builder = registry.NewRegistryBuilder(
addReadWrite,
addPorts,
addPortState,
addPortProcs,
)
Builder aggregates all I/O registration functions.
var Extension = registry.NewDescribedExtension("io", "I/O ports: reading, writing, string/bytevector ports, display, write.", AddToRegistry)
Extension is the I/O extension.
var PrimInputPortQ = helpers.MakeTypePredicate(func(o values.Value) bool { _, ok := o.(values.InputPort) return ok })
var PrimOutputPortQ = helpers.MakeTypePredicate(func(o values.Value) bool { _, ok := o.(values.OutputPort) return ok })
var PrimPortQ = helpers.MakeTypePredicate(func(o values.Value) bool { _, ok := o.(values.Port) return ok })
Port type predicates — R7RS §6.13.1.
Functions ¶
func GetCurrentErrorPortParam ¶
GetCurrentErrorPortParam returns the current-error-port parameter object.
func GetCurrentInputPort ¶
func GetCurrentInputPort() values.TextualReader
GetCurrentInputPort returns the base input port from the parameter. This is a test convenience for save/restore; production code should use resolveCurrentInputPort which checks continuation marks from parameterize.
func GetCurrentInputPortParam ¶
GetCurrentInputPortParam returns the current-input-port parameter object.
func GetCurrentOutputPort ¶
func GetCurrentOutputPort() values.OutputPort
GetCurrentOutputPort returns the base output port from the parameter. This is a test convenience for save/restore; production code should use resolveCurrentOutputPort which checks continuation marks from parameterize.
func GetCurrentOutputPortParam ¶
GetCurrentOutputPortParam returns the current-output-port parameter object.
func InitState ¶
func InitState()
InitState initializes the primitives state. Must be called before using I/O primitives. Safe to call multiple times - subsequent calls are no-ops. Thread-safe: protected by stateMu.
func PrimBinaryPortQ ¶
func PrimBinaryPortQ(mc machine.CallContext) error
PrimBinaryPortQ implements the binary-port? primitive. R7RS §6.13.1: Returns #t if the port is a binary port, #f otherwise.
func PrimCharReadyQ ¶
func PrimCharReadyQ(mc machine.CallContext) error
PrimCharReadyQ implements the char-ready? primitive. R7RS §6.13.2: (char-ready? [port]) Returns #t if a character is ready on the input port, #f otherwise.
func PrimCloseInputPort ¶ added in v1.5.0
func PrimCloseInputPort(mc machine.CallContext) error
PrimCloseInputPort implements the (close-input-port) primitive. Requires an input port; errors if given an output port.
R7RS §6.13.1: close-input-port takes an input port.
func PrimCloseOutputPort ¶ added in v1.5.0
func PrimCloseOutputPort(mc machine.CallContext) error
PrimCloseOutputPort implements the (close-output-port) primitive. Requires an output port; errors if given an input port. Flushes buffered data before closing.
R7RS §6.13.1: close-output-port takes an output port.
func PrimClosePort ¶
func PrimClosePort(mc machine.CallContext) error
PrimClosePort implements the (close-port) primitive. Closes an input or output port.
R7RS §6.13.1: Closes the resource associated with port.
func PrimDisplay ¶
func PrimDisplay(mc machine.CallContext) error
PrimDisplay implements the (display) primitive. Writes a human-readable representation of an object to an output port. R7RS §6.13.3: display uses datum labels to handle circular and shared structures.
func PrimEofObject ¶
func PrimEofObject(mc machine.CallContext) error
PrimEofObject implements the (eof-object) primitive. Returns the EOF object.
func PrimEofObjectQ ¶
func PrimEofObjectQ(mc machine.CallContext) error
PrimEofObjectQ implements the (eof-object?) primitive. Returns #t if the argument is the EOF object.
func PrimFlushOutputPort ¶
func PrimFlushOutputPort(mc machine.CallContext) error
PrimFlushOutputPort implements the flush-output-port primitive. R7RS §6.13.3: (flush-output-port [port]) Flushes any buffered output to the underlying output device.
func PrimGetOutputBytevector ¶
func PrimGetOutputBytevector(mc machine.CallContext) error
PrimGetOutputBytevector implements the Scheme get-output-bytevector primitive.
func PrimGetOutputString ¶
func PrimGetOutputString(mc machine.CallContext) error
PrimGetOutputString implements the Scheme get-output-string primitive.
func PrimInputPortOpenQ ¶
func PrimInputPortOpenQ(mc machine.CallContext) error
PrimInputPortOpenQ implements the (input-port-open?) primitive. Returns #t if input port is open.
R7RS §6.13.1: Returns #t if port is still open and capable of performing input.
func PrimNewline ¶
func PrimNewline(mc machine.CallContext) error
PrimNewline implements the newline primitive. Writes a newline character to the output port.
func PrimOpenInputBytevector ¶
func PrimOpenInputBytevector(mc machine.CallContext) error
PrimOpenInputBytevector implements the Scheme open-input-bytevector primitive.
func PrimOpenInputString ¶
func PrimOpenInputString(mc machine.CallContext) error
PrimOpenInputString implements the Scheme open-input-string primitive.
func PrimOpenOutputBytevector ¶
func PrimOpenOutputBytevector(mc machine.CallContext) error
PrimOpenOutputBytevector implements the Scheme open-output-bytevector primitive.
func PrimOpenOutputString ¶
func PrimOpenOutputString(mc machine.CallContext) error
PrimOpenOutputString implements the Scheme open-output-string primitive.
func PrimOutputPortOpenQ ¶
func PrimOutputPortOpenQ(mc machine.CallContext) error
PrimOutputPortOpenQ implements the output-port-open? primitive. Returns #t if the output port is open, #f otherwise.
R7RS §6.13.1: Returns #t if port is still open and capable of performing output.
func PrimPeekChar ¶
func PrimPeekChar(mc machine.CallContext) error
PrimPeekChar implements the peek-char primitive. R7RS §6.13.2: (peek-char [port]) Reads and returns a single character from the input port without consuming it.
func PrimPeekU8 ¶
func PrimPeekU8(mc machine.CallContext) error
PrimPeekU8 implements the peek-u8 primitive. R7RS §6.13.3: (peek-u8 [port]) Like read-u8, but does not consume the byte from the port.
func PrimRead ¶
func PrimRead(mc machine.CallContext) error
PrimRead implements the (read) primitive. Reads a Scheme datum from port. Reads from the current input port if no port is specified. R7RS §6.13.2: read uses datum labels to handle circular and shared structures.
func PrimReadBytevector ¶
func PrimReadBytevector(mc machine.CallContext) error
PrimReadBytevector implements the read-bytevector primitive. R7RS §6.13.3: (read-bytevector k [port]) Reads the next k bytes from port into a newly allocated bytevector. Returns eof-object if no bytes are available before end of file.
func PrimReadBytevectorBang ¶
func PrimReadBytevectorBang(mc machine.CallContext) error
PrimReadBytevectorBang implements the read-bytevector! primitive. R7RS §6.13.3: (read-bytevector! bytevector [port [start [end]]]) Reads bytes from port into an existing bytevector. Returns the number of bytes read, or eof-object if no bytes available.
func PrimReadChar ¶
func PrimReadChar(mc machine.CallContext) error
PrimReadChar implements the read-char primitive. R7RS §6.13.2: (read-char [port]) Reads and returns a single character from the input port.
func PrimReadLine ¶
func PrimReadLine(mc machine.CallContext) error
PrimReadLine implements the read-line primitive. R7RS §6.13.2: (read-line [port]) Reads a line of text from the input port, not including the line ending.
func PrimReadString ¶
func PrimReadString(mc machine.CallContext) error
PrimReadString implements the read-string primitive. R7RS §6.13.2: (read-string k [port]) Reads up to k characters from the input port and returns them as a string.
func PrimReadSyntax ¶
func PrimReadSyntax(mc machine.CallContext) error
PrimReadSyntax implements the (read-syntax) primitive. Reads datum with source information. Reads from the current input port if no port is specified.
func PrimReadToken ¶
func PrimReadToken(mc machine.CallContext) error
PrimReadToken implements the (read-token) primitive. Reads a single token from port. Reads from the current input port if no port is specified.
func PrimReadU8 ¶
func PrimReadU8(mc machine.CallContext) error
PrimReadU8 implements the read-u8 primitive. R7RS §6.13.3: (read-u8 [port]) Reads the next byte from the given binary input port and returns it as an exact integer. Returns eof-object at end of file.
func PrimTextualPortQ ¶
func PrimTextualPortQ(mc machine.CallContext) error
PrimTextualPortQ implements the textual-port? primitive. R7RS §6.13.1: Returns #t if the port is a textual port, #f otherwise.
func PrimU8ReadyQ ¶
func PrimU8ReadyQ(mc machine.CallContext) error
PrimU8ReadyQ implements the u8-ready? primitive. R7RS §6.13.3: (u8-ready? [port]) Returns #t if a byte is available for reading from the binary input port.
func PrimWrite ¶
func PrimWrite(mc machine.CallContext) error
PrimWrite implements the write primitive. Writes a machine-readable representation of an object to the current output port or to the specified port. R7RS §6.13.3: write uses datum labels to handle circular and shared structures.
func PrimWriteBytevector ¶
func PrimWriteBytevector(mc machine.CallContext) error
PrimWriteBytevector implements the write-bytevector primitive. R7RS §6.13.3: (write-bytevector bytevector [port [start [end]]]) Writes the bytes of bytevector to port.
func PrimWriteChar ¶
func PrimWriteChar(mc machine.CallContext) error
PrimWriteChar implements the write-char primitive. Writes a character to the current output port or to the specified output port.
func PrimWriteShared ¶
func PrimWriteShared(mc machine.CallContext) error
PrimWriteShared implements the write-shared primitive (R7RS). Writes a machine-readable representation of an object using datum labels (#n= and #n#) for shared and circular structure. R7RS §6.13.3: write-shared always uses datum labels for shared structure.
(write-shared obj) or (write-shared obj port)
func PrimWriteSimple ¶
func PrimWriteSimple(mc machine.CallContext) error
PrimWriteSimple implements the write-simple primitive (R7RS). Writes a machine-readable representation of an object without using datum labels for shared or circular structure. This is the same as write for non-circular data. (write-simple obj) or (write-simple obj port)
func PrimWriteString ¶
func PrimWriteString(mc machine.CallContext) error
PrimWriteString implements the write-string primitive. R7RS §6.13.3: (write-string string [port [start [end]]]) Writes the characters of string (optionally between start and end) to port.
func PrimWriteU8 ¶
func PrimWriteU8(mc machine.CallContext) error
PrimWriteU8 implements the write-u8 primitive. R7RS §6.13.3: (write-u8 byte [port]) Writes byte to the given binary output port and returns an unspecified value.
func ResetCurrentInputPort ¶
func ResetCurrentInputPort()
ResetCurrentInputPort resets the current input port to stdin. Used for testing.
func ResetCurrentOutputPort ¶
func ResetCurrentOutputPort()
ResetCurrentOutputPort resets the current output port to stdout. Used for testing.
func ResetState ¶
func ResetState()
ResetState resets the primitives state. Used for testing. Thread-safe: protected by stateMu and cacheMu.
func SetCurrentInputPort ¶
func SetCurrentInputPort(port values.TextualReader)
SetCurrentInputPort sets the current input port value. Used for testing.
func SetCurrentOutputPort ¶
func SetCurrentOutputPort(port values.OutputPort)
SetCurrentOutputPort sets the current output port value. Used for testing and parameterize.
func StringValue ¶
StringValue returns the display representation of a value. Uses String() if available (for human-readable output), otherwise SchemeString().
Types ¶
This section is empty.