io

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 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, 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, 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

This section is empty.

Variables

View Source
var (

	// Tokenizers caches tokenizers per input port.
	//
	// These are strong references: closing a port does not evict its cache
	// entry, so the tokenizer (and its internal buffers) remain reachable
	// as long as the port value itself is reachable as a map key. In
	// long-running programs that open many transient ports, this can leak
	// memory. If that becomes a problem, switch to weak.Pointer[T] (Go 1.24+)
	// or add explicit eviction on port close.
	//
	// To diagnose: run with GODEBUG=gctrace=1 or use runtime/pprof to
	// inspect heap; look for tokenizer.Tokenizer / parser.Parser objects
	// retained via this map.
	Tokenizers map[values.Value]*tokenizer.Tokenizer
	// Parsers caches parsers per input port.
	// Same retention caveat as Tokenizers above.
	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,
)

Builder aggregates all I/O registration functions.

Extension is the I/O extension.

Functions

func GetCurrentErrorPort

func GetCurrentErrorPort() *values.CharacterOutputPort

GetCurrentErrorPort returns the current error port from the parameter.

func GetCurrentErrorPortParam

func GetCurrentErrorPortParam() *machine.Parameter

GetCurrentErrorPortParam returns the current-error-port parameter object.

func GetCurrentInputPort

func GetCurrentInputPort() *values.CharacterInputPort

GetCurrentInputPort returns the current input port from the parameter.

func GetCurrentInputPortParam

func GetCurrentInputPortParam() *machine.Parameter

GetCurrentInputPortParam returns the current-input-port parameter object.

func GetCurrentOutputPort

func GetCurrentOutputPort() *values.CharacterOutputPort

GetCurrentOutputPort returns the current output port from the parameter.

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.

func PrimBinaryPortQ

func PrimBinaryPortQ(_ context.Context, mc *machine.MachineContext) error

PrimBinaryPortQ implements the binary-port? primitive. R7RS §6.13.1: Returns #t if the port is a binary port, #f otherwise.

func PrimCallWithPort

func PrimCallWithPort(_ context.Context, mc *machine.MachineContext) error

PrimCallWithPort implements the call-with-port primitive. R7RS §6.13.1: (call-with-port port proc) Calls proc with port as an argument. When proc returns, the port is closed.

func PrimCharReadyQ

func PrimCharReadyQ(_ context.Context, mc *machine.MachineContext) 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 PrimClosePort

func PrimClosePort(_ context.Context, mc *machine.MachineContext) 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(_ context.Context, mc *machine.MachineContext) 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(_ context.Context, mc *machine.MachineContext) error

PrimEofObject implements the (eof-object) primitive. Returns the EOF object.

func PrimEofObjectQ

func PrimEofObjectQ(_ context.Context, mc *machine.MachineContext) error

PrimEofObjectQ implements the (eof-object?) primitive. Returns #t if the argument is the EOF object.

func PrimFlushOutputPort

func PrimFlushOutputPort(_ context.Context, mc *machine.MachineContext) 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(_ context.Context, mc *machine.MachineContext) error

PrimGetOutputBytevector implements the Scheme get-output-bytevector primitive.

func PrimGetOutputString

func PrimGetOutputString(_ context.Context, mc *machine.MachineContext) error

PrimGetOutputString implements the Scheme get-output-string primitive.

func PrimInputPortOpenQ

func PrimInputPortOpenQ(_ context.Context, mc *machine.MachineContext) 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 PrimInputPortQ

func PrimInputPortQ(_ context.Context, mc *machine.MachineContext) error

PrimInputPortQ implements the (input-port?) primitive. Returns #t if argument is input port.

R7RS §6.13.1: Returns #t if obj is an input port, otherwise returns #f.

func PrimNewline

func PrimNewline(_ context.Context, mc *machine.MachineContext) error

PrimNewline implements the newline primitive. Writes a newline character to the output port.

func PrimOpenInputBytevector

func PrimOpenInputBytevector(_ context.Context, mc *machine.MachineContext) error

PrimOpenInputBytevector implements the Scheme open-input-bytevector primitive.

func PrimOpenInputString

func PrimOpenInputString(_ context.Context, mc *machine.MachineContext) error

PrimOpenInputString implements the Scheme open-input-string primitive.

func PrimOpenOutputBytevector

func PrimOpenOutputBytevector(_ context.Context, mc *machine.MachineContext) error

PrimOpenOutputBytevector implements the Scheme open-output-bytevector primitive.

func PrimOpenOutputString

func PrimOpenOutputString(_ context.Context, mc *machine.MachineContext) error

PrimOpenOutputString implements the Scheme open-output-string primitive.

func PrimOutputPortOpenQ

func PrimOutputPortOpenQ(_ context.Context, mc *machine.MachineContext) 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 PrimOutputPortQ

func PrimOutputPortQ(_ context.Context, mc *machine.MachineContext) error

PrimOutputPortQ implements the output-port? primitive. Returns #t if the argument is an output port, #f otherwise.

R7RS §6.13.1: Returns #t if obj is an output port, otherwise returns #f.

func PrimPeekChar

func PrimPeekChar(_ context.Context, mc *machine.MachineContext) 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(_ context.Context, mc *machine.MachineContext) 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 PrimPortQ

func PrimPortQ(_ context.Context, mc *machine.MachineContext) error

PrimPortQ implements the port? primitive. Returns #t if the argument is a port (input or output), #f otherwise.

R7RS §6.13.1: Returns #t if obj is a port, otherwise returns #f.

func PrimRead

func PrimRead(ctx context.Context, mc *machine.MachineContext) 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(_ context.Context, mc *machine.MachineContext) 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(_ context.Context, mc *machine.MachineContext) 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(_ context.Context, mc *machine.MachineContext) 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(_ context.Context, mc *machine.MachineContext) 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(_ context.Context, mc *machine.MachineContext) 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(ctx context.Context, mc *machine.MachineContext) 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(_ context.Context, mc *machine.MachineContext) 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(_ context.Context, mc *machine.MachineContext) 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(_ context.Context, mc *machine.MachineContext) 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(_ context.Context, mc *machine.MachineContext) 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(_ context.Context, mc *machine.MachineContext) 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(_ context.Context, mc *machine.MachineContext) 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(_ context.Context, mc *machine.MachineContext) 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(_ context.Context, mc *machine.MachineContext) 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(_ context.Context, mc *machine.MachineContext) 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(_ context.Context, mc *machine.MachineContext) 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(_ context.Context, mc *machine.MachineContext) 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.

func SetCurrentInputPort

func SetCurrentInputPort(port *values.CharacterInputPort)

SetCurrentInputPort sets the current input port value. Used for testing.

func SetCurrentOutputPort

func SetCurrentOutputPort(port *values.CharacterOutputPort)

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