bake.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // pretty print
  2. #include "stdafx.h"
  3. #include "defs.h"
  4. void
  5. bake(void)
  6. {
  7. int h, s, t, x, y, z;
  8. expanding++;
  9. save();
  10. p1 = pop();
  11. s = ispoly(p1, symbol(SYMBOL_S));
  12. t = ispoly(p1, symbol(SYMBOL_T));
  13. x = ispoly(p1, symbol(SYMBOL_X));
  14. y = ispoly(p1, symbol(SYMBOL_Y));
  15. z = ispoly(p1, symbol(SYMBOL_Z));
  16. if (s == 1 && t == 0 && x == 0 && y == 0 && z == 0) {
  17. p2 = symbol(SYMBOL_S);
  18. bake_poly();
  19. } else if (s == 0 && t == 1 && x == 0 && y == 0 && z == 0) {
  20. p2 = symbol(SYMBOL_T);
  21. bake_poly();
  22. } else if (s == 0 && t == 0 && x == 1 && y == 0 && z == 0) {
  23. p2 = symbol(SYMBOL_X);
  24. bake_poly();
  25. } else if (s == 0 && t == 0 && x == 0 && y == 1 && z == 0) {
  26. p2 = symbol(SYMBOL_Y);
  27. bake_poly();
  28. } else if (s == 0 && t == 0 && x == 0 && y == 0 && z == 1) {
  29. p2 = symbol(SYMBOL_Z);
  30. bake_poly();
  31. } else if (iscons(p1)) {
  32. h = tos;
  33. push(car(p1));
  34. p1 = cdr(p1);
  35. while (iscons(p1)) {
  36. push(car(p1));
  37. bake();
  38. p1 = cdr(p1);
  39. }
  40. list(tos - h);
  41. } else
  42. push(p1);
  43. restore();
  44. expanding--;
  45. }
  46. void
  47. polyform(void)
  48. {
  49. int h;
  50. save();
  51. p2 = pop();
  52. p1 = pop();
  53. if (ispoly(p1, p2))
  54. bake_poly();
  55. else if (iscons(p1)) {
  56. h = tos;
  57. push(car(p1));
  58. p1 = cdr(p1);
  59. while (iscons(p1)) {
  60. push(car(p1));
  61. push(p2);
  62. polyform();
  63. p1 = cdr(p1);
  64. }
  65. list(tos - h);
  66. } else
  67. push(p1);
  68. restore();
  69. }
  70. void
  71. bake_poly()
  72. {
  73. int h, i, k, n;
  74. U **a;
  75. a = stack + tos;
  76. push(p1); // p(x)
  77. push(p2); // x
  78. k = coeff();
  79. h = tos;
  80. for (i = k - 1; i >= 0; i--) {
  81. p1 = a[i];
  82. bake_poly_term(i);
  83. }
  84. n = tos - h;
  85. if (n > 1) {
  86. list(n);
  87. push(symbol(ADD));
  88. swap();
  89. cons();
  90. }
  91. p1 = pop();
  92. tos -= k;
  93. push(p1);
  94. }
  95. // p1 points to coefficient of p2 ^ k
  96. void
  97. bake_poly_term(int k)
  98. {
  99. int h, n;
  100. if (iszero(p1))
  101. return;
  102. // constant term?
  103. if (k == 0) {
  104. if (car(p1) == symbol(ADD)) {
  105. p1 = cdr(p1);
  106. while (iscons(p1)) {
  107. push(car(p1));
  108. p1 = cdr(p1);
  109. }
  110. } else
  111. push(p1);
  112. return;
  113. }
  114. h = tos;
  115. // coefficient
  116. if (car(p1) == symbol(MULTIPLY)) {
  117. p1 = cdr(p1);
  118. while (iscons(p1)) {
  119. push(car(p1));
  120. p1 = cdr(p1);
  121. }
  122. } else if (!equaln(p1, 1))
  123. push(p1);
  124. // x ^ k
  125. if (k == 1)
  126. push(p2);
  127. else {
  128. push(symbol(POWER));
  129. push(p2);
  130. push_integer(k);
  131. list(3);
  132. }
  133. n = tos - h;
  134. if (n > 1) {
  135. list(n);
  136. push(symbol(MULTIPLY));
  137. swap();
  138. cons();
  139. }
  140. }