Branch: Tag:

2000-09-12

2000-09-12 14:02:42 by Henrik Grubbström (Grubba) <grubba@grubba.org>

check_tree() no longer recurses.

Rev: src/las.c:1.206

5:   \*/   /**/   #include "global.h" - RCSID("$Id: las.c,v 1.205 2000/09/11 22:10:38 grubba Exp $"); + RCSID("$Id: las.c,v 1.206 2000/09/12 14:02:42 grubba Exp $");      #include "language.h"   #include "interpret.h"
94:   }      #ifdef PIKE_DEBUG - /* FIXME: Ought to use parent pointer to avoid recursion. */ +    void check_tree(node *n, int depth)   { -  +  node *orig_n = n; +  node *parent; +     if(!d_flag) return; -  if(!n) return; +  +  if (!n) return; +  +  parent = n->parent; +  n->parent = NULL; +  +  while(n) {    if(n->token==USHRT_MAX)    fatal("Free node in tree.\n");   
138: Inside #if defined(PIKE_DEBUG)
   }    }    -  if(d_flag<2) return; +  if(d_flag<2) break;       if (!(depth & 63)) {    /* 512 bytes/stack frame should be enough... */
152: Inside #if defined(PIKE_DEBUG)
   fatal("Cyclic node structure found.\n");    }    } -  depth++; +        if(car_is_node(n))    { - #ifndef SHARED_NODES +  /* Check CAR */ + #ifdef SHARED_NODES +  CAR(n)->parent = n; + #else /* !SHARED_NODES */    if(CAR(n)->parent != n)    fatal("Parent is wrong.\n"); - #endif /* !SHARED_NODES */ + #endif /* SHARED_NODES */    -  check_tree(CAR(n),depth); +  depth++; +  n = CAR(n); +  continue;    }       if(cdr_is_node(n))    { - #ifndef SHARED_NODES +  /* Check CDR */ + #ifdef SHARED_NODES +  CDR(n)->parent = n; + #else /* !SHARED_NODES */    if(CDR(n)->parent != n)    fatal("Parent is wrong.\n");   #endif /* !SHARED_NODES */    -  check_tree(CDR(n),depth); +  depth++; +  n = CDR(n); +  continue;    } -  +  +  while(n->parent && +  (!cdr_is_node(n->parent) || (CDR(n->parent) == n))) { +  /* Backtrack */ +  n = n->parent; +  depth--;    } -  +  +  if (n->parent && cdr_is_node(n->parent)) { +  /* Jump to the sibling */ + #ifdef SHARED_NODES +  CDR(n->parent)->parent = n->parent; + #else /* !SHARED_NODES */ +  if(CDR(n->parent)->parent != n->parent) +  fatal("Parent is wrong.\n"); + #endif /* !SHARED_NODES */ +  n = CDR(n->parent); +  continue; +  } +  break; +  } +  +  if (n != orig_n) { +  fprintf(stderr, "check_tree() lost track.\n"); +  d_flag = 0; +  fprintf(stderr, "n:"); +  print_tree(n); +  fprintf(stderr, "orig_n:"); +  print_tree(orig_n); +  fatal("check_tree() lost track.\n"); +  } +  n->parent = parent; + }   #endif      /* FIXME: Ought to use parent pointer to avoid recursion. */
220:    int tmp1,tmp2;    tmp1=count_args(CADR(n));    tmp2=count_args(CDDR(n)); -  if(tmp1==-1 || tmp2==-2) return -1; +  if(tmp1==-1 || tmp2==-1) return -1;    if(tmp1 < tmp2) return tmp1;    return tmp2;    }