@@ -171,6 +171,7 @@ int lispreadchar(char *chp);
171
171
172
172
PRIM breakpoint ();
173
173
void error (char * msg );
174
+ static inline int tracep (lisp f );
174
175
175
176
void run (char * s , lisp * envp );
176
177
@@ -1028,13 +1029,19 @@ PRIM evallist(lisp e, lisp* envp) {
1028
1029
return r ;
1029
1030
}
1030
1031
1031
- static inline int tracep (lisp f );
1032
+ #define MAX_STACK 80
1033
+
1034
+ static struct stack {
1035
+ lisp e ;
1036
+ lisp * envp ;
1037
+ } stack [MAX_STACK ];
1038
+
1032
1039
1033
1040
// dummy function that doesn't eval, used instead of eval
1034
1041
static PRIM noEval (lisp x , lisp * envp ) { return x ; }
1035
1042
1036
1043
PRIM primapply (lisp ff , lisp args , lisp * envp , lisp all , int noeval ) {
1037
- //printf("PRIMAPPLY "); princ(ff); princ(args); terpri();
1044
+ //printf("PRIMAPPLY "); princ(ff); putchar(' '); princ(args); putchar(' '); princ(*envp ); terpri();
1038
1045
int n = GETPRIMNUM (ff );
1039
1046
lisp (* e )(lisp x , lisp * envp ) = (noeval && n > 0 ) ? noEval : evalGC ;
1040
1047
int an = abs (n );
@@ -2123,7 +2130,12 @@ static inline lisp eval_hlp(lisp e, lisp* envp) {
2123
2130
}
2124
2131
2125
2132
// This may return a immediate, this allows tail recursion evalGC will reduce it.
2133
+
2134
+ stack [level ].e = f ;
2135
+ stack [level ].envp = envp ;
2126
2136
lisp r = callfunc (f , cdr (e ), envp , e , 0 );
2137
+ stack [level ].e = nil ;
2138
+ stack [level ].envp = nil ;
2127
2139
2128
2140
// we replace it after as no error was generated...
2129
2141
if (f != orig ) {
@@ -2208,25 +2220,20 @@ int needGC();
2208
2220
// #772 0x08052d58 in readeval ()
2209
2221
// #773 0x08048b57 in main ()
2210
2222
2211
- #define MAX_STACK 80
2212
-
2213
- static struct stack {
2214
- lisp e ;
2215
- lisp * envp ;
2216
- } stack [MAX_STACK ];
2217
-
2218
2223
// TODO: because of tail call optimization, we can't tell where the error occurred as it's not relevant on the stack???
2219
- PRIM print_detailed_stack () {
2224
+ PRIM print_detailed_stack (int curr ) {
2220
2225
int l ;
2221
2226
// TODO: DONE but too much: using fargs of f can use .envp to print actual arguments!
2222
2227
for (l = 0 ; l < level + 5 ; l ++ ) {
2223
2228
if (!stack [l ].e && !stack [l ].envp ) break ;
2224
2229
2225
2230
if (!l ) terpri ();
2231
+ if (curr && l == curr - 1 ) printf ("==>" );
2226
2232
printf ("%4d : " , l );
2227
- prin1 (stack [l ].e ); printf (" ==> " );
2233
+ prin1 (stack [l ].e ); printf (" ENV: " );
2228
2234
2229
2235
lisp f = car (stack [l ].e );
2236
+ if (!f ) f = stack [l ].e ;
2230
2237
lisp * envp = stack [l ].envp ;
2231
2238
lisp env = envp ? * envp : nil ; // env before
2232
2239
while (f && IS (f , symboll ) && !IS (f , func ) && !IS (f , thunk ) && !IS (f , prim ) && !IS (f , immediate )) {
@@ -2255,7 +2262,7 @@ PRIM print_detailed_stack() {
2255
2262
printf (" ... ] " );
2256
2263
} else {
2257
2264
printf (" ... ] " );
2258
- printf (" ...car did not evaluate to a function... (it's an %s)\n" , f ? tag_name [TAG (f )] : "nil" );
2265
+ printf (" ...car did not evaluate to a function... (it's %s)\n" , f ? tag_name [TAG (f )] : "nil" );
2259
2266
break ;
2260
2267
}
2261
2268
terpri ();
@@ -3513,31 +3520,43 @@ void error(char* msg) {
3513
3520
if (!msg ) { // breakpoint
3514
3521
int elsave = error_level ; error_level = 0 ;
3515
3522
print_stack (); terpri ();
3516
- int l = level > 0 ? level - 1 : 0 ;
3523
+ int l = level > 0 ? level : 0 ;
3517
3524
void print_env (int d ) {
3518
3525
l += d ;
3519
3526
if (l < 0 ) l = 0 ;
3520
3527
if (!stack [l ].envp ) l -= d ;
3521
- if (stack [l ].envp ) print (_env (stack [l ].envp , nil )); terpri ();
3528
+ printf (" STACK: " ); print_stack (); terpri ();
3529
+ printf ("CURRENT: " ); prin1 (stack [l ].e ); terpri ();
3530
+ if (stack [l ].envp ) {
3531
+ printf (" ENV: " );
3532
+ prin1 (_env (stack [l ].envp , nil )); terpri ();
3533
+ }
3522
3534
}
3523
3535
print (stack [l ].e ); terpri ();
3524
3536
char * ln = NULL ;
3525
3537
while (1 ) {
3526
3538
if (ln ) free (ln );
3527
- printf ("debug %d] " , l - 1 ); fflush (stdout );
3539
+ printf ("debug %d] " , l - 1 ); fflush (stdout );
3528
3540
char * ln = readline_int ("" , READLINE_MAXLEN , lispreadchar );
3541
+ printf ("---------\n" );
3529
3542
if (!ln ) break ;
3530
3543
if (!strcmp (ln , "q" )) break ;
3531
- if (!strcmp (ln , "e" )) { print_env (0 ); continue ; }
3544
+ if (!strcmp (ln , "h" ) || !strcmp (ln , "?" )) {
3545
+ printf ("Debug help: q(uit) h(elp) p(rint env) u(p) d(own) b(ack)t(race) EXPR\n" );
3546
+ continue ;
3547
+ }
3548
+ if (!strcmp (ln , "p" )) { print_env (0 ); continue ; }
3532
3549
if (!strcmp (ln , "u" )) { print_env (-1 ); continue ; }
3533
3550
if (!strcmp (ln , "d" )) { print_env (+1 ); continue ; }
3534
- if (!strcmp (ln , "bt" )) { print_detailed_stack (); continue ; }
3551
+ if (!strcmp (ln , "bt" )) { print_detailed_stack (l ); continue ; }
3535
3552
lisp r = reads (ln );
3536
3553
jmp_buf save ;
3537
3554
memcpy (save , lisp_break , sizeof (save ));
3538
3555
if (setjmp (lisp_break ) == 0 ) {
3539
- // TODO(jsk): this zeroes level/trace_level
3556
+ // continue deeper on the stack
3557
+ level ++ ;
3540
3558
prin1 (evalGC (r , stack [l ].envp )); terpri ();
3559
+ level -- ;
3541
3560
} else {
3542
3561
// TODO() if any error above (like aslfkjasdf) it'll mess up the stack?
3543
3562
printf ("\n%%back....from error/break\n" );
@@ -3548,7 +3567,7 @@ void error(char* msg) {
3548
3567
error_level = elsave ;
3549
3568
} else {
3550
3569
if (level ) { printf ("%%%s\nBacktrace: " , msg ); print_stack (); terpri (); }
3551
- print_detailed_stack ();
3570
+ print_detailed_stack (0 );
3552
3571
printf ("%s!\n" , msg );
3553
3572
}
3554
3573
error_level -- ;
@@ -3584,6 +3603,8 @@ void run(char* s, lisp* envp) {
3584
3603
blockGC = 0 ;
3585
3604
level = 0 ;
3586
3605
trace_level = 0 ;
3606
+ stack [0 ].e = nil ;
3607
+ stack [0 ].envp = NULL ;
3587
3608
printf ("\n%%%% type 'help' to get help\n" );
3588
3609
}
3589
3610
// disable longjmp
0 commit comments