
// string pool routines. refer to the wikipedia article: "string interning".

int string_length(int x) {
  return string_pool[x];
}

void write_char_literal(int x) {
  if (x == '\n') {
    write_char('\\');
    write_char('n');
  }
  else
    write_char(x);
  return;
}

int stringeq(int x, int y) {
  int last;
  int length_x;
  int length_y;
  int done;
  int equality;
  length_x = string_pool[x];
  length_y = string_pool[y];
  if (length_x < length_y)
    return -1;
  else
  if (length_x > length_y)
    return 1;
  else {
    done = 0;
    // assume they are equal
    equality = 0;
    x = x + 1;
    y = y + 1;
    last = x + length_x;
    do {
      if (x == last)
        done = 1;
      else if (string_pool[x] < string_pool[y]) {
        done = 1;
        equality = -1;
      }
      else if (string_pool[x] > string_pool[y]) {
        done = 1;
        equality = 1;
      }
      else {
        x = x + 1;
        y = y + 1;
      }
    } while (!(done));
    return equality;
  }
}

void string_write(int x) {
  int i;
  for (i = x + 1; i <= x + string_length(x); i++)
    write_char(string_pool[i]);
  return;
}

void string_write_literal(int x) {
  int i;
  for (i = x + 1; i <= x + string_length(x); i++)
    write_char_literal(string_pool[i]);
  return;
}

int is_number(int token) {
  int digit;
  digit = string_pool[token + 1];
  return ((digit >= '0') && (digit <= '9')) || (digit == '$');
}

int is_hex(int token) {
  int digit;
  digit = string_pool[token + 1];
  return (digit == '$');
}
