cheax
Lisp dialect with C API
cheax.h File Reference

The header for the cheax C API. More...

#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <cheax/export.h>
Include dependency graph for cheax.h:

Go to the source code of this file.

Data Structures

struct  chx_value
 Represents a value in the cheax environment. More...
 
struct  chx_id
 Identifier type. More...
 
struct  chx_quote
 Quoted value type. More...
 
struct  chx_list
 List type. More...
 
struct  chx_func
 Function or macro type. More...
 
union  chx_eval_out
 
struct  chx_ext_func
 Cheax external/user function expression. More...
 
struct  chx_sym
 Custom symbol. More...
 
struct  chx_config_help
 Information about cheax config option. More...
 

Macros

#define CHX_PURE
 
#define CHX_CONST
 
#define CHX_FORMAT(like, first, args)
 
#define CHX_INT_MIN   INT_LEAST64_MIN
 Minimum value for chx_int. More...
 
#define CHX_INT_MAX   INT_LEAST64_MAX
 Maximum value for chx_int. More...
 
#define PRIdCHX   PRIdLEAST64
 Conversion specifier to print chx_int (decimal). More...
 
#define PRIiCHX   PRIiLEAST64
 Conversion specifier to print chx_int (decimal). More...
 
#define SCNdCHX   SCNdLEAST64
 Conversion specifier to scan chx_int (decimal). More...
 
#define SCNiCHX   SCNiLEAST64
 Conversion specifier to scan chx_int (decimal). More...
 
#define CHEAX_NIL   ((struct chx_value){ 0 })
 The nil value. More...
 
#define cheax_id_value(X)   ((struct chx_value){ .type = CHEAX_ID, .data.as_id = (X) })
 Turns chx_id into chx_value. More...
 
#define cheax_int(X)   ((struct chx_value){ .type = CHEAX_INT, .data.as_int = (X) })
 Creates a chx_value of type CHEAX_INT. More...
 
#define cheax_true()   ((struct chx_value){ .type = CHEAX_BOOL, .data.as_int = 1 })
 Creates chx_value true. More...
 
#define cheax_false()   ((struct chx_value){ .type = CHEAX_BOOL, .data.as_int = 0 })
 Creates chx_value false. More...
 
#define cheax_bool(X)   ((struct chx_value){ .type = CHEAX_BOOL, .data.as_int = (X) ? 1 : 0 })
 Creates chx_value of type CHEAX_BOOL. More...
 
#define cheax_double(X)   ((struct chx_value){ .type = CHEAX_DOUBLE, .data.as_double = (X) })
 Creates a chx_value of type CHEAX_DOUBLE. More...
 
#define cheax_list_value(X)   ((struct chx_value){ .type = CHEAX_LIST, .data.as_list = (X) })
 Turns chx_list into chx_value. More...
 
#define cheax_func_value(X)   ((struct chx_value){ .type = CHEAX_FUNC, .data.as_func = (X) })
 
#define cheax_ext_func_value(X)   ((struct chx_value){ .type = CHEAX_EXT_FUNC, .data.as_ext_func = (X) })
 
#define cheax_quote_value(X)   ((struct chx_value){ .type = CHEAX_QUOTE, .data.as_quote = (X) })
 
#define cheax_backquote_value(X)   ((struct chx_value){ .type = CHEAX_BACKQUOTE, .data.as_quote = (X) })
 
#define cheax_comma_value(X)   ((struct chx_value){ .type = CHEAX_COMMA, .data.as_quote = (X) })
 
#define cheax_splice_value(X)   ((struct chx_value){ .type = CHEAX_SPLICE, .data.as_quote = (X) })
 
#define cheax_string_value(X)   ((struct chx_value){ .type = CHEAX_STRING, .data.as_string = (X) })
 
#define cheax_env_value(X)   ((struct chx_value){ .type = CHEAX_ENV, .data.as_env = (X) })
 
#define ERR_NAME_PAIR(NAME)   {#NAME, CHEAX_##NAME}
 
