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

Sandbox-base primitives – make-base, base-eval (setjmp cross-base eval), base-bind, make-token-base, base-make-type. More...

#include "x-prim.h"
#include "x-alist.h"
#include "x-eval.h"
#include "x-env.h"
#include "x-heap.h"
#include "x-type.h"
#include <setjmp.h>
#include "x-token.h"
#include "x-type/buffer.h"
#include "x-type/char.h"
#include "x-type/err.h"
#include "x-type/comment.h"
#include "x-type/int.h"
#include "x-type/list.h"
#include "x-type/operative.h"
#include "x-type/prim.h"
#include "x-type/procedure.h"
#include "x-type/ptr.h"
#include "x-type/str.h"
#include "x-type/symbol.h"
#include "x-type/whitespace.h"

Functions

static x_obj_tx_prim_base_make_type (x_obj_t *p_base, x_obj_t *p_args)
 Create a type on a target base (cross-base type registration).
 
static void x_base_attach_read_buffer (x_obj_t *p_new)
 Give a base the reader buffer the tokenizer reads through.
 
static x_obj_tx_prim_make_token_base (x_obj_t *p_base, x_obj_t *p_args)
 Create a bare base suitable for tokenization only.
 
static x_obj_tx_prim_make_base (x_obj_t *p_base, x_obj_t *p_args)
 Create a fully initialized sandboxed interpreter base.
 
static x_obj_tx_prim_base_eval (x_obj_t *p_base, x_obj_t *p_args)
 Evaluate an expression in a target base's environment.
 
static x_obj_tx_prim_base_bind (x_obj_t *p_base, x_obj_t *p_args)
 Bind a name-value pair in a target base's environment.
 
static x_obj_tx_prim_define_global (x_obj_t *p_base, x_obj_t *p_args)
 Bind a name in the base's ROOT environment, whatever environment is current.
 
x_obj_tx_prim_base_register (x_obj_t *p_base, x_obj_t *p_args)
 

Detailed Description

Sandbox-base primitives – make-base, base-eval (setjmp cross-base eval), base-bind, make-token-base, base-make-type.

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

Function Documentation

◆ x_base_attach_read_buffer()

static void x_base_attach_read_buffer ( x_obj_t p_new)
static

Give a base the reader buffer the tokenizer reads through.

Both base constructors need this and each used to spell it out, which is how make-tok came to be missing it entirely: an empty input happened to work, because nothing was ever read, and the first character dereferenced a buffer that was never made.

Attaching rather than allocating-and-returning keeps each caller's existing order intact – make-base registers its types before this runs, and that ordering is not something to disturb while fixing a crash.

Parameters
p_newThe base to attach a reader buffer to.

◆ x_prim_base_bind()

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

Bind a name-value pair in a target base's environment.

x-lang form:

(base-bind base name value)

Binds name to value in the target base's ROOT environment, where every environment in that base reaches it: the door for handing a child base a capability.

Parameters
p_baseCalling execution context.
p_argsUnevaluated: (self target-base name value).
Returns
The bound value.

◆ x_prim_base_eval()

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

Evaluate an expression in a target base's environment.

x-lang form:

(base-eval base expr)

Pushes a setjmp-based error handler onto the target base's error handler stack, evaluates p_expr in the target, then pops the handler. If an error occurs in the target, it is caught, the handler is popped, the environment is restored, and the error is propagated to the calling base's error handler (or printed if none exists).

Parameters
p_baseCalling execution context.
p_argsUnevaluated: (self target-base expr).
Returns
Result of evaluating expr in the target base, or NULL on error.
Note
Uses setjmp/longjmp for error propagation across bases.

◆ x_prim_base_make_type()

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

Create a type on a target base (cross-base type registration).

x-lang form:

(base-make-type base name handlers)

Like make-type, but registers the type on p_target rather than the calling base. Pins the type struct as SHARED so the calling base's GC will not sweep the name atom or the handler closures the target now refers to from its own heap chain.

Parameters
p_baseCalling execution context (used for handler closure allocation).
p_argsUnevaluated: (self target-base name-string handlers-alist).
Returns
The type name atom.
Note
Sets X_OBJ_FLAG_SHARED on the target base and on the type struct to prevent cross-base GC.
See also
x_prim_make_type

◆ x_prim_base_register()

x_obj_t * x_prim_base_register ( x_obj_t p_base,
x_obj_t p_args 
)

Register the sandbox base primitives.

◆ x_prim_define_global()

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

Bind a name in the base's ROOT environment, whatever environment is current.

x-lang form:

((prim-ref 'base 'def-global) name value)

This is def with the root named instead of the current environment, and it is expressible without a primitive now that environments are values: a def evaluated with the root as its environment binds there. It stays for one release so the langs that call it through the catalog keep working until they move to that spelling; its manifest row goes with it.

Parameters
p_baseBase (execution context).
p_argsUnevaluated: (self name value); both are evaluated.
Returns
The bound value.
See also
x_env_bind

◆ x_prim_make_base()

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

Create a fully initialized sandboxed interpreter base.

x-lang form:

(make-base)

Allocates a new base, registers all built-in types (prim, operative, procedure, symbol, list, int, str, char, whitespace, comment), sets up a read buffer, and registers all C primitives. The result is a complete interpreter context that can be evaluated into via base-eval.

Parameters
p_baseBase (execution context) (unused beyond allocation).
p_argsUnused.
Returns
Fully bootstrapped base object.
See also
x_prim_base_eval

◆ x_prim_make_token_base()

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

Create a bare base suitable for tokenization only.

x-lang form:

(make-token-base)

Allocates a minimal base with no types or primitives registered, inheriting only the boolean singletons (t/f) from the calling base. Used for custom tokenizer type registration on an isolated base.

Parameters
p_baseBase (execution context) (boolean singletons are inherited).
p_argsUnused.
Returns
New bare base object.
See also
x_prim_make_base