
// cons cell operations - lets get a little lispy

int is_null(int x) {
  return (x == 0);
}

int is_pair(int x) {
  return (x >= POOL_SIZE);
}

int is_zero(int x) {
  return (x == zero_tok);
}

int numberp(int x) {
  int n;
  if (!is_pair(x)) {
    n = output_queue[x];
    if (is_number(n))
      return 1;
    else
      return 0;
  }
  return 0;
}

int eqp(int x, int y) {
  x = output_queue[x];
  y = output_queue[y];
  return (x == y);
}

int my_cons(int x, int y) {
  int start;
  if (cons_cell_idx >= MAX_CONS_CELL) {
    log_error(MAX_NUMBER_OF_CONS_CELLS, 0);
    return 0;
  }
  else {
    start = cons_cell_idx;
    cons_cell[cons_cell_idx] = x;
    cons_cell_idx = cons_cell_idx + 1;
    cons_cell[cons_cell_idx] = y;
    cons_cell_idx = cons_cell_idx + 1;
    return start + POOL_SIZE;
  }
}

int my_car(int x) {
  if (!is_pair(x)) {
    printf("car not a pair");
    writeln();
    return 0;
  }
  else
    return cons_cell[x - POOL_SIZE];
}

int my_cdr(int x) {
  if (!is_pair(x)) {
    printf("cdr not a pair");
    writeln();
    return 0;
  }
  else
    return cons_cell[(x + 1) - POOL_SIZE];
}

int my_reverse_core(int x, int y) {
  if (is_null(x))
    return y;
  else
    return my_reverse_core(my_cdr(x), my_cons(my_car(x), y));
}

int my_reverse(int x) {
  return my_reverse_core(x, 0);
}

int list_length(int x) {
  int n;

  n = 0;
  while (!is_null(x)) {
    n = n + 1;
    x = my_cdr(x);
  }
  return n;
} 
