cst_val.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*************************************************************************/
  2. /* */
  3. /* Language Technologies Institute */
  4. /* Carnegie Mellon University */
  5. /* Copyright (c) 1999 */
  6. /* All Rights Reserved. */
  7. /* */
  8. /* Permission is hereby granted, free of charge, to use and distribute */
  9. /* this software and its documentation without restriction, including */
  10. /* without limitation the rights to use, copy, modify, merge, publish, */
  11. /* distribute, sublicense, and/or sell copies of this work, and to */
  12. /* permit persons to whom this work is furnished to do so, subject to */
  13. /* the following conditions: */
  14. /* 1. The code must retain the above copyright notice, this list of */
  15. /* conditions and the following disclaimer. */
  16. /* 2. Any modifications must be clearly marked as such. */
  17. /* 3. Original authors' names are not deleted. */
  18. /* 4. The authors' names are not used to endorse or promote products */
  19. /* derived from this software without specific prior written */
  20. /* permission. */
  21. /* */
  22. /* CARNEGIE MELLON UNIVERSITY AND THE CONTRIBUTORS TO THIS WORK */
  23. /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
  24. /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
  25. /* SHALL CARNEGIE MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE */
  26. /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
  27. /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
  28. /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
  29. /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
  30. /* THIS SOFTWARE. */
  31. /* */
  32. /*************************************************************************/
  33. /* Author: Alan W Black (awb@cs.cmu.edu) */
  34. /* Date: December 1999 */
  35. /*************************************************************************/
  36. /* */
  37. /* Vals, typed objects */
  38. /* */
  39. /*************************************************************************/
  40. #ifndef _CST_VAL_H__
  41. #define _CST_VAL_H__
  42. #include "cst_file.h"
  43. #include "cst_string.h"
  44. #include "cst_error.h"
  45. #include "cst_alloc.h"
  46. #include "cst_val_defs.h"
  47. /* Only CONS can be an even number */
  48. #define CST_VAL_TYPE_CONS 0
  49. #define CST_VAL_TYPE_INT 1
  50. #define CST_VAL_TYPE_FLOAT 3
  51. #define CST_VAL_TYPE_STRING 5
  52. #define CST_VAL_TYPE_FIRST_FREE 7
  53. #define CST_VAL_TYPE_MAX 54
  54. typedef struct cst_val_cons_struct {
  55. struct cst_val_struct *car;
  56. struct cst_val_struct *cdr;
  57. } cst_val_cons;
  58. typedef struct cst_val_atom_struct {
  59. #ifdef WORDS_BIGENDIAN
  60. short ref_count;
  61. short type; /* order is here important */
  62. #else
  63. #if (defined(__x86_64__) || defined(_M_X64))
  64. int type; /* order is here important */
  65. int ref_count;
  66. #else
  67. short type; /* order is here important */
  68. short ref_count;
  69. #endif
  70. #endif
  71. union
  72. {
  73. #if (defined(__x86_64__) || defined(_M_X64))
  74. double fval;
  75. long long ival;
  76. void *vval;
  77. #else
  78. float fval;
  79. int ival;
  80. void *vval;
  81. #endif
  82. } v;
  83. } cst_val_atom;
  84. typedef struct cst_val_struct {
  85. union
  86. {
  87. cst_val_cons cc;
  88. cst_val_atom a;
  89. } c;
  90. } cst_val;
  91. typedef struct cst_val_def_struct {
  92. const char *name;
  93. void (*delete_function)(void *);
  94. } cst_val_def;
  95. /* Constructor functions */
  96. cst_val *int_val(int i);
  97. cst_val *float_val(float f);
  98. cst_val *string_val(const char *s);
  99. cst_val *val_new_typed(int type, void *vv);
  100. cst_val *cons_val(const cst_val *a, const cst_val *b);
  101. /* Derefence and delete val if no other references */
  102. void delete_val(cst_val *val);
  103. void delete_val_list(cst_val *val);
  104. /* Accessor functions */
  105. int val_int(const cst_val *v);
  106. float val_float(const cst_val *v);
  107. const char *val_string(const cst_val *v);
  108. void *val_void(const cst_val *v);
  109. void *val_generic(const cst_val *v, int type, const char *stype);
  110. const cst_val *val_car(const cst_val *v);
  111. const cst_val *val_cdr(const cst_val *v);
  112. const cst_val *set_cdr(cst_val *v1, const cst_val *v2);
  113. const cst_val *set_car(cst_val *v1, const cst_val *v2);
  114. int cst_val_consp(const cst_val *v);
  115. /* Unsafe accessor function -- for the brave and foolish */
  116. #define CST_VAL_STRING_LVAL(X) ((X)->c.a.v.vval)
  117. #define CST_VAL_TYPE(X) ((X)->c.a.type)
  118. #define CST_VAL_INT(X) ((X)->c.a.v.ival)
  119. #define CST_VAL_FLOAT(X) ((X)->c.a.v.fval)
  120. #define CST_VAL_STRING(X) ((const char *)(CST_VAL_STRING_LVAL(X)))
  121. #define CST_VAL_VOID(X) ((X)->c.a.v.vval)
  122. #define CST_VAL_CAR(X) ((X)->c.cc.car)
  123. #define CST_VAL_CDR(X) ((X)->c.cc.cdr)
  124. #define CST_VAL_REFCOUNT(X) ((X)->c.a.ref_count)
  125. /* Some standard function */
  126. int val_equal(const cst_val *a, const cst_val *b);
  127. int val_less(const cst_val *a, const cst_val *b);
  128. int val_greater(const cst_val *a, const cst_val *b);
  129. int val_member(const cst_val *a, const cst_val *b);
  130. int val_member_string (const char *a, const cst_val *b);
  131. int val_stringp(const cst_val *a);
  132. const cst_val *val_assoc_string(const char *v1,const cst_val *al);
  133. void val_print(cst_file fd,const cst_val *v);
  134. cst_val *val_readlist_string(const char *str);
  135. cst_val *val_reverse(cst_val *v);
  136. cst_val *val_append(cst_val *a,cst_val *b);
  137. int val_length(const cst_val *l);
  138. cst_val *cst_utf8_explode(const cst_string *utf8string);
  139. cst_string *cst_implode(const cst_val *string_list);
  140. cst_val *cst_utf8_ord(const cst_val *utf8_char);
  141. cst_val *cst_utf8_chr(const cst_val *ord);
  142. int cst_utf8_ord_string(const char *utf8_char);
  143. /* make sure you know what you are doing before you call these */
  144. int val_dec_refcount(const cst_val *b);
  145. cst_val *val_inc_refcount(const cst_val *b);
  146. #include "cst_val_const.h"
  147. extern const cst_val_def cst_val_defs[];
  148. /* Generic pointer vals */
  149. typedef void cst_userdata;
  150. CST_VAL_USER_TYPE_DCLS(userdata,cst_userdata)
  151. #endif