PageRenderTime 57ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/object.c

http://blisp.googlecode.com/
C | 224 lines | 123 code | 32 blank | 69 comment | 32 complexity | c0995de8be34c8084df88b076cfdb003 MD5 | raw file
  1. #include "blisp.h"
  2. /*
  3. * Miscellaneous objects & type objects
  4. * Type, Nil, Empty list, Booleans
  5. */
  6. /* locations of type, nil, empty list, and boolean objects and type objects */
  7. Type Type_t;
  8. Object Nil_o;
  9. Type Nil_t;
  10. Object Empty_o;
  11. Type Empty_t;
  12. Object True_o;
  13. Object False_o;
  14. Type Boolean_t;
  15. /*
  16. * To-C-String char * strings
  17. * FIXME: Should these be static?
  18. */
  19. /* Type header */
  20. #define TH "type:"
  21. char Type_Type_C_str[] = TH "type";
  22. char Nil_Type_C_str[] = TH "nil";
  23. char Empty_Type_C_str[] = TH "empty";
  24. char Boolean_Type_C_str[] = TH "boolean";
  25. char Number_Type_C_str[] = TH "number";
  26. char String_Type_C_str[] = TH "string";
  27. char Symbol_Type_C_str[] = TH "symbol";
  28. char Pair_Type_C_str[] = TH "pair";
  29. char Function_Type_C_str[] = TH "function";
  30. #undef TH /* !type header */
  31. char Invalid_Type_C_str[] = "INVALID-TYPE";
  32. char Nil_C_str[] = "nil";
  33. char Empty_C_str[] = "()";
  34. char True_C_str[] = "#t";
  35. char False_C_str[] = "#f";
  36. /* Type */
  37. SELF_EVAL_FUNC(Type)
  38. char *Type_to_cstr(Object *o)
  39. {
  40. Type *t = (Type *) o;
  41. char *s;
  42. if (t == TTYPE)
  43. s = Type_Type_C_str;
  44. else if (t == TNIL)
  45. s = Nil_Type_C_str;
  46. else if (t == TEMPTY)
  47. s = Empty_Type_C_str;
  48. else if (t == TBOOLEAN)
  49. s = Boolean_Type_C_str;
  50. else if (t == TNUMBER)
  51. s = Number_Type_C_str;
  52. else if (t == TSTRING)
  53. s = String_Type_C_str;
  54. else if (t == TSYMBOL)
  55. s = Symbol_Type_C_str;
  56. else if (t == TPAIR)
  57. s = Pair_Type_C_str;
  58. else if (t == TFUNCTION)
  59. s = Function_Type_C_str;
  60. else
  61. {
  62. int VALID_TYPE = 0;
  63. assert(VALID_TYPE); /* should fail in debug mode */
  64. s = Invalid_Type_C_str;
  65. }
  66. return s;
  67. }
  68. /* Nil */
  69. SELF_EVAL_FUNC(Nil)
  70. char *Nil_to_cstr(Object *o)
  71. {
  72. return Nil_C_str;
  73. }
  74. /* Empty list */
  75. SELF_EVAL_FUNC(Empty)
  76. char *Empty_to_cstr(Object *o)
  77. {
  78. return Empty_C_str;
  79. }
  80. /* Booleans */
  81. SELF_EVAL_FUNC(Boolean)
  82. char *Boolean_to_cstr(Object *o)
  83. {
  84. if (getboolean(o))
  85. return True_C_str;
  86. else
  87. return False_C_str;
  88. }
  89. /* Equals functions */
  90. /*
  91. Object *Equal_p(Object *o1, Object *o2)
  92. {
  93. /=*
  94. EqualFunc equal_p = gettype(o1)->equal;
  95. if (equal_p == NULL) return Eq_p(o1, o2);
  96. return equal_p(o1, o2);
  97. *=/
  98. return gettype(o1)->equal(o1, o2);
  99. }
  100. */
  101. Object *Type_equal(Object *o1, Object *o2)
  102. {
  103. return Eq_p(o1, o2);
  104. }
  105. Object *Nil_equal(Object *o1, Object *o2)
  106. {
  107. return Eq_p(o1, o2);
  108. }
  109. Object *Empty_equal(Object *o1, Object *o2)
  110. {
  111. return Eq_p(o1, o2);
  112. }
  113. Object *Boolean_equal(Object *o1, Object *o2)
  114. {
  115. return Eq_p(o1, o2);
  116. }
  117. /*
  118. * Fold/reduce left algorithms
  119. * Rather important ones for a functional-ish programming language
  120. * based around lists :)
  121. * The "c" means it takes and uses a C function pointer
  122. */
  123. Object *cfold_left(BinaryFunc func, Object *first, Object *list)
  124. {
  125. Object *acc; /* accumulator */
  126. /* Only act if it is a proper list */
  127. if (!list_p(list))
  128. return ONIL;
  129. acc = first;
  130. while (pair_p(list))
  131. {
  132. acc = func(acc, Head(list));
  133. list = Tail(list);
  134. }
  135. return acc;
  136. }
  137. /*
  138. * Take a binary C function and a list and reduce the list to a single value
  139. *
  140. * If there is only 1 item in the list, return it
  141. */
  142. Object *cfold_left_no_init(BinaryFunc func, Object *list)
  143. {
  144. if (!pair_p(list))
  145. return ONIL;
  146. return cfold_left(func, Head(list), Tail(list));
  147. }
  148. /*
  149. * Is "o" a proper list?
  150. *
  151. * Def:
  152. * If o is () -> True
  153. * Else if o is Pair -> repeat on tail
  154. * Else -> False
  155. */
  156. /*
  157. int list_p(Object *o)
  158. {
  159. Object *cur = o;
  160. while (pair_p(cur))
  161. {
  162. cur = Tail(cur);
  163. }
  164. if (empty_p(cur))
  165. return CTRUE;
  166. else
  167. return CFALSE;
  168. }
  169. */
  170. /*
  171. * Starting with 0, increment for every new pair.
  172. * If first non-pair is "()" (for a proper list), return the accumulator.
  173. * Else, return (-1) for error.
  174. *
  175. * Follows closely the format of "list_p".
  176. */
  177. int list_length(Object *o)
  178. {
  179. Object *cur = o;
  180. int acc = 0;
  181. while (pair_p(cur))
  182. {
  183. acc++;
  184. cur = Tail(cur);
  185. }
  186. if (empty_p(cur))
  187. return acc;
  188. else
  189. return (-1);
  190. }