io

package
v1.13.21 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 13, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

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

View Source
const (
	// MaxReadBytevectorBytes is the maximum size of bytevector that
	// read-bytevector can allocate (100 MB).
	MaxReadBytevectorBytes = 100 * 1024 * 1024 // 100 MB
)
View Source
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

View Source
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.

View Source
var AddToRegistry = Builder.AddToRegistry

AddToRegistry registers all I/O primitives.

View Source
var Builder = registry.NewRegistryBuilder(
	addReadWrite,
	addPorts,
	addPortState,
	addPortProcs,
)

Builder aggregates all I/O registration functions.

View Source
var Extension = registry.NewDescribedExtension("io",
	"I/O ports: reading, writing, string/bytevector ports, display, write.",
	AddToRegistry)

Extension is the I/O extension.

View Source
var PrimInputPortQ = helpers.MakeTypePredicate(func(o values.Value) bool {
	_, ok := o.(values.InputPort)
	return ok
})
View Source
var PrimOutputPortQ = helpers.MakeTypePredicate(func(o values.Value) bool {
	_, ok := o.(values.OutputPort)
	return ok
})
View Source
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

func GetCurrentErrorPortParam() *machine.Parameter

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

func GetCurrentInputPortParam() *machine.Parameter

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

func GetCurrentOutputPortParam() *machine.Parameter

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

func StringValue(o values.Value) string

StringValue returns the display representation of a value. Uses String() if available (for human-readable output), otherwise SchemeString().

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL