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

Primitive (C function) type and unified callable dispatch. More...

#include "x-type.h"

Go to the source code of this file.

Macros

#define X_TYPE_PRIM_NAME   "PRIMITIVE"
 
Predicates
#define x_obj_type_isprim(B, X)   x_obj_is_type((B), (X), X_TYPE_PRIM_NAME)
 
Accessors
#define x_primval(X)   x_firstfn((X))
 
#define x_callable_state(X)   x_secondobj((X))
 
Constructors
#define x_mkprim(B, FN)   x_make_prim((B), X_OBJ_FLAG_NONE, (FN))
 
#define x_mkfprim(B, F, FN)   x_make_prim((B), (F), (FN))
 

Functions

x_obj_tx_make_prim (x_obj_t *p_base, x_obj_flag_t flags, x_fn_t fn)
 
x_obj_tx_type_prim_register (x_obj_t *p_base, x_obj_t *p_args)
 
x_obj_tx_type_prim_struct (x_obj_t *p_base, x_obj_t *p_args)
 
x_obj_tx_type_prim_make (x_obj_t *p_base, x_obj_t *p_args)
 
x_obj_tx_callable_call (x_obj_t *p_base, x_obj_t *p_args)
 
x_obj_tx_callable_apply (x_obj_t *p_base, x_obj_t *p_args)
 

Variables

Static Atoms
x_satom_t x_type_prim_name
 
x_satom_t x_type_prim_make_prim
 
x_satom_t x_callable_call_prim
 
x_satom_t x_type_prim_struct_prim
 

Detailed Description

Primitive (C function) type and unified callable dispatch.

A PRIMITIVE wraps a C function pointer (x_fn_t) as a first-class object. The callable_call / callable_apply functions provide unified dispatch for all callable types (primitives, closures, operatives, and type-internal handlers).

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

Macro Definition Documentation

◆ x_callable_state

#define x_callable_state (   X)    x_secondobj((X))

Access the state slot (second object) of a callable.

◆ x_mkfprim

#define x_mkfprim (   B,
  F,
  FN 
)    x_make_prim((B), (F), (FN))

Create a PRIMITIVE with explicit flags.

◆ x_mkprim

#define x_mkprim (   B,
  FN 
)    x_make_prim((B), X_OBJ_FLAG_NONE, (FN))

Create a PRIMITIVE with default flags.

◆ x_obj_type_isprim

#define x_obj_type_isprim (   B,
 
)    x_obj_is_type((B), (X), X_TYPE_PRIM_NAME)

Test whether X is a PRIMITIVE object.

◆ x_primval

#define x_primval (   X)    x_firstfn((X))

Extract the x_fn_t function pointer from a callable.

◆ X_TYPE_PRIM_NAME

#define X_TYPE_PRIM_NAME   "PRIMITIVE"

Canonical type name.

Function Documentation

◆ x_callable_apply()

x_obj_t * x_callable_apply ( x_obj_t p_base,
x_obj_t p_args 
)

Unified apply dispatch with TCO trampoline support.

Unified apply dispatch with TCO trampoline support.

Like x_callable_call() but uses the non-TCO apply path for procedures (args already evaluated) and a trampoline for operatives.

Parameters
p_baseBase (execution context).
p_argsArgument list: (callable . args).
Returns
Result of the applied function.
See also
x_callable_call
x_eval_tco_trampoline

◆ x_callable_call()

x_obj_t * x_callable_call ( x_obj_t p_base,
x_obj_t p_args 
)

Unified call dispatch for all callable types.

Unified call dispatch for all callable types.

All callables store a function pointer in slot 0:

  • Closures/operatives: fn-ptr is x_type_procedure_call / x_type_operative_call.
  • C primitives (spair): fn-ptr is the C function itself.
  • Type handlers (satom): fn-ptr is a type-internal handler (no self-passing).
Parameters
p_baseBase (execution context).
p_argsArgument list: (callable . args).
Returns
Result of the called function.

◆ x_make_prim()

x_obj_t * x_make_prim ( x_obj_t p_base,
x_obj_flag_t  flags,
x_fn_t  fn 
)

Allocate a PRIMITIVE object wrapping fn.

Allocate a PRIMITIVE object wrapping a C function pointer.

Parameters
p_baseBase (execution context).
flagsObject flags.
fnC function pointer to wrap.
Returns
Newly allocated PRIMITIVE object.

◆ x_type_prim_make()

x_obj_t * x_type_prim_make ( x_obj_t p_base,
x_obj_t p_args 
)

Type-system make handler for PRIMITIVE objects.

Type-system make handler for PRIMITIVE objects.

Extracts the function pointer from p_args[0] and optional flags from p_args[1], then allocates a pair-length object via x_obj_make().

Parameters
p_baseBase (execution context).
p_argsArgument list: (fn-atom [flags-atom]).
Returns
Newly allocated PRIMITIVE object.

◆ x_type_prim_register()

x_obj_t * x_type_prim_register ( x_obj_t p_base,
x_obj_t p_args 
)

Register (or retrieve) the PRIMITIVE type in the type alist.

Register (or retrieve) the PRIMITIVE type in the type alist.

Parameters
p_baseBase (execution context).
p_argsUnused.
Returns
The registered PRIMITIVE type object.

◆ x_type_prim_struct()

x_obj_t * x_type_prim_struct ( x_obj_t p_base,
x_obj_t p_args 
)

Build the PRIMITIVE type struct descriptor.

Variable Documentation

◆ x_callable_call_prim

x_satom_t x_callable_call_prim

Static atom: call handler.

◆ x_type_prim_make_prim

x_satom_t x_type_prim_make_prim

Static atom: make handler.

◆ x_type_prim_name

x_satom_t x_type_prim_name
extern

Static atom: type name.

◆ x_type_prim_struct_prim

x_satom_t x_type_prim_struct_prim

Static atom: struct constructor.