misc.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #include "stdafx.h"
  2. #include "defs.h"
  3. #include <string.h>
  4. void
  5. new_string(char *s)
  6. {
  7. save();
  8. p1 = alloc();
  9. p1->k = STR;
  10. p1->u.str=(char *)malloc(strlen(s)+1);
  11. memcpy(p1->u.str,s,strlen(s)+1);
  12. push(p1);
  13. restore();
  14. }
  15. void
  16. out_of_memory(void)
  17. {
  18. stop("out of memory");
  19. }
  20. void
  21. push_zero_matrix(int i, int j)
  22. {
  23. push(alloc_tensor(i * j));
  24. stack[tos - 1]->u.tensor->ndim = 2;
  25. stack[tos - 1]->u.tensor->dim[0] = i;
  26. stack[tos - 1]->u.tensor->dim[1] = j;
  27. }
  28. void
  29. push_identity_matrix(int n)
  30. {
  31. int i;
  32. push_zero_matrix(n, n);
  33. for (i = 0; i < n; i++)
  34. stack[tos - 1]->u.tensor->elem[i * n + i] = one;
  35. }
  36. void
  37. push_cars(U *p)
  38. {
  39. while (iscons(p)) {
  40. push(car(p));
  41. p = cdr(p);
  42. }
  43. }
  44. void
  45. peek(void)
  46. {
  47. save();
  48. p1 = pop();
  49. push(p1);
  50. printline(p1);
  51. restore();
  52. }
  53. void
  54. peek2(void)
  55. {
  56. print_lisp(stack[tos - 2]);
  57. print_lisp(stack[tos - 1]);
  58. }
  59. int
  60. equal(U *p1, U *p2)
  61. {
  62. if (cmp_expr(p1, p2) == 0)
  63. return 1;
  64. else
  65. return 0;
  66. }
  67. int
  68. lessp(U *p1, U *p2)
  69. {
  70. if (cmp_expr(p1, p2) < 0)
  71. return 1;
  72. else
  73. return 0;
  74. }
  75. int
  76. sign(int n)
  77. {
  78. if (n < 0)
  79. return -1;
  80. else if (n > 0)
  81. return 1;
  82. else
  83. return 0;
  84. }
  85. int
  86. cmp_expr(U *p1, U *p2)
  87. {
  88. int n;
  89. if (p1 == p2)
  90. return 0;
  91. if (p1 == symbol(NIL))
  92. return -1;
  93. if (p2 == symbol(NIL))
  94. return 1;
  95. if (isnum(p1) && isnum(p2))
  96. return sign(compare_numbers(p1, p2));
  97. if (isnum(p1))
  98. return -1;
  99. if (isnum(p2))
  100. return 1;
  101. if (isstr(p1) && isstr(p2))
  102. return sign(strcmp(p1->u.str, p2->u.str));
  103. if (isstr(p1))
  104. return -1;
  105. if (isstr(p2))
  106. return 1;
  107. if (issymbol(p1) && issymbol(p2))
  108. return sign(strcmp(get_printname(p1), get_printname(p2)));
  109. if (issymbol(p1))
  110. return -1;
  111. if (issymbol(p2))
  112. return 1;
  113. if (istensor(p1) && istensor(p2))
  114. return compare_tensors(p1, p2);
  115. if (istensor(p1))
  116. return -1;
  117. if (istensor(p2))
  118. return 1;
  119. while (iscons(p1) && iscons(p2)) {
  120. n = cmp_expr(car(p1), car(p2));
  121. if (n != 0)
  122. return n;
  123. p1 = cdr(p1);
  124. p2 = cdr(p2);
  125. }
  126. if (iscons(p2))
  127. return -1;
  128. if (iscons(p1))
  129. return 1;
  130. return 0;
  131. }
  132. int
  133. length(U *p)
  134. {
  135. int n = 0;
  136. while (iscons(p)) {
  137. p = cdr(p);
  138. n++;
  139. }
  140. return n;
  141. }
  142. static void unique_f(U *);
  143. U *
  144. unique(U *p)
  145. {
  146. save();
  147. p1 = symbol(NIL);
  148. p2 = symbol(NIL);
  149. unique_f(p);
  150. if (p2 != symbol(NIL))
  151. p1 = symbol(NIL);
  152. p = p1;
  153. restore();
  154. return p;
  155. }
  156. static void
  157. unique_f(U *p)
  158. {
  159. if (isstr(p)) {
  160. if (p1 == symbol(NIL))
  161. p1 = p;
  162. else if (p != p1)
  163. p2 = p;
  164. return;
  165. }
  166. while (iscons(p)) {
  167. unique_f(car(p));
  168. if (p2 != symbol(NIL))
  169. return;
  170. p = cdr(p);
  171. }
  172. }
  173. #if 0
  174. void
  175. check_endianess(void)
  176. {
  177. int tmp = 1;
  178. if (((char *) &tmp)[0] == 1 && Y_LITTLE_ENDIAN == 0) {
  179. printf("Please change Y_LITTLE_ENDIAN to 1 in defs.h and recompile.");
  180. Exit(1);
  181. }
  182. if (((char *) &tmp)[0] == 0 && Y_LITTLE_ENDIAN != 0) {
  183. printf("Please change Y_LITTLE_ENDIAN to 0 in defs.h and recompile.");
  184. Exit(1);
  185. }
  186. }
  187. #endif
  188. void
  189. ssqrt(void)
  190. {
  191. push_rational(1, 2);
  192. power();
  193. }
  194. void
  195. yyexpand(void)
  196. {
  197. int x;
  198. x = expanding;
  199. expanding = 1;
  200. eval();
  201. expanding = x;
  202. }
  203. void
  204. exponential(void)
  205. {
  206. push_symbol(E);
  207. swap();
  208. power();
  209. }
  210. void
  211. square(void)
  212. {
  213. push_integer(2);
  214. power();
  215. }
  216. static int
  217. __cmp(const void *p1, const void *p2)
  218. {
  219. return cmp_expr(*((U **) p1), *((U **) p2));
  220. }
  221. void
  222. sort_stack(int n)
  223. {
  224. qsort(stack + tos - n, n, sizeof (U *), __cmp);
  225. }