|
x-engine-c v0.2.31
The C engine for x-lang
|
Type system field accessors and type struct definition. More...
#include "x-eval.h"Go to the source code of this file.
Data Structures | |
| struct | x_type_t |
| C-side mirror of a type descriptor's fields. More... | |
Functions | |
| x_int_t | x_type_units_count (x_obj_t *p_units) |
| x_int_t | x_type_units_mask (x_obj_t *p_units) |
| x_int_t | x_type_units_described (x_obj_t *p_units) |
| int | x_type_unit_label (x_int_t mask, x_int_t i, x_int_t described) |
| x_obj_t * | x_type_prim_length (x_obj_t *p_base, x_obj_t *p_args) |
Type Heap Callbacks | |
| x_obj_t * | x_type_heap_mark (x_obj_t *p_base, x_obj_t *p_obj, x_obj_flag_t flags) |
| void | x_type_heap_free (x_obj_t *p_base, x_obj_t *p_obj) |
Type Functions | |
| x_satom_t | x_type_save_default_prim |
| x_obj_t * | x_type_struct_make (x_obj_t *p_base, struct x_type_t type) |
| x_obj_t * | x_type_save_default (x_obj_t *p_base, x_obj_t *p_args) |
| The default save: the units the type's unit labels declare, each with the label the mask gives it (a bare count: all references). Args are already evaluated: (obj buf). | |
| x_obj_t * | x_type_save_units (x_obj_t *p_obj, x_int_t *buf, x_int_t n, const int *labels, int nlabels) |
| int | x_type_op_try (x_obj_t *p_base, x_char_t *op, x_obj_t *p_a, x_obj_t *p_b, x_obj_t **pp_result) |
| x_obj_t * | x_type_struct_get (x_obj_t *p_base, x_obj_t *p_args) |
| x_obj_t * | x_type_prim_type_name (x_obj_t *p_base, x_obj_t *p_args) |
| x_obj_t * | x_type_prim_units (x_obj_t *p_base, x_obj_t *p_args) |
Type system field accessors and type struct definition.
Defines the x_type_t struct that mirrors the pair-tree layout of a type descriptor, along with macros to navigate each field (name, data, heap, proc, cvt, io, iter). Each field has a _stack variant returning the full (current . saved) cell, and a bare variant returning the current value.
Type descriptor pair-tree layout (all leaf fields are stack-wrapped):
Every field is stack-wrapped: stored as (current . saved). The _stack macros return the cell, the bare macros return x_firstobj(cell) (the current value). Stack-wrapping enables type-push-write / type-pop-write to temporarily override handlers (e.g. for write-to-str capture).
| #define x_mktype | ( | B, | |
| T, | |||
| X | |||
| ) | x_obj_make((B), (T), 0, 1, (X)) |
Allocate a new type object with the given payload.
| #define x_type_arg_type | ( | X | ) | x_firstobj((X)) |
Extract the type object from a type-dispatch argument list.
| #define X_TYPE_NO_CVT_TEXT "no declared promotion; declare the cvt relation for" |
The op-arbitration refusal (#584); x_eval_error appends the type name.
| #define x_type_settypes | ( | B, | |
| X | |||
| ) | (x_type_types((B)) = (X)) |
Set the type alist on a base object.
| #define x_type_types | ( | B | ) | (x_firstobj((B))) |
Get the type alist from a base object.
| #define X_TYPE_UNIT_BITS 2 |
Mask bits per unit.
| #define X_TYPE_UNIT_BYTES 2 |
A pointer to bytes the type can measure.
| #define X_TYPE_UNIT_DESCRIBED_MAX ((x_int_t)(sizeof(x_int_t) * 8 / X_TYPE_UNIT_BITS)) |
Units a mask can describe before the repeat rule takes over.
| #define X_TYPE_UNIT_FOREIGN 3 |
An address C owns.
| #define X_TYPE_UNIT_LABEL_MASK 3 |
Low-bit mask of one label field.
| #define X_TYPE_UNIT_REF 0 |
A heap object pointer; the GC traces it.
| #define X_TYPE_UNIT_WORD 1 |
An immediate; nothing dereferences it.
GC free callback – free a typed object's resources.
GC free hook for typed heap objects.
If the object's type has a free callback, invokes it to release type-specific resources before the heap cell is reclaimed.
| p_base | x_obj_t* – Base (execution context) |
| p_args | x_obj_t* – Argument run: (object) – the object being freed |
| x_obj_t * x_type_heap_mark | ( | x_obj_t * | p_base, |
| x_obj_t * | p_obj, | ||
| x_obj_flag_t | flags | ||
| ) |
GC mark callback – mark a typed object and its contents.
GC mark hook for typed heap objects.
For child base objects (type == x_eval_obj), returns the data pointer so the GC traverses the base's pair tree. For custom types, calls the type's mark callback if present, a primitive or a closure, with the object and the flags. Otherwise falls back to a generic N-slot traversal using the units count, which marks every reference unit but the last and returns that one for the caller's loop to take, so a list's rest is followed without a C frame per pair.
| p_base | x_obj_t* – Base (execution context) |
| p_args | x_obj_t* – Argument run: (object, flags) – the object being marked, and the GC mark flags |
| int x_type_op_try | ( | x_obj_t * | p_base, |
| x_char_t * | op, | ||
| x_obj_t * | p_a, | ||
| x_obj_t * | p_b, | ||
| x_obj_t ** | pp_result | ||
| ) |
Try generic-operator dispatch for a binary op; 1 if dispatched.
Primitive: return the length of an object.
Return the length for an object. x-lang: (length obj)
Dispatches to pair or atom length primitives for built-in types. For custom types, calls the type's length hook function.
| p_base | x_obj_t* – Base (execution context) |
| p_args | x_obj_t* – Argument run: (object) |
Primitive: return the name of an object's type.
Return the type name for an object. x-lang: (type-name obj)
For stack atoms/pairs and untyped objects, returns the raw type pointer. For heap-typed objects, extracts the name from the type struct.
| p_base | x_obj_t* – Base (execution context) |
| p_args | x_obj_t* – Argument run: (object) |
Primitive: return the units (element size) of an object.
Return the unit count for an object. x-lang: (units obj)
Dispatches to pair or atom unit primitives for built-in types. For custom types, reads the type's units count.
| p_base | x_obj_t* – Base (execution context) |
| p_args | x_obj_t* – Argument run: (object) |
The default save: the units the type's unit labels declare, each with the label the mask gives it (a bare count: all references). Args are already evaluated: (obj buf).
The default save handler: every unit the type's unit labels declare.
| x_obj_t * x_type_save_units | ( | x_obj_t * | p_obj, |
| x_int_t * | buf, | ||
| x_int_t | n, | ||
| const int * | labels, | ||
| int | nlabels | ||
| ) |
Write n at buf[0] and the units of p_obj as [label][word] pairs after it, labels from labels (the last repeats); every C save handler's helper. Returns p_obj.
Look up a type struct from a type-dispatch argument list.
Retrieve or create a type struct by name.
First checks the type alist cache. On miss, calls the callable in rest of p_args to construct the type, then caches it in the type alist for future lookups.
| p_base | x_obj_t* – Base (execution context) |
| p_args | x_obj_t* – (type-name . constructor-callable) |
Build a type pair tree from a C x_type_t struct.
Construct a type struct from a type descriptor.
Builds the canonical nested-pair structure that represents a type in the type alist: name-stack, data-stack, heap fields (mark, make, free, clone, units, length), proc fields (call, eval), cvt fields (from, to), IO fields (analyse, delimit, read, write, display, error), iter fields, and ops fields.
| p_base | x_obj_t* – Base (execution context) (for allocation) |
| type | struct x_type_t – Type descriptor with all hook pointers |
The label of unit i, applying the repeat rule past described.
The label of unit i.
Units at or past described take the label of the last described unit. That is the repeat rule, and it is how a dynamic-size type says what its payload units are without a marker.
| mask | x_int_t – The label mask |
| i | x_int_t – Unit index |
| described | x_int_t – Fields the mask describes |
The declared unit count from either form of a p_units slot.
The declared unit count, from either form of a p_units slot.
A bare INT atom is the count itself; a pair is (count . mask) and the count is its first. Negative keeps its dynamic-size meaning in both.
| p_units | x_obj_t* – A type's p_units slot, or NULL |
How many leading units a p_units slot's mask describes.
How many leading units the mask describes before the repeat rule applies.
A fixed count describes exactly its own units. A dynamic count of -k describes the k leading units plus the label of the payload that follows them – k + 1 fields – so a count of -1 with mask (ref, ref) covers slot 0 and the slot-0-many payload units after it, with no repeat marker.
Note that the slot-0-counted convention holds its length as a heap INTEGER OBJECT, so slot 0 is X_TYPE_UNIT_REF, not X_TYPE_UNIT_WORD – see the label documentation in x-type.h.
| p_units | x_obj_t* – A type's p_units slot, or NULL |
The label mask from either form of a p_units slot (0 = all references).
The per-unit label mask, from either form of a p_units slot.
The bare-count form has no mask; 0 is the honest answer for it, since X_TYPE_UNIT_REF is 0 and "every unit a reference" is what a bare count has always meant.
| p_units | x_obj_t* – A type's p_units slot, or NULL |
|
extern |