eigenmath/run.cpp

145 lines
1.9 KiB
C++
Raw Permalink Normal View History

2004-03-03 21:24:06 +01:00
#include "stdafx.h"
#include "defs.h"
2004-08-28 00:09:03 +02:00
extern int symbol_level;
extern int test_flag;
2004-03-03 21:24:06 +01:00
extern U *varlist;
jmp_buf stop_return;
static char *errstr;
void
2004-08-28 00:09:03 +02:00
stop(char *str)
2004-03-03 21:24:06 +01:00
{
2004-08-28 00:09:03 +02:00
errstr = str; // don't print str now, jmp_buf might be redirected
2004-03-03 21:24:06 +01:00
longjmp(stop_return, 1);
}
void
run(char *s)
{
2004-08-31 02:56:15 +02:00
int n;
2004-03-03 21:24:06 +01:00
2004-08-28 00:38:58 +02:00
esc_flag = 0;
2004-03-03 21:24:06 +01:00
if (setjmp(stop_return)) {
restore_symbols(0);
if (errstr) {
2004-09-01 16:55:03 +02:00
printstr("Stop: ");
2004-03-03 21:24:06 +01:00
printstr(errstr);
printstr("\n");
}
return;
}
2004-08-28 00:09:03 +02:00
init();
2004-03-03 21:24:06 +01:00
tos = 0;
frame = stack + TOS;
2004-08-28 00:09:03 +02:00
symbol_level = 0;
2006-01-16 20:37:31 +01:00
varlist = symbol(NIL);
2004-08-28 00:09:03 +02:00
if (dash_dash_command(s))
return;
2004-03-03 21:24:06 +01:00
while (1) {
n = scan(s);
if (n == 0)
break;
s += n;
setup();
p1 = pop();
2004-08-28 00:09:03 +02:00
check_stack();
2004-03-03 21:24:06 +01:00
push(p1);
eval();
2004-08-28 00:09:03 +02:00
2004-03-03 21:24:06 +01:00
p2 = pop();
2004-08-28 00:09:03 +02:00
check_stack();
2004-03-03 21:24:06 +01:00
2005-08-05 21:28:02 +02:00
symbol(LAST)->u.sym.binding = symbol(YYLAST)->u.sym.binding;
2006-01-16 20:37:31 +01:00
symbol(LAST)->u.sym.binding2 = symbol(NIL);
2004-03-03 21:24:06 +01:00
// print string w/o quotes
2005-08-06 22:57:37 +02:00
if (isstr(p2)) {
2004-03-03 21:24:06 +01:00
printstr(p2->u.str);
printstr("\n");
continue;
}
// don't print nil unless it was due to eval of a symbol
2006-01-16 20:37:31 +01:00
if (p2 == symbol(NIL) && (iskeyword(p1) || !issymbol(p1)))
2004-03-03 21:24:06 +01:00
continue;
2006-02-10 18:01:28 +01:00
if (!iszero(symbol(BAKE)->u.sym.binding)) {
push(p2);
bake();
p2 = pop();
}
2006-02-10 17:21:52 +01:00
2004-08-31 02:56:15 +02:00
if (equal(symbol(TTY)->u.sym.binding, one) || test_flag) // tty mode?
2004-03-03 21:24:06 +01:00
printline(p2);
else {
2004-06-12 01:27:33 +02:00
#ifdef LINUX
display(p2);
#else
2005-08-07 16:42:42 +02:00
if (issymbol(p1) && !iskeyword(p1) && p1 != p2) {
// print as a = b
2004-03-03 21:24:06 +01:00
push_symbol(SETQ);
push(p1);
push(p2);
list(3);
p2 = pop();
}
cmdisplay(p2);
2004-06-12 01:27:33 +02:00
#endif
2004-03-03 21:24:06 +01:00
}
}
}
2005-12-16 02:09:51 +01:00
int
2004-08-28 00:09:03 +02:00
dash_dash_command(char *s)
{
if (strncmp(s, "--mem", 5) == 0) {
print_mem_info();
return 1;
}
if (strncmp(s, "--gc", 4) == 0) {
gc();
2005-06-25 21:29:07 +02:00
printstr("OK\n");
2004-08-28 00:09:03 +02:00
return 1;
}
if (strncmp(s, "--test", 6) == 0) {
selftest();
return 1;
}
return 0;
}
2005-12-16 02:09:51 +01:00
void
2004-08-28 00:09:03 +02:00
check_stack(void)
2004-03-03 21:24:06 +01:00
{
if (tos != 0)
2004-08-28 00:09:03 +02:00
stop("stack error");
2004-03-03 21:24:06 +01:00
if (frame != stack + TOS)
2004-08-28 00:09:03 +02:00
stop("frame error");
2004-03-03 21:24:06 +01:00
}
2006-07-13 02:09:33 +02:00
// cannot reference symbols yet
void
echo_input(char *s)
{
printstr(s);
printstr("\n");
}