#define CHEAX_BUILTIN_ERROR_NAMES(var)
 
#define cheax_ft(c, pad)   { if (cheax_errno(c) != 0) goto pad; }
 Macro to fall through to a pad in case of an error. More...
 

Typedefs

typedef struct cheax CHEAX
 The type of the cheax virtual machine state, a pointer to wich is needed for most cheax API functions. More...
 
typedef int_least64_t chx_int
 Integer type. More...
 
typedef double chx_double
 Floating point type. More...
 
typedef struct chx_value(* chx_func_ptr) (CHEAX *c, struct chx_list *args, void *info)
 Type for C functions to be invoked from cheax. More...
 
typedef int(* chx_tail_func_ptr) (CHEAX *c, struct chx_list *args, void *info, struct chx_env *pop_stop, union chx_eval_out *out)
 
typedef struct chx_value(* chx_getter) (CHEAX *c, struct chx_sym *sym)
 
typedef void(* chx_setter) (CHEAX *c, struct chx_sym *sym, struct chx_value value)
 
typedef void(* chx_finalizer) (CHEAX *c, struct chx_sym *sym)
 
typedef int chx_ref
 

Enumerations

enum  {
  CHEAX_LIST , CHEAX_INT , CHEAX_BOOL , CHEAX_DOUBLE ,
  CHEAX_USER_PTR , CHEAX_ID , CHEAX_FUNC , CHEAX_EXT_FUNC ,
  CHEAX_SPECIAL_OP , CHEAX_QUOTE , CHEAX_BACKQUOTE , CHEAX_COMMA ,
  CHEAX_SPLICE , CHEAX_STRING , CHEAX_ENV , CHEAX_LAST_BASIC_TYPE = CHEAX_ENV ,
  CHEAX_TYPESTORE_BIAS , CHEAX_TYPECODE = CHEAX_TYPESTORE_BIAS + 0 , CHEAX_ERRORCODE
}
 Types of expressions within cheax. More...
 
enum  { CHEAX_VALUE_OUT , CHEAX_TAIL_OUT }
 
enum  {
  CHEAX_ENOERR = 0x0000 , CHEAX_EREAD = 0x0001 , CHEAX_EEOF = 0x0002 , CHEAX_EEVAL = 0x0101 ,
  CHEAX_ENOSYM = 0x0102 , CHEAX_ESTACK = 0x0103 , CHEAX_ETYPE = 0x0104 , CHEAX_EMATCH = 0x0105 ,
  CHEAX_ESTATIC = 0x0106 , CHEAX_EDIVZERO = 0x0107 , CHEAX_EREADONLY = 0x0108 , CHEAX_EWRITEONLY = 0x0109 ,
  CHEAX_EEXIST = 0x010A , CHEAX_EVALUE = 0x010B , CHEAX_EOVERFLOW = 0x010C , CHEAX_EINDEX = 0x010D ,
  CHEAX_EIO = 0x010E , CHEAX_EAPI = 0x0200 , CHEAX_ENOMEM = 0x0201 , CHEAX_EUSER0 = 0x0400
}
 Pre-defined cheax error codes. More...
 
enum  { CHEAX_SYNCED = 0x01 , CHEAX_READONLY = 0x02 , CHEAX_WRITEONLY = 0x04 , CHEAX_EVAL_NODES = 0x08 }
 Options for symbol declaration and value matching. More...
 

Functions

struct chx_value cheax_nil (void) CHX_CONST
 Creates a nil value. More...
 
bool cheax_is_nil (struct chx_value v) CHX_CONST
 Tests whether given value is nil. More...
 
struct chx_value cheax_id (CHEAX *c, const char *id) CHX_PURE
 Creates a chx_value of type CHEAX_ID. More...
 
struct chx_value cheax_id_value_proc (struct chx_id *id) CHX_CONST
 Turns chx_id into chx_value. Like cheax_id_value(), but a function and not a macro. More...
 
