예제 #1
0
NODE*
parse_paren(SCANNER *s, int mode) {
  NODE *head, *node, *x;

  skip_white(s);
  if (s_eof(s)) return raise(s, "unexpected end of file");

  head = node = new_node();
  node->t = NODE_CELL;
  while (!s_eof(s) && s_peek(s) != ')') {
    NODE *child;
    char q = s_peek(s) == ',';
    if (q) s_getc(s);

    child = parse_any(s, PARSE_ANY);
    if (child == NULL) return NULL;

    if ((mode & PARSE_BQUOTE) != 0 && !q) {
      NODE *r = new_node();
      r->t = NODE_QUOTE;
      r->car = child;
      child = r;
    }

    if (child->t == NODE_IDENT && !strcmp(".", child->s)) {
      if (!head->car) {
        free_node(child);
        return raise(s, "illegal dot operation");
      }
      free_node(child);

      child = parse_any(s, PARSE_ANY);
      if (child == NULL) return NULL;
      node->cdr = child;
      break;
    } else {
      if (head->car) {
        x = new_node();
        x->t = NODE_CELL;
        node->cdr = x;
        node = x;
      }
      node->car = child;
    }

    skip_white(s);
  }

  if (!head->car && !head->cdr)
    head->t = NODE_NIL;

  return head;
}
예제 #2
0
NODE*
parse_any(SCANNER *s, int mode) {
  NODE *x = NULL;
  int c;

  skip_white(s);
  if (s_eof(s)) return raise(s, "unexpected end of file");

  c = s_peek(s);

  if (c == '(') {
    s_getc(s);
    x = parse_paren(s, mode);
    if (x == NULL) return NULL;
    if (s_eof(s)) {
      return raise(s, "unexpected end of file");
    }
    skip_white(s);
    if (s_getc(s) != ')') {
      return invalid_token(s);
    }
  } else if (c == '\'')
    x = parse_quote(s);
  else if (c == '`')
    return parse_bquote(s);
  else if (c == '"')
    x = parse_string(s);
  else if (isalnum((int)c) || strchr(SYMBOL_CHARS, c))
    x = parse_primitive(s);
  else
    return invalid_token(s);

  return x;
}
예제 #3
0
파일: main.c 프로젝트: johnsoga/Copy
void copy_recursive() {
    
    struct Stack* paths;
    DIR *dir;
    struct dirent *entry;
    const String d_name;
    int dir_count;
    
    dir_count = 0;
    paths = malloc(sizeof (struct Stack));
    s_create(paths);
    s_push(basename(src), paths);
    strcpy(src, dirname(src));
    strcpy(dest, dirname(dest));
    printf("Source: %s\nTarget: %s\n\n", src, dest);

    
    while(!s_isEmpty(paths)) {
        strcat(src, "/");
        strcat(src, s_peek(paths));
        s_pop(paths);
        if((dir = opendir(src))) {
            while((entry = readdir(dir))) {
                d_name = entry->d_name;
                if(entry->d_type & DT_DIR) {
                    printf("Found FOLDER: %s\n", d_name);
                    if (strcmp(d_name, "..") != 0 && strcmp(d_name, ".") != 0) {
                        s_push(d_name, paths);
                        dir_count++;
                    }
                } else if (entry->d_type & DT_REG) {
                    printf("Found FILE: %s\n", d_name);
                }
            }
            if (!closedir(dir)) {
                printf("Time to copy\n");
            } else {
                fprintf(stderr, "Could not close '%s': %s\n", src, strerror(errno));
                exit(EXIT_FAILURE);
            }
        } else {
            fprintf(stderr, "Cannot open %s: %s\n", src, strerror(errno));
            exit(EXIT_FAILURE);
        }
        if(dir_count) {
            strcpy(src, dirname(src));
            dir_count--;
        }
    }
}
예제 #4
0
static NODE*
parse_string(SCANNER *s) {
  char *buf = NULL;
  int n = 0, l = 0;
  int c = 0;
  NODE *node;

  buf = (char*)malloc(10);
  s_getc(s);
  while (!s_eof(s)) {
    c = s_getc(s);
    if (c == '\\' && !s_eof(s)) {
      c = s_peek(s);
      switch (c) {
      case '\\': c = '\\'; break;
      case 'b': c = '\b'; break;
      case 'f': c = '\f'; break;
      case 'n': c = '\n'; break;
      case 'r': c = '\r'; break;
      case 't': c = '\t'; break;
      default: free(buf); return invalid_token(s);
      }
    } else if (c == '"') break;
    if (n == l) {
      buf = (char*)realloc(buf, l+20);
      l += 20;
    }
    buf[n++] = c;
  }
  buf[n] = 0;
  if (c != '"') {
    free(buf);
    return invalid_token(s);
  }

  node = new_node();
  node->t = NODE_STRING;
  node->s = (char*)realloc(buf, n+1);
  return node;
}
예제 #5
0
static NODE*
parse_primitive(SCANNER *s) {
  char buf[BUFSIZ];
  size_t n = 0;
  char *e, c;
  NODE *x;

  while (n < sizeof(buf) && !s_eof(s)) {
    c = s_peek(s);
    if (c == -1) return NULL;
    if (isalnum(c) || strchr(SYMBOL_CHARS, c)) buf[n++] = s_getc(s);
    else break;
  }
  buf[n] = 0;

  x = new_node();
  if (match(buf, "nil", n)) {
    return x;
  }
  if (match(buf, "t", n)) {
    x->t = NODE_T;
    return x;
  }
  x->i = strtol(buf, &e, 10);
  if (e == buf+n) {
    x->t = NODE_INT;
    return x;
  }
  x->d = strtod(buf, &e);
  if (e == buf+n) {
    x->t = NODE_DOUBLE;
    return x;
  }
  x->t = NODE_IDENT;
  x->s = (char*)malloc(n + 1);
  memset(x->s, 0, n + 1);
  memcpy(x->s, buf, n);
  return x;
}
struct stack *b_current_stack(struct stack *balancer) {
    struct node *n = s_peek(balancer);
    return (struct stack *) (n ? n->value : NULL);
}