Little fix after the last commit (mostly a git fail)
[eigenmath-fx.git] / run.cpp
blob508165af919bc7ab3c017e14a21f4b4fc8fd5fa6
1 #include "stdafx.h"
2 #include "defs.h"
4 jmp_buf stop_return, draw_stop_return;
6 void
7 stop(char *s)
9 if (draw_flag == 2)
10 longjmp(draw_stop_return, 1);
11 else {
12 printstr("Stop: ");
13 printstr(s);
14 printstr(" ");
15 longjmp(stop_return, 1);
19 void
20 run(char *s)
22 int i, n;
24 /*if (strncmp(s, "selftest", 8) == 0) {
25 selftest();
26 return;
27 }*/
29 if (setjmp(stop_return))
30 return;
32 init();
34 while (1) {
36 n = scan(s);
38 p1 = pop();
39 check_stack();
41 if (n == 0)
42 break;
44 // if debug mode then print the source text
46 if (equaln(get_binding(symbol(TRACE)), 1)) {
47 for (i = 0; i < n; i++)
48 if (s[i] != ' ')
49 printchar(s[i]);
50 if (s[n - 1] != ' ') // n is not zero, see above
51 printchar(' ');
54 s += n;
56 push(p1);
57 top_level_eval();
59 p2 = pop();
60 check_stack();
62 if (p2 == symbol(NIL))
63 continue;
65 // print string w/o quotes
67 if (isstr(p2)) {
68 printstr(p2->u.str);
69 printstr(" ");
70 continue;
73 if (equaln(get_binding(symbol(TTY)), 1) || test_flag) // tty mode?
74 printline(p2);
75 else {
76 //#ifdef LINUX
77 display(p2);
78 /*#else
79 push(p2);
80 cmdisplay();
81 #endif*/
86 void
87 check_stack(void)
89 if (tos != 0)
90 stop("stack error");
91 if (frame != stack + TOS)
92 stop("frame error");
95 // cannot reference symbols yet
97 void
98 echo_input(char *s)
100 printstr(s);
101 printstr(" ");
104 // returns nil on stack if no result to print
106 void
107 top_level_eval(void)
109 save();
111 trigmode = 0;
113 p1 = symbol(AUTOEXPAND);
115 if (iszero(get_binding(p1)))
116 expanding = 0;
117 else
118 expanding = 1;
120 p1 = pop();
121 push(p1);
122 eval();
123 p2 = pop();
125 // "draw", "for" and "setq" return "nil", there is no result to print
127 if (p2 == symbol(NIL)) {
128 push(p2);
129 restore();
130 return;
133 // update "last"
135 set_binding(symbol(LAST), p2);
137 if (!iszero(get_binding(symbol(BAKE)))) {
138 push(p2);
139 bake();
140 p2 = pop();
143 // If we evaluated the symbol "i" or "j" and the result was sqrt(-1)
145 // then don't do anything.
147 // Otherwise if "j" is an imaginary unit then subst.
149 // Otherwise if "i" is an imaginary unit then subst.
151 if ((p1 == symbol(SYMBOL_I) || p1 == symbol(SYMBOL_J))
152 && isimaginaryunit(p2))
154 else if (isimaginaryunit(get_binding(symbol(SYMBOL_J)))) {
155 push(p2);
156 push(imaginaryunit);
157 push_symbol(SYMBOL_J);
158 subst();
159 p2 = pop();
160 } else if (isimaginaryunit(get_binding(symbol(SYMBOL_I)))) {
161 push(p2);
162 push(imaginaryunit);
163 push_symbol(SYMBOL_I);
164 subst();
165 p2 = pop();
168 #ifndef LINUX
170 // if we evaluated the symbol "a" and got "b" then print "a=b"
172 // do not print "a=a"
174 if (issymbol(p1) && !iskeyword(p1) && p1 != p2 && test_flag == 0) {
175 push_symbol(SETQ);
176 push(p1);
177 push(p2);
178 list(3);
179 p2 = pop();
181 #endif
182 push(p2);
184 restore();
187 void
188 check_esc_flag(void)
190 if (esc_flag)
191 stop("esc key");