run.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #include "stdafx.h"
  2. #include "defs.h"
  3. jmp_buf stop_return, draw_stop_return;
  4. void
  5. stop(char *s)
  6. {
  7. if (draw_flag == 2)
  8. longjmp(draw_stop_return, 1);
  9. else {
  10. printstr("Stop: ");
  11. printstr(s);
  12. printstr(" ");
  13. longjmp(stop_return, 1);
  14. }
  15. }
  16. void
  17. run(char *s)
  18. {
  19. int i, n;
  20. /*if (strncmp(s, "selftest", 8) == 0) {
  21. selftest();
  22. return;
  23. }*/
  24. if (setjmp(stop_return))
  25. return;
  26. init();
  27. while (1) {
  28. n = scan(s);
  29. p1 = pop();
  30. check_stack();
  31. if (n == 0)
  32. break;
  33. // if debug mode then print the source text
  34. if (equaln(get_binding(symbol(TRACE)), 1)) {
  35. for (i = 0; i < n; i++)
  36. if (s[i] != ' ')
  37. printchar(s[i]);
  38. if (s[n - 1] != ' ') // n is not zero, see above
  39. printchar(' ');
  40. }
  41. s += n;
  42. push(p1);
  43. top_level_eval();
  44. p2 = pop();
  45. check_stack();
  46. if (p2 == symbol(NIL))
  47. continue;
  48. // print string w/o quotes
  49. if (isstr(p2)) {
  50. printstr(p2->u.str);
  51. printstr(" ");
  52. continue;
  53. }
  54. if (equaln(get_binding(symbol(TTY)), 1) || test_flag) // tty mode?
  55. printline(p2);
  56. else {
  57. //#ifdef LINUX
  58. display(p2);
  59. /*#else
  60. push(p2);
  61. cmdisplay();
  62. #endif*/
  63. }
  64. }
  65. }
  66. void
  67. check_stack(void)
  68. {
  69. if (tos != 0)
  70. stop("stack error");
  71. if (frame != stack + TOS)
  72. stop("frame error");
  73. }
  74. // cannot reference symbols yet
  75. void
  76. echo_input(char *s)
  77. {
  78. printstr(s);
  79. printstr(" ");
  80. }
  81. // returns nil on stack if no result to print
  82. void
  83. top_level_eval(void)
  84. {
  85. save();
  86. trigmode = 0;
  87. p1 = symbol(AUTOEXPAND);
  88. if (iszero(get_binding(p1)))
  89. expanding = 0;
  90. else
  91. expanding = 1;
  92. p1 = pop();
  93. push(p1);
  94. eval();
  95. p2 = pop();
  96. // "draw", "for" and "setq" return "nil", there is no result to print
  97. if (p2 == symbol(NIL)) {
  98. push(p2);
  99. restore();
  100. return;
  101. }
  102. // update "last"
  103. set_binding(symbol(LAST), p2);
  104. if (!iszero(get_binding(symbol(BAKE)))) {
  105. push(p2);
  106. bake();
  107. p2 = pop();
  108. }
  109. // If we evaluated the symbol "i" or "j" and the result was sqrt(-1)
  110. // then don't do anything.
  111. // Otherwise if "j" is an imaginary unit then subst.
  112. // Otherwise if "i" is an imaginary unit then subst.
  113. if ((p1 == symbol(SYMBOL_I) || p1 == symbol(SYMBOL_J))
  114. && isimaginaryunit(p2))
  115. ;
  116. else if (isimaginaryunit(get_binding(symbol(SYMBOL_J)))) {
  117. push(p2);
  118. push(imaginaryunit);
  119. push_symbol(SYMBOL_J);
  120. subst();
  121. p2 = pop();
  122. } else if (isimaginaryunit(get_binding(symbol(SYMBOL_I)))) {
  123. push(p2);
  124. push(imaginaryunit);
  125. push_symbol(SYMBOL_I);
  126. subst();
  127. p2 = pop();
  128. }
  129. #ifndef LINUX
  130. // if we evaluated the symbol "a" and got "b" then print "a=b"
  131. // do not print "a=a"
  132. if (issymbol(p1) && !iskeyword(p1) && p1 != p2 && test_flag == 0) {
  133. push_symbol(SETQ);
  134. push(p1);
  135. push(p2);
  136. list(3);
  137. p2 = pop();
  138. }
  139. #endif
  140. push(p2);
  141. restore();
  142. }
  143. void
  144. check_esc_flag(void)
  145. {
  146. if (esc_flag)
  147. stop("esc key");
  148. }