rewrite.cpp 837 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Rewrite by expanding all symbols
  2. #include "stdafx.h"
  3. #include "defs.h"
  4. void
  5. rewrite(void)
  6. {
  7. int h;
  8. save();
  9. p1 = pop();
  10. if (istensor(p1)) {
  11. rewrite_tensor();
  12. restore();
  13. return;
  14. }
  15. if (iscons(p1)) {
  16. h = tos;
  17. push(car(p1)); // Do not rewrite function name
  18. p1 = cdr(p1);
  19. while (iscons(p1)) {
  20. push(car(p1));
  21. rewrite();
  22. p1 = cdr(p1);
  23. }
  24. list(tos - h);
  25. restore();
  26. return;
  27. }
  28. // If not a symbol then done
  29. if (!issymbol(p1)) {
  30. push(p1);
  31. restore();
  32. return;
  33. }
  34. // Get the symbol's binding, try again
  35. p2 = get_binding(p1);
  36. push(p2);
  37. if (p1 != p2)
  38. rewrite();
  39. restore();
  40. }
  41. void
  42. rewrite_tensor(void)
  43. {
  44. int i;
  45. push(p1);
  46. copy_tensor();
  47. p1 = pop();
  48. for (i = 0; i < p1->u.tensor->nelem; i++) {
  49. push(p1->u.tensor->elem[i]);
  50. rewrite();
  51. p1->u.tensor->elem[i] = pop();
  52. }
  53. push(p1);
  54. }