misc.cpp.000 3.3 KB

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