struct chx_value cheax_int_proc (chx_int value) CHX_CONST
 Creates a chx_value of type CHEAX_INT. Like cheax_int(), but a function and not a macro. More...
 
struct chx_value cheax_bool_proc (bool value) CHX_CONST
 Creates a chx_value of type CHEAX_BOOL. Like cheax_bool(), but a function and not a macro. More...
 
struct chx_value cheax_double_proc (chx_double value) CHX_CONST
 Creates a chx_value of type CHEAX_DOUBLE. Like cheax_double(), but a function and not a macro. More...
 
struct chx_value cheax_list (CHEAX *c, struct chx_value car, struct chx_list *cdr)
 Creates a list. More...
 
struct chx_value cheax_list_value_proc (struct chx_list *list) CHX_CONST
 Turns chx_list into chx_value. Like cheax_list_value(), but a function and not a macro. More...
 
struct chx_value cheax_func_value_proc (struct chx_func *fn) CHX_CONST
 
struct chx_value cheax_ext_func (CHEAX *c, const char *name, chx_func_ptr perform, void *info)
 Creates a cheax external/user function expression. More...
 
struct chx_value cheax_ext_func_value_proc (struct chx_ext_func *sf) CHX_CONST
 
struct chx_value cheax_quote (CHEAX *c, struct chx_value value)
 Creates a quoted cheax expression. More...
 
struct chx_value cheax_quote_value_proc (struct chx_quote *quote) CHX_CONST
 
struct chx_value cheax_backquote (CHEAX *c, struct chx_value value)
 Creates a backquoted cheax expression. More...
 
struct chx_value cheax_backquote_value_proc (struct chx_quote *bkquote) CHX_CONST
 
struct chx_value cheax_comma (CHEAX *c, struct chx_value value)
 Creates a cheax comma expression. More...
 
struct chx_value cheax_comma_value_proc (struct chx_quote *comma) CHX_CONST
 
struct chx_value cheax_splice (CHEAX *c, struct chx_value value)
 Creates a cheax comma splice expression. More...
 
struct chx_value cheax_splice_value_proc (struct chx_quote *splice) CHX_CONST
 
size_t cheax_strlen (CHEAX *c, struct chx_string *str) CHX_PURE
 Size of string in number of bytes. More...
 
struct chx_value cheax_string (CHEAX *c, const char *value)
 Creates a cheax string expression. More...
 
struct chx_value cheax_nstring (CHEAX *c, const char *value, size_t len)
 Creates a cheax string expression of given length. More...
 
struct chx_value cheax_string_value_proc (struct chx_string *string) CHX_CONST
 
struct chx_value cheax_substr (CHEAX *c, struct chx_string *str, size_t pos, size_t len)
 Takes substring of given cheax string. More...
 
char * cheax_strdup (struct chx_string *str)
 Allocates a null-terminated copy of given chx_string. More...
 
struct chx_value cheax_user_ptr (CHEAX *c, void *value, int type)
 Creates a cheax user pointer expression. More...
 
struct chx_value cheax_env (CHEAX *c)
 Currently active chx_env. More...
 
struct chx_value cheax_env_value_proc (struct chx_env *env) CHX_CONST
 
chx_ref cheax_ref (CHEAX *c, struct chx_value value)
 Increase reference count on cheax value, preventing it from gc deletion when cheax_eval() is called. More...
 
chx_ref cheax_ref_ptr (CHEAX *c, void *obj)
 
void cheax_unref (CHEAX *c, struct chx_value value, chx_ref ref)
 Decrease reference count on cheax value, potentially allowing it to be deleted by gc when cheax_eval() is called. More...
 
void cheax_unref_ptr (CHEAX *c, void *obj, chx_ref ref)
 
int cheax_new_type (CHEAX *c, const char *name, int base_type)
 Creates a new type code as an alias for another. More...
 
int cheax_find_type (CHEAX *c, const char *name)
 Looks up the type code of a named type. More...
 
