cheax
Lisp dialect with C API
cheax.h
Go to the documentation of this file.
1 
10 #ifndef CHEAX_H
11 #define CHEAX_H
12 
13 #include <stdbool.h>
14 #include <stdint.h>
15 #include <stdio.h>
16 
17 #include <cheax/export.h>
18 
19 #ifdef __GNUC__
20 #define CHX_PURE __attribute__((pure))
21 #define CHX_CONST __attribute__((const))
22 #define CHX_FORMAT(like, first, args) __attribute__((format(like, first, args)))
23 #else
24 #define CHX_PURE
25 #define CHX_CONST
26 #define CHX_FORMAT(like, first, args)
27 #endif
28 
38 typedef struct cheax CHEAX;
39 
50 enum {
64 
78 
82 };
83 
85 #define CHX_INT_MIN INT_LEAST64_MIN
87 #define CHX_INT_MAX INT_LEAST64_MAX
88 
92 #define PRIdCHX PRIdLEAST64
93 
97 #define PRIiCHX PRIiLEAST64
98 
102 #define SCNdCHX SCNdLEAST64
103 
107 #define SCNiCHX SCNiLEAST64
108 
113 typedef int_least64_t chx_int;
114 
118 typedef double chx_double;
119 
120 struct chx_list;
121 struct chx_id;
122 struct chx_string;
123 struct chx_quote;
124 struct chx_func;
125 struct chx_ext_func;
126 struct chx_special_op;
127 struct chx_env;
128 
132 struct chx_value {
133  int type;
134 #if __STDC_VERSION__ >= 201112L
135  union {
136  chx_int as_int;
138  struct chx_list *as_list;
139  struct chx_id *as_id;
140  struct chx_string *as_string;
141  struct chx_quote *as_quote;
142  struct chx_func *as_func;
143  struct chx_ext_func *as_ext_func;
144  struct chx_special_op *as_special_op;
145  struct chx_env *as_env;
146  void *user_ptr;
147 
148  unsigned *rtflags_ptr;
149 #endif
150 
152  union {
158  struct chx_list *as_list;
160  struct chx_id *as_id;
162  struct chx_string *as_string;
167  struct chx_func *as_func;
171  struct chx_special_op *as_special_op;
173  struct chx_env *as_env;
175  void *user_ptr;
176 
178  unsigned *rtflags_ptr;
179  } data;
180 #if __STDC_VERSION__ + 0 >= 201112L
181  };
182 #endif
183 };
184 
186 #define CHEAX_NIL ((struct chx_value){ 0 })
187 
189 CHX_API struct chx_value cheax_nil(void) CHX_CONST;
190 
195 CHX_API bool cheax_is_nil(struct chx_value v) CHX_CONST;
196 
199 struct chx_id {
200  unsigned rtflags;
201  char *value;
202 };
203 
209 struct chx_quote {
210  unsigned rtflags;
211  struct chx_value value;
212 };
213 
220 CHX_API struct chx_value cheax_id(CHEAX *c, const char *id) CHX_PURE;
221 
225 #define cheax_id_value(X) ((struct chx_value){ .type = CHEAX_ID, .data.as_id = (X) })
226 
230 CHX_API struct chx_value cheax_id_value_proc(struct chx_id *id) CHX_CONST;
231 
238 #define cheax_int(X) ((struct chx_value){ .type = CHEAX_INT, .data.as_int = (X) })
239 
243 CHX_API struct chx_value cheax_int_proc(chx_int value) CHX_CONST;
244 
248 #define cheax_true() ((struct chx_value){ .type = CHEAX_BOOL, .data.as_int = 1 })
249 
253 #define cheax_false() ((struct chx_value){ .type = CHEAX_BOOL, .data.as_int = 0 })
254 
261 #define cheax_bool(X) ((struct chx_value){ .type = CHEAX_BOOL, .data.as_int = (X) ? 1 : 0 })
262 
266 CHX_API struct chx_value cheax_bool_proc(bool value) CHX_CONST;
267 
274 #define cheax_double(X) ((struct chx_value){ .type = CHEAX_DOUBLE, .data.as_double = (X) })
275 
279 CHX_API struct chx_value cheax_double_proc(chx_double value) CHX_CONST;
280 
287 struct chx_list {
288  unsigned rtflags;
289  struct chx_value value;
290  struct chx_list *next;
291 };
292 
300 CHX_API struct chx_value cheax_list(CHEAX *c, struct chx_value car, struct chx_list *cdr);
301 
305 #define cheax_list_value(X) ((struct chx_value){ .type = CHEAX_LIST, .data.as_list = (X) })
306 
310 CHX_API struct chx_value cheax_list_value_proc(struct chx_list *list) CHX_CONST;
311 
323 struct chx_func {
324  unsigned rtflags;
325  struct chx_value args;
326  struct chx_list *body;
327  struct chx_env *lexenv;
328 };
329 
330 #define cheax_func_value(X) ((struct chx_value){ .type = CHEAX_FUNC, .data.as_func = (X) })
331 CHX_API struct chx_value cheax_func_value_proc(struct chx_func *fn) CHX_CONST;
332 
346 typedef struct chx_value (*chx_func_ptr)(CHEAX *c, struct chx_list *args, void *info);
347 
348 enum {
350 };
351 
353  struct {
354  struct chx_value tail;
355  struct chx_env *pop_stop;
356  } ts;
357 
358  struct chx_value value;
359 };
360 
361 typedef int (*chx_tail_func_ptr)(CHEAX *c,
362  struct chx_list *args,
363  void *info,
364  struct chx_env *pop_stop,
365  union chx_eval_out *out);
366 
370 struct chx_ext_func {
371  unsigned rtflags;
372  const char *name;
374  void *info;
375 };
376 
386 CHX_API struct chx_value cheax_ext_func(CHEAX *c,
387  const char *name,
388  chx_func_ptr perform,
389  void *info);
390 
391 #define cheax_ext_func_value(X) ((struct chx_value){ .type = CHEAX_EXT_FUNC, .data.as_ext_func = (X) })
392 CHX_API struct chx_value cheax_ext_func_value_proc(struct chx_ext_func *sf) CHX_CONST;
393 
400 CHX_API struct chx_value cheax_quote(CHEAX *c, struct chx_value value);
401 
402 #define cheax_quote_value(X) ((struct chx_value){ .type = CHEAX_QUOTE, .data.as_quote = (X) })
403 CHX_API struct chx_value cheax_quote_value_proc(struct chx_quote *quote) CHX_CONST;
404 
411 CHX_API struct chx_value cheax_backquote(CHEAX *c, struct chx_value value);
412 
413 #define cheax_backquote_value(X) ((struct chx_value){ .type = CHEAX_BACKQUOTE, .data.as_quote = (X) })
414 CHX_API struct chx_value cheax_backquote_value_proc(struct chx_quote *bkquote) CHX_CONST;
415 
422 CHX_API struct chx_value cheax_comma(CHEAX *c, struct chx_value value);
423 
424 #define cheax_comma_value(X) ((struct chx_value){ .type = CHEAX_COMMA, .data.as_quote = (X) })
425 CHX_API struct chx_value cheax_comma_value_proc(struct chx_quote *comma) CHX_CONST;
426 
433 CHX_API struct chx_value cheax_splice(CHEAX *c, struct chx_value value);
434 
435 #define cheax_splice_value(X) ((struct chx_value){ .type = CHEAX_SPLICE, .data.as_quote = (X) })
436 CHX_API struct chx_value cheax_splice_value_proc(struct chx_quote *splice) CHX_CONST;
437 
442 struct chx_string;
443 
450 CHX_API size_t cheax_strlen(CHEAX *c, struct chx_string *str) CHX_PURE;
451 
458 CHX_API struct chx_value cheax_string(CHEAX *c, const char *value);
459 
467 CHX_API struct chx_value cheax_nstring(CHEAX *c, const char *value, size_t len);
468 
469 #define cheax_string_value(X) ((struct chx_value){ .type = CHEAX_STRING, .data.as_string = (X) })
470 CHX_API struct chx_value cheax_string_value_proc(struct chx_string *string) CHX_CONST;
471 
480 CHX_API struct chx_value cheax_substr(CHEAX *c, struct chx_string *str, size_t pos, size_t len);
481 
490 CHX_API char *cheax_strdup(struct chx_string *str);
491 
500 CHX_API struct chx_value cheax_user_ptr(CHEAX *c, void *value, int type);
501 
507 struct chx_env;
508 
516 CHX_API struct chx_value cheax_env(CHEAX *c);
517 
518 #define cheax_env_value(X) ((struct chx_value){ .type = CHEAX_ENV, .data.as_env = (X) })
519 CHX_API struct chx_value cheax_env_value_proc(struct chx_env *env) CHX_CONST;
520 
521 #if __STDC_VERSION__ + 0 >= 201112L
522 #define cheax_value(v) \
523  (_Generic((0,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))
532 #endif
533 
534 struct chx_sym;
535 
536 typedef struct chx_value (*chx_getter)(CHEAX *c, struct chx_sym *sym);
537 typedef void (*chx_setter)(CHEAX *c, struct chx_sym *sym, struct chx_value value);
538 typedef void (*chx_finalizer)(CHEAX *c, struct chx_sym *sym);
539 
541 struct chx_sym {
542  void *user_info;
547  struct chx_value protect;
549 };
550 
551 typedef int chx_ref;
552 
557 CHX_API chx_ref cheax_ref(CHEAX *c, struct chx_value value);
558 CHX_API chx_ref cheax_ref_ptr(CHEAX *c, void *obj);
559 
564 CHX_API void cheax_unref(CHEAX *c, struct chx_value value, chx_ref ref);
565 CHX_API void cheax_unref_ptr(CHEAX *c, void *obj, chx_ref ref);
566 
579 CHX_API int cheax_new_type(CHEAX *c, const char *name, int base_type);
580 
590 CHX_API int cheax_find_type(CHEAX *c, const char *name);
591 
598 CHX_API bool cheax_is_valid_type(CHEAX *c, int type) CHX_PURE;
599 
606 CHX_API bool cheax_is_basic_type(CHEAX *c, int type) CHX_PURE;
607 
614 CHX_API bool cheax_is_user_type(CHEAX *c, int type) CHX_PURE;
615 
632 CHX_API int cheax_get_base_type(CHEAX *c, int type);
633 
648 CHX_API int cheax_resolve_type(CHEAX *c, int type);
649 
661 enum {
662  CHEAX_ENOERR = 0x0000,
664  /* Read errors */
665  CHEAX_EREAD = 0x0001,
666  CHEAX_EEOF = 0x0002,
668  /* Eval/runtime errors */
669  CHEAX_EEVAL = 0x0101,
670  CHEAX_ENOSYM = 0x0102,
671  CHEAX_ESTACK = 0x0103,
672  CHEAX_ETYPE = 0x0104,
673  CHEAX_EMATCH = 0x0105,
674  CHEAX_ESTATIC = 0x0106,
675  CHEAX_EDIVZERO = 0x0107,
676  CHEAX_EREADONLY = 0x0108,
677  CHEAX_EWRITEONLY = 0x0109,
678  CHEAX_EEXIST = 0x010A,
679  CHEAX_EVALUE = 0x010B,
680  CHEAX_EOVERFLOW = 0x010C,
681  CHEAX_EINDEX = 0x010D,
682  CHEAX_EIO = 0x010E,
684  CHEAX_EAPI = 0x0200,
685  CHEAX_ENOMEM = 0x0201,
687  CHEAX_EUSER0 = 0x0400,
688 };
689 
690 #define ERR_NAME_PAIR(NAME) {#NAME, CHEAX_##NAME}
691 
692 #define CHEAX_BUILTIN_ERROR_NAMES(var) \
693 static const struct { const char *name; int code; } var[] = { \
694  ERR_NAME_PAIR(ENOERR), \
695  \
696  ERR_NAME_PAIR(EREAD), ERR_NAME_PAIR(EEOF), \
697  \
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), \
705  \
706  ERR_NAME_PAIR(EAPI), ERR_NAME_PAIR(ENOMEM) \
707 }
708 
714 CHX_API int cheax_errno(CHEAX *c) CHX_PURE;
715 
723 #define cheax_ft(c, pad) { if (cheax_errno(c) != 0) goto pad; }
724 
730 CHX_API void cheax_perror(CHEAX *c, const char *s);
731 
737 CHX_API void cheax_clear_errno(CHEAX *c);
738 
748 CHX_API void cheax_throw(CHEAX *c, int code, struct chx_string *msg);
749 CHX_API void cheax_throwf(CHEAX *c, int code, const char *fmt, ...) CHX_FORMAT(printf, 3, 4);
750 
751 CHX_API void cheax_add_bt(CHEAX *c);
752 
767 CHX_API int cheax_new_error_code(CHEAX *c, const char *name);
768 
778 CHX_API int cheax_find_error_code(CHEAX *c, const char *name);
779 
793 CHX_API CHEAX *cheax_init(void);
794 
798 CHX_API const char *cheax_version(void);
799 
822 CHX_API int cheax_load_feature(CHEAX *c, const char *feat);
823 
833 CHX_API int cheax_load_prelude(CHEAX *c);
834 
842 CHX_API void cheax_destroy(CHEAX *c);
843 
853 CHX_API int cheax_config_get_int(CHEAX *c, const char *opt);
854 
866 CHX_API int cheax_config_int(CHEAX *c, const char *opt, int value);
867 
877 CHX_API bool cheax_config_get_bool(CHEAX *c, const char *opt);
878 
890 CHX_API int cheax_config_bool(CHEAX *c, const char *opt, bool value);
891 
894  const char *name;
895  int type;
896  const char *metavar;
897  const char *help;
898 };
899 
908 CHX_API int cheax_config_help(struct chx_config_help **help, size_t *num_opts);
909 
928 CHX_API int cheax_list_to_array(CHEAX *c,
929  struct chx_list *list,
930  struct chx_value **array_ptr,
931  size_t *length);
932 
942 CHX_API struct chx_value cheax_array_to_list(CHEAX *c,
943  struct chx_value *array,
944  size_t length);
945 
951 enum {
952  CHEAX_SYNCED = 0x01,
953  CHEAX_READONLY = 0x02,
959 };
960 
964 CHX_API void cheax_push_env(CHEAX *c);
965 
973 CHX_API void cheax_enter_env(CHEAX *c, struct chx_env *main);
974 
980 CHX_API void cheax_pop_env(CHEAX *c);
981 
999 CHX_API struct chx_sym *cheax_defsym(CHEAX *c, const char *id,
1001  chx_finalizer fin, void *user_info);
1002 
1014 CHX_API void cheax_def(CHEAX *c, const char *id, struct chx_value value, int flags);
1015 
1031 CHX_API struct chx_value cheax_get(CHEAX *c, const char *id);
1032 
1048 CHX_API bool cheax_try_get(CHEAX *c, const char *id, struct chx_value *out);
1049 
1067 CHX_API struct chx_value cheax_get_from(CHEAX *c, struct chx_env *env, const char *id);
1068 
1086 CHX_API bool cheax_try_get_from(CHEAX *c,
1087  struct chx_env *env,
1088  const char *id,
1089  struct chx_value *out);
1090 
1111 CHX_API void cheax_set(CHEAX *c, const char *id, struct chx_value value);
1112 
1127 CHX_API void cheax_defun(CHEAX *c, const char *id, chx_func_ptr perform, void *info);
1128 CHX_API void cheax_defsyntax(CHEAX *c,
1129  const char *id,
1130  chx_tail_func_ptr perform,
1131  chx_func_ptr preproc,
1132  void *info);
1133 
1140 CHX_API void cheax_sync_int(CHEAX *c, const char *name, int *var, int flags);
1141 
1148 CHX_API void cheax_sync_bool(CHEAX *c, const char *name, bool *var, int flags);
1149 
1156 CHX_API void cheax_sync_float(CHEAX *c, const char *name, float *var, int flags);
1157 
1164 CHX_API void cheax_sync_double(CHEAX *c, const char *name, double *var, int flags);
1165 
1174 CHX_API void cheax_sync_nstring(CHEAX *c, const char *name, char *buf, size_t size, int flags);
1175 
1193 CHX_API bool cheax_match_in(CHEAX *c,
1194  struct chx_env *env,
1195  struct chx_value pan,
1196  struct chx_value match,
1197  int flags);
1198 
1213 CHX_API bool cheax_match(CHEAX *c, struct chx_value pan, struct chx_value match, int flags);
1214 
1222 CHX_API bool cheax_eq(CHEAX *c, struct chx_value l, struct chx_value r);
1223 CHX_API bool cheax_equiv(struct chx_value l, struct chx_value r);
1224 
1234 CHX_API struct chx_value cheax_cast(CHEAX *c, struct chx_value v, int type);
1235 
1255 CHX_API struct chx_value cheax_read(CHEAX *c, FILE *f);
1256 
1273 CHX_API struct chx_value cheax_read_at(CHEAX *c,
1274  FILE *f,
1275  const char *path,
1276  int *line,
1277  int *pos);
1278 
1301 CHX_API struct chx_value cheax_readstr(CHEAX *c, const char *str);
1302 
1327 CHX_API struct chx_value cheax_readstr_at(CHEAX *c,
1328  const char **str,
1329  const char *path,
1330  int *line,
1331  int *pos);
1332 
1345 CHX_API struct chx_value cheax_macroexpand(CHEAX *c, struct chx_value expr);
1346 
1359 CHX_API struct chx_value cheax_macroexpand_once(CHEAX *c, struct chx_value expr);
1360 
1361 CHX_API struct chx_value cheax_preproc(CHEAX *c, struct chx_value expr);
1362 
1376 CHX_API struct chx_value cheax_eval(CHEAX *c, struct chx_value expr);
1377 
1391 CHX_API struct chx_value cheax_apply(CHEAX *c, struct chx_value func, struct chx_list *list);
1392 
1402 CHX_API void cheax_print(CHEAX *c, FILE *output, struct chx_value expr);
1403 
1422 CHX_API struct chx_value cheax_format(CHEAX *c, struct chx_string *fmt, struct chx_list *args);
1423 
1428 CHX_API void cheax_exec(CHEAX *c, const char *f);
1429 
1430 CHX_API void *cheax_malloc(CHEAX *c, size_t size);
1431 CHX_API void *cheax_calloc(CHEAX *c, size_t nmemb, size_t size);
1432 CHX_API void *cheax_realloc(CHEAX *c, void *ptr, size_t size);
1433 CHX_API void cheax_free(CHEAX *c, void *ptr);
1434 CHX_API void cheax_gc(CHEAX *c);
1435 
1438 #endif
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_gc(CHEAX *c)
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.
Definition: cheax.h:352
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