17 #include <cheax/export.h> 
   20 #define CHX_PURE                      __attribute__((pure)) 
   21 #define CHX_CONST                     __attribute__((const)) 
   22 #define CHX_FORMAT(like, first, args) __attribute__((format(like, first, args))) 
   26 #define CHX_FORMAT(like, first, args) 
   38 typedef struct cheax 
CHEAX;
 
   85 #define CHX_INT_MIN INT_LEAST64_MIN 
   87 #define CHX_INT_MAX INT_LEAST64_MAX 
   92 #define PRIdCHX PRIdLEAST64 
   97 #define PRIiCHX PRIiLEAST64 
  102 #define SCNdCHX SCNdLEAST64 
  107 #define SCNiCHX SCNiLEAST64 
  126 struct chx_special_op;
 
  134 #if __STDC_VERSION__ >= 201112L 
  180 #if __STDC_VERSION__ + 0 >= 201112L 
  186 #define CHEAX_NIL ((struct chx_value){ 0 }) 
  225 #define cheax_id_value(X) ((struct chx_value){ .type = CHEAX_ID, .data.as_id = (X) }) 
  238 #define cheax_int(X) ((struct chx_value){ .type = CHEAX_INT, .data.as_int = (X) }) 
  248 #define cheax_true() ((struct chx_value){ .type = CHEAX_BOOL, .data.as_int = 1 }) 
  253 #define cheax_false() ((struct chx_value){ .type = CHEAX_BOOL, .data.as_int = 0 }) 
  261 #define cheax_bool(X) ((struct chx_value){ .type = CHEAX_BOOL, .data.as_int = (X) ? 1 : 0 }) 
  274 #define cheax_double(X) ((struct chx_value){ .type = CHEAX_DOUBLE, .data.as_double = (X) }) 
  305 #define cheax_list_value(X) ((struct chx_value){ .type = CHEAX_LIST, .data.as_list = (X) }) 
  330 #define cheax_func_value(X) ((struct chx_value){ .type = CHEAX_FUNC, .data.as_func = (X) }) 
  364                                  struct chx_env *pop_stop,
 
  391 #define cheax_ext_func_value(X) ((struct chx_value){ .type = CHEAX_EXT_FUNC, .data.as_ext_func = (X) }) 
  402 #define cheax_quote_value(X) ((struct chx_value){ .type = CHEAX_QUOTE, .data.as_quote = (X) }) 
  413 #define cheax_backquote_value(X) ((struct chx_value){ .type = CHEAX_BACKQUOTE, .data.as_quote = (X) }) 
  424 #define cheax_comma_value(X) ((struct chx_value){ .type = CHEAX_COMMA, .data.as_quote = (X) }) 
  435 #define cheax_splice_value(X) ((struct chx_value){ .type = CHEAX_SPLICE, .data.as_quote = (X) }) 
  469 #define cheax_string_value(X) ((struct chx_value){ .type = CHEAX_STRING, .data.as_string = (X) }) 
  518 #define cheax_env_value(X) ((struct chx_value){ .type = CHEAX_ENV, .data.as_env = (X) }) 
  521 #if __STDC_VERSION__ + 0 >= 201112L 
  522 #define cheax_value(v)                                              \ 
  524                 int:                 cheax_int_proc,                \ 
  525                 chx_int:             cheax_int_proc,                \ 
  526                 double:              cheax_double_proc,             \ 
  527                 float:               cheax_double_proc,             \ 
  528                 struct chx_env *:    cheax_env_value_proc,          \ 
  529                 struct chx_id *:     cheax_id_value_proc,           \ 
  530                 struct chx_string *: cheax_string_value_proc,       \ 
  531                 struct chx_list *:   cheax_list_value_proc)(v)) 
  690 #define ERR_NAME_PAIR(NAME) {#NAME, CHEAX_##NAME} 
  692 #define CHEAX_BUILTIN_ERROR_NAMES(var)                        \ 
  693 static const struct { const char *name; int code; } var[] = { \ 
  694         ERR_NAME_PAIR(ENOERR),                                \ 
  696         ERR_NAME_PAIR(EREAD), ERR_NAME_PAIR(EEOF),            \ 
  698         ERR_NAME_PAIR(EEVAL), ERR_NAME_PAIR(ENOSYM),          \ 
  699         ERR_NAME_PAIR(ESTACK), ERR_NAME_PAIR(ETYPE),          \ 
  700         ERR_NAME_PAIR(EMATCH), ERR_NAME_PAIR(ESTATIC),        \ 
  701         ERR_NAME_PAIR(EDIVZERO), ERR_NAME_PAIR(EREADONLY),    \ 
  702         ERR_NAME_PAIR(EWRITEONLY), ERR_NAME_PAIR(EEXIST),     \ 
  703         ERR_NAME_PAIR(EVALUE), ERR_NAME_PAIR(EOVERFLOW),      \ 
  704         ERR_NAME_PAIR(EINDEX), ERR_NAME_PAIR(EIO),            \ 
  706         ERR_NAME_PAIR(EAPI), ERR_NAME_PAIR(ENOMEM)            \ 
  723 #define cheax_ft(c, pad) { if (cheax_errno(c) != 0) goto pad; } 
 1087                                 struct chx_env *env,
 
 1194                             struct chx_env *env,
 
void cheax_set(CHEAX *c, const char *id, struct chx_value value)
Sets the value of a symbol.
 
void cheax_print(CHEAX *c, FILE *output, struct chx_value expr)
Prints given value to file.
 
struct chx_value cheax_macroexpand(CHEAX *c, struct chx_value expr)
Expand given expression until it is no longer a macro form.
 
bool cheax_equiv(struct chx_value l, struct chx_value r)
 
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.
 
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,...
 
void cheax_exec(CHEAX *c, const char *f)
Reads a file and executes it.
 
void cheax_defsyntax(CHEAX *c, const char *id, chx_tail_func_ptr perform, chx_func_ptr preproc, void *info)
 
struct chx_value cheax_readstr(CHEAX *c, const char *str)
Reads value from string.
 
struct chx_value cheax_eval(CHEAX *c, struct chx_value expr)
Evaluates given cheax expression.
 
void cheax_pop_env(CHEAX *c)
Pops environment off environment stack.
 
void * cheax_malloc(CHEAX *c, size_t size)
 
void cheax_push_env(CHEAX *c)
Pushes new empty environment to environment stack.
 
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.
 
#define CHX_PURE
Definition: cheax.h:24
 
struct chx_value cheax_apply(CHEAX *c, struct chx_value func, struct chx_list *list)
Invokes function with given argument list.
 
struct chx_value cheax_array_to_list(CHEAX *c, struct chx_value *array, size_t length)
Converts array to chx_list.
 
void cheax_def(CHEAX *c, const char *id, struct chx_value value, int flags)
Creates a new symbol in the cheax environment.
 
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.
 
void cheax_enter_env(CHEAX *c, struct chx_env *main)
Pushes new bifurcated environment to environment stack.
 
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.
 
#define CHX_CONST
Definition: cheax.h:25
 
struct chx_value cheax_get(CHEAX *c, const char *id)
Obtains the value of the given symbol.
 
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.
 
bool cheax_try_get(CHEAX *c, const char *id, struct chx_value *out)
 
#define CHX_FORMAT(like, first, args)
Definition: cheax.h:26
 
void * cheax_realloc(CHEAX *c, void *ptr, size_t size)
 
struct chx_sym * cheax_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.
 
struct chx_value cheax_preproc(CHEAX *c, struct chx_value expr)
 
bool cheax_match(CHEAX *c, struct chx_value pan, struct chx_value match, int flags)
Matches a cheax expression to a given pattern.
 
struct chx_value cheax_cast(CHEAX *c, struct chx_value v, int type)
Attempts to cast an expression to a given type.
 
void cheax_free(CHEAX *c, void *ptr)
 
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.
 
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.
 
void cheax_defun(CHEAX *c, const char *id, chx_func_ptr perform, void *info)
Shorthand function to declare an external function the cheax environment.
 
bool cheax_eq(CHEAX *c, struct chx_value l, struct chx_value r)
Tests whether two given cheax expressions are equal.
 
struct chx_value cheax_macroexpand_once(CHEAX *c, struct chx_value expr)
Expand expression if it is a macro form.
 
struct chx_value cheax_read(CHEAX *c, FILE *f)
Reads value from file.
 
int cheax_list_to_array(CHEAX *c, struct chx_list *list, struct chx_value **array_ptr, size_t *length)
Converts chx_list to array.
 
struct cheax CHEAX
The type of the cheax virtual machine state, a pointer to wich is needed for most cheax API functions...
Definition: cheax.h:38
 
void * cheax_calloc(CHEAX *c, size_t nmemb, size_t size)
 
bool cheax_try_get_from(CHEAX *c, struct chx_env *env, const char *id, struct chx_value *out)
 
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.
 
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.
 
@ CHEAX_EVAL_NODES
For cheax_match() and cheax_match_in(): evaluate list nodes before matching them.
Definition: cheax.h:958
 
@ CHEAX_READONLY
Definition: cheax.h:953
 
@ CHEAX_SYNCED
Definition: cheax.h:952
 
@ CHEAX_WRITEONLY
Definition: cheax.h:954
 
void cheax_throw(CHEAX *c, int code, struct chx_string *msg)
Sets cheax_errno() to the given value.
 
void cheax_throwf(CHEAX *c, int code, const char *fmt,...) CHX_FORMAT(printf
 
int cheax_find_error_code(CHEAX *c, const char *name)
Looks up the value of a named error code.
 
int cheax_new_error_code(CHEAX *c, const char *name)
Creates a new error code with a given name.
 
void cheax_clear_errno(CHEAX *c)
Sets cheax_errno() to 0.
 
int cheax_errno(CHEAX *c) CHX_PURE
Gets the value of the current cheax error code.
 
void cheax_perror(CHEAX *c, const char *s)
Prints the current cheax error code and error message.
 
void void cheax_add_bt(CHEAX *c)
 
@ CHEAX_ETYPE
Definition: cheax.h:672
 
@ CHEAX_EMATCH
Definition: cheax.h:673
 
@ CHEAX_ENOMEM
Definition: cheax.h:685
 
@ CHEAX_ENOERR
Definition: cheax.h:662
 
@ CHEAX_EWRITEONLY
Definition: cheax.h:677
 
@ CHEAX_EAPI
Definition: cheax.h:684
 
@ CHEAX_EIO
Definition: cheax.h:682
 
@ CHEAX_EEXIST
Definition: cheax.h:678
 
@ CHEAX_EVALUE
Definition: cheax.h:679
 
@ CHEAX_EEVAL
Definition: cheax.h:669
 
@ CHEAX_EDIVZERO
Definition: cheax.h:675
 
@ CHEAX_EREADONLY
Definition: cheax.h:676
 
@ CHEAX_EEOF
Definition: cheax.h:666
 
@ CHEAX_ESTATIC
Definition: cheax.h:674
 
@ CHEAX_EINDEX
Definition: cheax.h:681
 
@ CHEAX_EREAD
Definition: cheax.h:665
 
@ CHEAX_ESTACK
Definition: cheax.h:671
 
@ CHEAX_EOVERFLOW
Definition: cheax.h:680
 
@ CHEAX_EUSER0
Definition: cheax.h:687
 
@ CHEAX_ENOSYM
Definition: cheax.h:670
 
void cheax_destroy(CHEAX *c)
Destroys a cheax virtual machine instance, freeing its resources.
 
int cheax_load_feature(CHEAX *c, const char *feat)
Loads extra functions or language features into the cheax environment, including 'unsafe' ones.
 
int cheax_config_int(CHEAX *c, const char *opt, int value)
Set value of integer configuration option.
 
int cheax_config_bool(CHEAX *c, const char *opt, bool value)
Set value of boolean configuration option.
 
const char * cheax_version(void)
Returns cheax library version as a string in the static storage class.
 
CHEAX * cheax_init(void)
Initializes a new cheax virtual machine instance.
 
int cheax_config_help(struct chx_config_help **help, size_t *num_opts)
Load information about all cheax config options.
 
int cheax_load_prelude(CHEAX *c)
Loads the cheax standard library.
 
bool cheax_config_get_bool(CHEAX *c, const char *opt)
Get value of boolean configuration option.
 
int cheax_config_get_int(CHEAX *c, const char *opt)
Get value of integer configuration option.
 
struct chx_value cheax_comma(CHEAX *c, struct chx_value value)
Creates a cheax comma expression.
 
struct chx_value cheax_ext_func(CHEAX *c, const char *name, chx_func_ptr perform, void *info)
Creates a cheax external/user function expression.
 
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.
 
struct chx_value cheax_nstring(CHEAX *c, const char *value, size_t len)
Creates a cheax string expression of given length.
 
struct chx_value cheax_splice(CHEAX *c, struct chx_value value)
Creates a cheax comma splice expression.
 
struct chx_value cheax_id(CHEAX *c, const char *id) CHX_PURE
Creates a chx_value of type CHEAX_ID.
 
size_t cheax_strlen(CHEAX *c, struct chx_string *str) CHX_PURE
Size of string in number of bytes.
 
char * cheax_strdup(struct chx_string *str)
Allocates a null-terminated copy of given chx_string.
 
struct chx_value cheax_string_value_proc(struct chx_string *string) CHX_CONST
 
struct chx_value cheax_splice_value_proc(struct chx_quote *splice) 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.
 
struct chx_value cheax_string(CHEAX *c, const char *value)
Creates a cheax string expression.
 
double chx_double
Floating point type.
Definition: cheax.h:118
 
int(* chx_tail_func_ptr)(CHEAX *c, struct chx_list *args, void *info, struct chx_env *pop_stop, union chx_eval_out *out)
Definition: cheax.h:361
 
struct chx_value cheax_env(CHEAX *c)
Currently active chx_env.
 
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.
 
struct chx_value cheax_ext_func_value_proc(struct chx_ext_func *sf) 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.
 
void(* chx_finalizer)(CHEAX *c, struct chx_sym *sym)
Definition: cheax.h:538
 
struct chx_value cheax_quote_value_proc(struct chx_quote *quote) CHX_CONST
 
int_least64_t chx_int
Integer type.
Definition: cheax.h:113
 
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.
 
int cheax_resolve_type(CHEAX *c, int type)
Resolves the basic type to which a given type code refers.
 
bool cheax_is_user_type(CHEAX *c, int type) CHX_PURE
Checks whether a given type code is a user-defined type code.
 
struct chx_value cheax_quote(CHEAX *c, struct chx_value value)
Creates a quoted cheax expression.
 
bool cheax_is_nil(struct chx_value v) CHX_CONST
Tests whether given value is nil.
 
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(...
 
struct chx_value cheax_list(CHEAX *c, struct chx_value car, struct chx_list *cdr)
Creates a list.
 
struct chx_value cheax_backquote_value_proc(struct chx_quote *bkquote) CHX_CONST
 
struct chx_value cheax_user_ptr(CHEAX *c, void *value, int type)
Creates a cheax user pointer expression.
 
struct chx_value cheax_func_value_proc(struct chx_func *fn) CHX_CONST
 
chx_ref cheax_ref_ptr(CHEAX *c, void *obj)
 
int cheax_get_base_type(CHEAX *c, int type)
Gets the base type for a given type.
 
struct chx_value cheax_backquote(CHEAX *c, struct chx_value value)
Creates a backquoted cheax expression.
 
struct chx_value cheax_env_value_proc(struct chx_env *env) CHX_CONST
 
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.
 
void(* chx_setter)(CHEAX *c, struct chx_sym *sym, struct chx_value value)
Definition: cheax.h:537
 
void cheax_unref_ptr(CHEAX *c, void *obj, chx_ref ref)
 
struct chx_value cheax_comma_value_proc(struct chx_quote *comma) CHX_CONST
 
bool cheax_is_valid_type(CHEAX *c, int type) CHX_PURE
Checks whether a given type code is valid.
 
bool cheax_is_basic_type(CHEAX *c, int type) CHX_PURE
Checks whether a given type code is a basic type.
 
struct chx_value(* chx_func_ptr)(CHEAX *c, struct chx_list *args, void *info)
Type for C functions to be invoked from cheax.
Definition: cheax.h:346
 
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.
 
struct chx_value(* chx_getter)(CHEAX *c, struct chx_sym *sym)
Definition: cheax.h:536
 
struct chx_value cheax_nil(void) CHX_CONST
Creates a nil value.
 
int cheax_find_type(CHEAX *c, const char *name)
Looks up the type code of a named type.
 
int chx_ref
Definition: cheax.h:551
 
int cheax_new_type(CHEAX *c, const char *name, int base_type)
Creates a new type code as an alias for another.
 
@ CHEAX_LIST
Definition: cheax.h:51
 
@ CHEAX_BACKQUOTE
Definition: cheax.h:70
 
@ CHEAX_TYPESTORE_BIAS
Definition: cheax.h:77
 
@ CHEAX_LAST_BASIC_TYPE
Definition: cheax.h:76
 
@ CHEAX_FUNC
Definition: cheax.h:66
 
@ CHEAX_USER_PTR
Type of user pointers defined from outside the cheax environment.
Definition: cheax.h:63
 
@ CHEAX_STRING
Definition: cheax.h:73
 
@ CHEAX_EXT_FUNC
Definition: cheax.h:67
 
@ CHEAX_TYPECODE
Definition: cheax.h:80
 
@ CHEAX_SPECIAL_OP
Definition: cheax.h:68
 
@ CHEAX_COMMA
Definition: cheax.h:71
 
@ CHEAX_INT
Definition: cheax.h:52
 
@ CHEAX_ENV
Definition: cheax.h:74
 
@ CHEAX_DOUBLE
Definition: cheax.h:54
 
@ CHEAX_ERRORCODE
Definition: cheax.h:81
 
@ CHEAX_QUOTE
Definition: cheax.h:69
 
@ CHEAX_ID
Definition: cheax.h:65
 
@ CHEAX_BOOL
Definition: cheax.h:53
 
@ CHEAX_SPLICE
Definition: cheax.h:72
 
@ CHEAX_TAIL_OUT
Definition: cheax.h:349
 
@ CHEAX_VALUE_OUT
Definition: cheax.h:349
 
Information about cheax config option.
Definition: cheax.h:893
 
const char * help
Definition: cheax.h:897
 
const char * metavar
Definition: cheax.h:896
 
int type
Definition: cheax.h:895
 
const char * name
Definition: cheax.h:894
 
Cheax external/user function expression.
Definition: cheax.h:370
 
const char * name
Definition: cheax.h:372
 
void * info
Definition: cheax.h:374
 
unsigned rtflags
Definition: cheax.h:371
 
chx_func_ptr perform
Definition: cheax.h:373
 
Function or macro type.
Definition: cheax.h:323
 
struct chx_value args
Definition: cheax.h:325
 
struct chx_env * lexenv
Definition: cheax.h:327
 
struct chx_list * body
Definition: cheax.h:326
 
unsigned rtflags
Definition: cheax.h:324
 
Identifier type.
Definition: cheax.h:199
 
char * value
Definition: cheax.h:201
 
unsigned rtflags
Definition: cheax.h:200
 
List type.
Definition: cheax.h:287
 
struct chx_value value
Definition: cheax.h:289
 
unsigned rtflags
Definition: cheax.h:288
 
struct chx_list * next
Definition: cheax.h:290
 
Quoted value type.
Definition: cheax.h:209
 
unsigned rtflags
Definition: cheax.h:210
 
struct chx_value value
Definition: cheax.h:211
 
Custom symbol.
Definition: cheax.h:541
 
chx_finalizer fin
Definition: cheax.h:546
 
struct chx_value protect
Definition: cheax.h:547
 
chx_setter set
Definition: cheax.h:545
 
void * user_info
Definition: cheax.h:542
 
chx_getter get
Definition: cheax.h:544
 
Represents a value in the cheax environment.
Definition: cheax.h:132
 
void * user_ptr
Data when type is CHEAX_USER_PTR.
Definition: cheax.h:175
 
struct chx_env * as_env
Data when type is CHEAX_ENV.
Definition: cheax.h:173
 
chx_double as_double
Data when type is CHEAX_DOUBLE.
Definition: cheax.h:156
 
struct chx_list * as_list
Data when type is CHEAX_LIST.
Definition: cheax.h:158
 
struct chx_id * as_id
Data when type is CHEAX_ID.
Definition: cheax.h:160
 
struct chx_ext_func * as_ext_func
Data when type is CHEAX_SPECIAL_OP or CHEAX_EXT_FUNC.
Definition: cheax.h:170
 
struct chx_string * as_string
Data when type is CHEAX_STRING.
Definition: cheax.h:162
 
unsigned * rtflags_ptr
Runtime flags.
Definition: cheax.h:178
 
struct chx_quote * as_quote
Data when type is CHEAX_QUOTE, CHEAX_COMMA or CHEAX_SPLICE..
Definition: cheax.h:165
 
int type
Definition: cheax.h:133
 
struct chx_special_op * as_special_op
Definition: cheax.h:171
 
chx_int as_int
Data when type is CHEAX_INT or CHEAX_BOOL.
Definition: cheax.h:154
 
struct chx_func * as_func
Data when type is CHEAX_FUNC.
Definition: cheax.h:167
 
union chx_value::@4 data
Data stored in the value.
 
struct chx_env * pop_stop
Definition: cheax.h:355
 
struct chx_value value
Definition: cheax.h:358
 
struct chx_value tail
Definition: cheax.h:354
 
struct chx_eval_out::@5 ts