コード例 #1
0
ファイル: str0_getline.c プロジェクト: TimJT/qhasm
int str0_getline(str0 *s,limitmalloc_pool *pool)
{
  char c;
  int pos;

  if (!str0_copyc(s,pool,"")) return -1;

  pos = 0;
  for (;;) {
    if (scanf("%c",&c) < 1) {
      if (ferror(stdin)) return -2;
      buf[pos] = 0;
      if (!str0_concatc(s,pool,buf)) return -1;
      if (!str0_length(s)) return 0;
      return 1;
    }
    if (c == '\n') {
      buf[pos] = 0;
      if (!str0_concatc(s,pool,buf)) return -1;
      return 2;
    }
    if (c == 0) c = '\n';
    buf[pos++] = c;
    if (pos == 128) {
      buf[pos] = 0; /* redundant */
      if (!str0_concatc(s,pool,buf)) return -1;
      pos = 0;
    }
  }
}
コード例 #2
0
ファイル: str0_increment.c プロジェクト: drudru/qhasm
int str0_increment(str0 *s,limitmalloc_pool *pool)
{
    char *x = *s;
    int64 len = str0_length(s);
    int64 i;

    for (i = len - 1; i >= 0; --i) {
        if (x[i] >= '0' && x[i] <= '8') {
            ++x[i];
            return 1;
        }
        x[i] = '0';
    }
    if (!str0_concatc(s,pool,"0")) return 0;
    s[0][0]++;
    return 1;
}
コード例 #3
0
ファイル: critbit0_delete.c プロジェクト: TimJT/qhasm
int critbit0_delete(critbit0_tree *t,limitmalloc_pool *pool,str0 *u)
{
  const uint8 *ubytes = (void *) *u;
  int64 ulen = str0_length(u);
  uint8 *p = t->root;
  void **wherep = &t->root;
  void **whereq = 0;
  critbit0_node *q = 0;
  uint32 byte;
  uint32 otherbits;
  uint8 c;
  int direction;

  if (!p) return 0; /* S = {} */

  while (1 & (int) p) {
    whereq = wherep;
    q = (void *) (p - 1);
    byte = q->byte;
    otherbits = q->otherbits;
    c = 0;
    if (byte < ulen) c = ubytes[byte];
    direction = (1 + (otherbits | c)) >> 8;
    wherep = q->child + direction;
    p = *wherep;
  }

  if (!str0_equalc(u,(const char *) p)) return 0;
  limitmalloc_close(pool,ulen + 1,p);

  if (!whereq) { /* #S = 1 */
    t->root = 0;
    return 1;
  }

  *whereq = q->child[1 - direction];
  limitmalloc_close(pool,sizeof(critbit0_node),q);
  return 1;
}
コード例 #4
0
ファイル: qhasm-regs.c プロジェクト: hlThai/gpg-TeslaRing
int finduses2(void)
{
  switch(finduses2_state) {
    case 0:
      if (!str0_copy(&n,0,&inputfield)) return -1;
      finduses2_state = 1; return 1;
    case 1:
      if (str0_equalc(&inputfield,"inputreg")) { finduses2_state = 10; return 1; }
      if (str0_equalc(&inputfield,"outputreg")) { finduses2_state = 20; return 1; }
      if (str0_equalc(&inputfield,"kill")) { finduses2_state = 30; return 1; }
      if (str0_equalc(&inputfield,"reglimit")) { finduses2_state = 40; return 1; }
      if (str0_equalc(&inputfield,"op")) { finduses2_state = 50; return 1; }
      if (str0_equalc(&inputfield,"maybegoto")) { finduses2_state = 50; return 1; }
      break;
    case 10:
      if (!critbit0_insert(&inputregs,0,&inputfield)) return -1;
      if (!str0_copy(&key,0,&inputfield)) return -1;
      if (!str0_concatc(&key,0,":")) return -1;
      if (!critbit0_firstprefixed(&vtypes,0,&type,&key)) {
	if (!str0_copyc(&outputline,0,"error:unknown variable ")) return -1;
	if (!str0_concat(&outputline,0,&key)) return -1;
	if (!str0_concat(&outputline,0,&inputline)) return -1;
	str0_putline(&outputline);
	return 0;
      }
      finduses2_state = 11; return 1;
    case 11:
      if (!str0_copy(&x,0,&key)) return -1;
      if (!str0_concat(&x,0,&inputfield)) return -1;
      if (!critbit0_insert(&inputregassignments,0,&x)) return -1;
      break;
    case 20:
      if (!critbit0_insert(&outputregs,0,&inputfield)) return -1;
      if (!str0_copy(&key,0,&inputfield)) return -1;
      if (!str0_concatc(&key,0,":")) return -1;
      if (!critbit0_firstprefixed(&vtypes,0,&type,&key)) {
	if (!str0_copyc(&outputline,0,"error:unknown variable ")) return -1;
	if (!str0_concat(&outputline,0,&key)) return -1;
	if (!str0_concat(&outputline,0,&inputline)) return -1;
	str0_putline(&outputline);
	return 0;
      }
      finduses2_state = 21; return 1;
    case 21:
      if (!str0_copy(&x,0,&key)) return -1;
      if (!str0_concat(&x,0,&inputfield)) return -1;
      if (!critbit0_insert(&outputregassignments,0,&x)) return -1;
      break;
    case 30:
      if (!str0_copy(&key,0,&inputfield)) return -1;
      if (!str0_concatc(&key,0,":")) return -1;
      if (!critbit0_firstprefixed(&vtypes,0,&type,&key)) {
	if (!str0_copyc(&outputline,0,"error:unknown variable ")) return -1;
	if (!str0_concat(&outputline,0,&key)) return -1;
	if (!str0_concat(&outputline,0,&inputline)) return -1;
	str0_putline(&outputline);
      }
      break;
    case 40:
      if (!str0_copy(&key,0,&inputfield)) return -1;
      if (!str0_concatc(&key,0,":")) return -1;
      if (!critbit0_firstprefixed(&vtypes,0,&type,&key)) {
	if (!str0_copyc(&outputline,0,"error:unknown variable ")) return -1;
	if (!str0_concat(&outputline,0,&key)) return -1;
	if (!str0_concat(&outputline,0,&inputline)) return -1;
	str0_putline(&outputline);
	return 0;
      }
      if (!str0_copy(&reglimit,0,&n)) return -1;
      if (!str0_concatc(&reglimit,0,"<")) return -1;
      if (!str0_concat(&reglimit,0,&inputfield)) return -1;
      if (!str0_copy(&key,0,&n)) return -1;
      if (!str0_concatc(&key,0,":<")) return -1;
      if (!str0_concat(&key,0,&inputfield)) return -1;
      if (!critbit0_insert(&uses,0,&key)) return -1;
      finduses2_state = 41; return 1;
    case 41:
      finduses_reglimit_state = 0;
      if (str0_word(&inputword,0,&inputfield,finduses_reglimit) < 0) return -1;
      return 1;
    case 50:
      finduses2_state = 51; return 1;
    case 51:
      if (str0_length(&inputfield) > 0) {
        if (!str0_copy(&key,0,&n)) return -1;
        if (!str0_concatc(&key,0,":")) return -1;
        if (!str0_concat(&key,0,&inputfield)) return -1;
        if (!critbit0_insert(&uses,0,&key)) return -1;
      }
      return 1;
  }
  return 0;
}
コード例 #5
0
ファイル: critbit0_insert.c プロジェクト: TimJT/qhasm
int critbit0_insert(critbit0_tree *t,limitmalloc_pool *pool,str0 *u)
{
  const uint8 *ubytes = (void *) *u;
  int64 ulen = str0_length(u);
  uint8 *p = t->root;
  critbit0_node *q;
  uint32 byte;
  uint32 otherbits;
  uint8 c;
  int direction;
  char *x;
  int64 i;
  uint32 newbyte;
  uint32 newotherbits;
  int newdirection;
  critbit0_node *newnode;
  void **wherep;

  if (ulen > 1073741824) { errno = ENOMEM; return 0; }

  if (!p) { /* S = {} */
    x = limitmalloc_open_if2(pool,ulen + 1);
    if (!x) return 0;
    for (i = 0;i < ulen;++i) x[i] = ubytes[i];
    x[ulen] = 0;
    t->root = x;
    return 2;
  }

  while (1 & (int) p) {
    q = (void *) (p - 1);
    byte = q->byte;
    otherbits = q->otherbits;
    c = 0;
    if (byte < ulen) c = ubytes[byte];
    direction = (1 + (otherbits | c)) >> 8;
    p = q->child[direction];
  }

  for (newbyte = 0;newbyte < ulen;++newbyte)
    if (p[newbyte] != ubytes[newbyte]) {
      newotherbits = p[newbyte] ^ ubytes[newbyte];
      goto differentbyte;
    }
  if (p[newbyte] != 0) {
    newotherbits = p[newbyte];
    goto differentbyte;
  }
  return 1;

  differentbyte:
  newotherbits &= 255;
  while (newotherbits & (newotherbits - 1)) newotherbits &= newotherbits - 1;
  newotherbits ^= 255;
  c = p[newbyte];
  newdirection = (1 + (newotherbits | c)) >> 8;

  newnode = limitmalloc_open_if2(pool,sizeof(critbit0_node));
  if (!newnode) return 0;

  x = limitmalloc_open_if2(pool,ulen + 1);
  if (!x) {
    limitmalloc_close(pool,sizeof(critbit0_node),newnode);
    return 0;
  }
  for (i = 0;i < ulen;++i) x[i] = ubytes[i];
  x[ulen] = 0;

  newnode->byte = newbyte;
  newnode->otherbits = newotherbits;
  newnode->child[1 - newdirection] = x;

  wherep = &t->root;
  for (;;) {
    p = *wherep;
    if (!(1 & (int) p)) break;
    q = (void *) (p - 1);
    byte = q->byte;
    otherbits = q->otherbits;
    if (byte > newbyte) break;
    if (byte == newbyte && otherbits > newotherbits) break;
    c = 0;
    if (byte < ulen) c = ubytes[byte];
    direction = (1 + (otherbits | c)) >> 8;
    wherep = q->child + direction;
  }

  newnode->child[newdirection] = *wherep;
  *wherep = (void *) (1 + (char *) newnode);
  return 2;
}