x-engine-c v0.2.13
The C engine for x-lang
Loading...
Searching...
No Matches
io.c File Reference

I/O primitives: read, write, display, string conversion, heap/GC, system, REPL. More...

#include "x-prim.h"
#include "x-eval.h"
#include "x-heap.h"
#include "x-token.h"
#include "x-type.h"
#include "x-type/buffer.h"
#include "x-type/char.h"
#include "x-type/int.h"
#include "x-type/prim.h"
#include "x-type/procedure.h"
#include "x-type/str.h"
#include "x-type/symbol.h"
#include "x-obj/prim.h"

Functions

static x_obj_tx_prim_write_str (x_obj_t *p_base, x_obj_t *p_args)
 
static x_obj_tx_prim_read_expr_raw (x_obj_t *p_base)
 
static x_obj_tx_prim_read_expr (x_obj_t *p_base, x_obj_t *p_args)
 
static x_obj_tx_prim_read_char (x_obj_t *p_base, x_obj_t *p_args)
 
x_obj_tx_prim_repl (x_obj_t *p_base, x_obj_t *p_args)
 
static x_obj_tx_prim_repl_read (x_obj_t *p_base, x_obj_t *p_args)
 
x_obj_tx_prim_io_register (x_obj_t *p_base, x_obj_t *p_args)
 

Detailed Description

I/O primitives: read, write, display, string conversion, heap/GC, system, REPL.

Author
Jon Ruttan (jonru.nosp@m.ttan.nosp@m.@gmai.nosp@m.l.co.nosp@m.m)

Function Documentation

◆ x_prim_io_register()

x_obj_t * x_prim_io_register ( x_obj_t p_base,
x_obj_t p_args 
)

Register I/O primitives into the environment.

Binds: write-str, read, read-char, heap-mark, heap-sweep, heap-count, gc-pin!, repl-read. Conditionally binds clock (when X_SYS_CLOCK defined). (The printers – write, display, write-to-str, display-to-str – and error-line are pure x-lang now: boot/printer.x renders over the (io write-str) OUT door; boot/reflect.x walks the error handler.)

Parameters
p_baseBase (execution context).
p_argsUnused.
Returns
The base object.

◆ x_prim_read_char()

static x_obj_t * x_prim_read_char ( x_obj_t p_base,
x_obj_t p_args 
)
static

Read one character from stdin. x-lang: (read-char)

Parameters
p_baseBase (execution context).
p_argsUnused.
Returns
Character object, or NULL on EOF.
See also
x_prim_read_expr

◆ x_prim_read_expr()

static x_obj_t * x_prim_read_expr ( x_obj_t p_base,
x_obj_t p_args 
)
static

Read one s-expression from stdin. x-lang: (read)

Answers the EOF sentinel at end of input, the value x-lang binds as token-eof. A top-level () reads as nil, so end of input has to be a value of its own for a loop to tell the two apart: a loop that stopped at nil stopped at the first () in its input. Callers that want nil at end of input read through x-lang's (Io read), which answers it.

Parameters
p_baseBase (execution context).
p_argsUnused.
Returns
Parsed s-expression, NULL for a nil value, or the EOF sentinel at end of input.
See also
x_prim_repl_read, x_prim_read_char

◆ x_prim_read_expr_raw()

static x_obj_t * x_prim_read_expr_raw ( x_obj_t p_base)
static

Read one s-expression from stdin, RAW: the EOF sentinel passes through, so loop callers can tell end of input from a nil VALUE (a top-level () reads as NULL by design).

Parameters
p_baseBase (execution context).
Returns
Parsed s-expression, NULL for a nil value, or x_token_eof_prim at end of input.

◆ x_prim_repl()

x_obj_t * x_prim_repl ( x_obj_t p_base,
x_obj_t p_args 
)

Minimal read-eval loop: reads and evaluates expressions until EOF.

Parameters
p_baseBase (execution context).
p_argsUnused.
Returns
NULL on EOF.
Note
No output, no prompt, no hooks. Used for C-level bootstrapping; the x-lang REPL operative in x-core.x provides the full experience.

◆ x_prim_repl_read()

static x_obj_t * x_prim_repl_read ( x_obj_t p_base,
x_obj_t p_args 
)
static

Read one expression, after resetting the source-line counter to 0.

The REPL uses this instead of plain read so that forms typed at the prompt are tagged with lines relative to THIS input rather than the cumulative boot+session stream. The reset must happen here, inside the read primitive: eval updates the line counter from each form's source-line metadata (x-eval.c) when it evaluates the (repl-read) call, so an earlier reset would be clobbered before the read runs. (include pushes its own counter, so file lines are unaffected.)

x-lang form:

(repl-read)
Parameters
p_baseBase (execution context).
p_argsUnused.
Returns
The expression read, nil for a nil value, or the EOF sentinel (token-eof) at clean end of input.

◆ x_prim_write_str()

static x_obj_t * x_prim_write_str ( x_obj_t p_base,
x_obj_t p_args 
)
static

Write a string's bytes to the current output: the OUT port instruction. x-lang: (write-str s)

Parameters
p_baseBase (execution context).
p_argsUnevaluated argument list (s).
Returns
NULL.
Note
The byte door the pure-X printer (lib/x/boot/printer.x) bottoms out at. Emits through x_base_write, so it respects the write-buffer capture stack exactly as the C renderers did.