bool cheax_is_valid_type (CHEAX *c, int type) CHX_PURE
 Checks whether a given type code is valid. More...
 
bool cheax_is_basic_type (CHEAX *c, int type) CHX_PURE
 Checks whether a given type code is a basic type. More...
 
bool cheax_is_user_type (CHEAX *c, int type) CHX_PURE
 Checks whether a given type code is a user-defined type code. More...
 
int cheax_get_base_type (CHEAX *c, int type)
 Gets the base type for a given type. More...
 
int cheax_resolve_type (CHEAX *c, int type)
 Resolves the basic type to which a given type code refers. More...
 
int cheax_errno (CHEAX *c) CHX_PURE
 Gets the value of the current cheax error code. More...
 
void cheax_perror (CHEAX *c, const char *s)
 Prints the current cheax error code and error message. More...
 
void cheax_clear_errno (CHEAX *c)
 Sets cheax_errno() to 0. More...
 
void cheax_throw (CHEAX *c, int code, struct chx_string *msg)
 Sets cheax_errno() to the given value. More...
 
void cheax_throwf (CHEAX *c, int code, const char *fmt,...) CHX_FORMAT(printf
 
void void cheax_add_bt (CHEAX *c)
 
int cheax_new_error_code (CHEAX *c, const char *name)
 Creates a new error code with a given name. More...
 
int cheax_find_error_code (CHEAX *c, const char *name)
 Looks up the value of a named error code. More...
 
CHEAXcheax_init (void)
 Initializes a new cheax virtual machine instance. More...
 
const char * cheax_version (void)
 Returns cheax library version as a string in the static storage class. More...
 
int cheax_load_feature (CHEAX *c, const char *feat)
 Loads extra functions or language features into the cheax environment, including 'unsafe' ones. More...
 
int cheax_load_prelude (CHEAX *c)
 Loads the cheax standard library. More...
 
void cheax_destroy (CHEAX *c)
 Destroys a cheax virtual machine instance, freeing its resources. More...
 
int cheax_config_get_int (CHEAX *c, const char *opt)
 Get value of integer configuration option. More...
 
int cheax_config_int (CHEAX *c, const char *opt, int value)
 Set value of integer configuration option. More...
 
bool cheax_config_get_bool (CHEAX *c, const char *opt)
 Get value of boolean configuration option. More...
 
int cheax_config_bool (CHEAX *c, const char *opt, bool value)
 Set value of boolean configuration option. More...
 
int cheax_config_help (struct chx_config_help **help, size_t *num_opts)
 Load information about all cheax config options. More...
 
int cheax_list_to_array (CHEAX *c, struct chx_list *list, struct chx_value **array_ptr, size_t *length)
 Converts chx_list to array. More...
 
struct chx_value cheax_array_to_list (CHEAX *c, struct chx_value *array, size_t length)
 Converts array to chx_list. More...
 
void cheax_push_env (CHEAX *c)
 Pushes new empty environment to environment stack. More...
 
void cheax_enter_env (CHEAX *c, struct chx_env *main)
 Pushes new bifurcated environment to environment stack. More...
 
void cheax_pop_env (CHEAX *c)
 Pops environment off environment stack. More...
 
struct chx_symcheax_defsym (CHEAX *c, const char *id, chx_getter get, chx_setter set, chx_finalizer fin, void *user_info)
 Define symbol with custom getter and setter. More...
 
void cheax_def (CHEAX *c, const char *id, struct chx_value value, int flags)
 Creates a new symbol in the cheax environment. More...
 
struct chx_value cheax_get (CHEAX *c, const char *id)
 Obtains the value of the given symbol. More...
 
bool cheax_try_get (CHEAX *c, const char *id, struct chx_value *out)
 
struct chx_value cheax_get_from (CHEAX *c, struct chx_env *env, const char *id)
 Retrieves the value of the given symbol, performing symbol lookup only in the specified environment. More...
 
bool cheax_try_get_from (CHEAX *c, struct chx_env *env, const char *id, struct chx_value *out)
 
void cheax_set (CHEAX *c, const char *id, struct chx_value value)
 Sets the value of a symbol. More...
 
void cheax_defun (CHEAX *c, const char *id, chx_func_ptr perform, void *info)
 Shorthand function to declare an external function the cheax environment. More...
 
void cheax_defsyntax (CHEAX *c, const char *id, chx_tail_func_ptr perform, chx_func_ptr preproc, void *info)
 
void cheax_sync_int (CHEAX *c, const char *name, int *var, int flags)
 Synchronizes a variable from C with a symbol in the cheax environment. More...
 
void cheax_sync_bool (CHEAX *c, const char *name, bool *var, int flags)
 Synchronizes a variable from C with a symbol in the cheax environment. More...
 
void cheax_sync_float (CHEAX *c, const char *name, float *var, int flags)
 Synchronizes a variable from C with a symbol in the cheax environment. More...
 
void cheax_sync_double (CHEAX *c, const char *name, double *var, int flags)
 Synchronizes a variable from C with a symbol in the cheax environment. More...
 
void cheax_sync_nstring (CHEAX *c, const char *name, char *buf, size_t size, int flags)
 Synchronizes a null-terminated string buffer from C with a symbol in the cheax environment. More...
 
bool cheax_match_in (CHEAX *c, struct chx_env *env, struct chx_value pan, struct chx_value match, int flags)
 Matches a cheax expression to a given pattern. More...
 
bool cheax_match (CHEAX *c, struct chx_value pan, struct chx_value match, int flags)
 Matches a cheax expression to a given pattern. More...
 
bool cheax_eq (CHEAX *c, struct chx_value l, struct chx_value r)
 Tests whether two given cheax expressions are equal. More...
 
bool cheax_equiv (struct chx_value l, struct chx_value r)
 
struct chx_value cheax_cast (CHEAX *c, struct chx_value v, int type)
 Attempts to cast an expression to a given type. More...
 
struct chx_value cheax_read (CHEAX *c, FILE *f)
 Reads value from file. More...
 
struct chx_value cheax_read_at (CHEAX *c, FILE *f, const char *path, int *line, int *pos)
 Reads value from file and reports back line and column information. More...
 
struct chx_value cheax_readstr (CHEAX *c, const char *str)
 Reads value from string. More...
 
struct chx_value cheax_readstr_at (CHEAX *c, const char **str, const char *path, int *line, int *pos)
 Reads value from string, updates the string to reference the byte where it left off reading, and reports back line and column information. More...
 
struct chx_value cheax_macroexpand (CHEAX *c, struct chx_value expr)
 Expand given expression until it is no longer a macro form. More...
 
struct chx_value cheax_macroexpand_once (CHEAX *c, struct chx_value expr)
 Expand expression if it is a macro form. More...
 
struct chx_value cheax_preproc (CHEAX *c, struct chx_value expr)
 
struct chx_value cheax_eval (CHEAX *c, struct chx_value expr)
 Evaluates given cheax expression. More...
 
struct chx_value cheax_apply (CHEAX *c, struct chx_value func, struct chx_list *list)
 Invokes function with given argument list. More...
 
void cheax_print (CHEAX *c, FILE *output, struct chx_value expr)
 Prints given value to file. More...
 
struct chx_value cheax_format (CHEAX *c, struct chx_string *fmt, struct chx_list *args)
 Expresses given cheax values as a chx_string, using given format string. More...
 
void cheax_exec (CHEAX *c, const char *f)
 Reads a file and executes it. More...
 
void * cheax_malloc (CHEAX *c, size_t size)
 
void * cheax_calloc (CHEAX *c, size_t nmemb, size_t size)
 
void * cheax_realloc (CHEAX *c, void *ptr, size_t size)
 
void cheax_free (CHEAX *c, void *ptr)
 
void cheax_gc (CHEAX *c)
 

Detailed Description

The header for the cheax C API.