Example #1
0
File: regexp.c Project: krig/picrin
static pic_value
pic_regexp_regexp_match(pic_state *pic)
{
  pic_value reg;
  const char *input;
  regmatch_t match[100];
  pic_value matches, positions;
  pic_str *str;
  int i, offset;

  pic_get_args(pic, "oz", &reg, &input);

  pic_assert_type(pic, reg, regexp);

  matches = pic_nil_value();
  positions = pic_nil_value();

  if (strchr(pic_regexp_data_ptr(reg)->flags, 'g') != NULL) {
    /* global search */

    offset = 0;
    while (regexec(&pic_regexp_data_ptr(reg)->reg, input, 1, match, 0) != REG_NOMATCH) {
      pic_push(pic, pic_obj_value(pic_str_new(pic, input, match[0].rm_eo - match[0].rm_so)), matches);
      pic_push(pic, pic_int_value(offset), positions);

      offset += match[0].rm_eo;
      input += match[0].rm_eo;
    }
  } else {
    /* local search */

    if (regexec(&pic_regexp_data_ptr(reg)->reg, input, 100, match, 0) == 0) {
      for (i = 0; i < 100; ++i) {
        if (match[i].rm_so == -1) {
          break;
        }
        str = pic_str_new(pic, input + match[i].rm_so, match[i].rm_eo - match[i].rm_so);
        pic_push(pic, pic_obj_value(str), matches);
        pic_push(pic, pic_int_value(match[i].rm_so), positions);
      }
    }
  }

  if (pic_nil_p(matches)) {
    matches = pic_false_value();
    positions = pic_false_value();
  } else {
    matches = pic_reverse(pic, matches);
    positions = pic_reverse(pic, positions);
  }
  return pic_values2(pic, matches, positions);
}
Example #2
0
static pic_value
pic_vec_vector_for_each(pic_state *pic)
{
  struct pic_proc *proc;
  int argc, i, len, j;
  pic_value *argv, vals;

  pic_get_args(pic, "l*", &proc, &argc, &argv);

  len = INT_MAX;
  for (i = 0; i < argc; ++i) {
    pic_assert_type(pic, argv[i], vec);

    len = len < pic_vec_ptr(argv[i])->len
      ? len
      : pic_vec_ptr(argv[i])->len;
  }

  for (i = 0; i < len; ++i) {
    vals = pic_nil_value();
    for (j = 0; j < argc; ++j) {
      pic_push(pic, pic_vec_ptr(argv[j])->data[i], vals);
    }
    pic_apply_list(pic, proc, vals);
  }

  return pic_undef_value();
}
Example #3
0
static pic_value
pic_str_string_for_each(pic_state *pic)
{
  struct pic_proc *proc;
  int argc, len, i, j;
  pic_value *argv, vals;

  pic_get_args(pic, "l*", &proc, &argc, &argv);

  if (argc == 0) {
    pic_errorf(pic, "string-map: one or more strings expected, but got zero");
  } else {
    pic_assert_type(pic, argv[0], str);
    len = pic_str_len(pic_str_ptr(argv[0]));
  }
  for (i = 1; i < argc; ++i) {
    pic_assert_type(pic, argv[i], str);

    len = len < pic_str_len(pic_str_ptr(argv[i]))
      ? len
      : pic_str_len(pic_str_ptr(argv[i]));
  }

  for (i = 0; i < len; ++i) {
    vals = pic_nil_value();
    for (j = 0; j < argc; ++j) {
      pic_push(pic, pic_char_value(pic_str_ref(pic, pic_str_ptr(argv[j]), i)), vals);
    }
    pic_apply_list(pic, proc, vals);
  }

  return pic_undef_value();
}
Example #4
0
File: regexp.c Project: krig/picrin
static pic_value
pic_regexp_regexp_split(pic_state *pic)
{
  pic_value reg;
  const char *input;
  regmatch_t match;
  pic_value output = pic_nil_value();

  pic_get_args(pic, "oz", &reg, &input);

  pic_assert_type(pic, reg, regexp);

  while (regexec(&pic_regexp_data_ptr(reg)->reg, input, 1, &match, 0) != REG_NOMATCH) {
    pic_push(pic, pic_obj_value(pic_str_new(pic, input, match.rm_so)), output);

    input += match.rm_eo;
  }

  pic_push(pic, pic_obj_value(pic_str_new_cstr(pic, input)), output);

  return pic_reverse(pic, output);
}
Example #5
0
static pic_value
pic_dict_dictionary_to_alist(pic_state *pic)
{
  struct pic_dict *dict;
  pic_value item, alist = pic_nil_value();
  pic_sym *sym;
  khiter_t it;

  pic_get_args(pic, "d", &dict);

  pic_dict_for_each (sym, dict, it) {
    item = pic_cons(pic, pic_obj_value(sym), pic_dict_ref(pic, dict, sym));
    pic_push(pic, item, alist);
  }
Example #6
0
static pic_value
pic_str_string_map(pic_state *pic)
{
  struct pic_proc *proc;
  pic_value *argv, vals, val;
  int argc, i, len, j;
  pic_str *str;
  char *buf;

  pic_get_args(pic, "l*", &proc, &argc, &argv);

  if (argc == 0) {
    pic_errorf(pic, "string-map: one or more strings expected, but got zero");
  } else {
    pic_assert_type(pic, argv[0], str);
    len = pic_str_len(pic_str_ptr(argv[0]));
  }
  for (i = 1; i < argc; ++i) {
    pic_assert_type(pic, argv[i], str);

    len = len < pic_str_len(pic_str_ptr(argv[i]))
      ? len
      : pic_str_len(pic_str_ptr(argv[i]));
  }
  buf = pic_malloc(pic, len);

  pic_try {
    for (i = 0; i < len; ++i) {
      vals = pic_nil_value();
      for (j = 0; j < argc; ++j) {
        pic_push(pic, pic_char_value(pic_str_ref(pic, pic_str_ptr(argv[j]), i)), vals);
      }
      val = pic_apply_list(pic, proc, vals);

      pic_assert_type(pic, val, char);
      buf[i] = pic_char(val);
    }
    str = pic_make_str(pic, buf, len);
  }
  pic_catch {
    pic_free(pic, buf);
    pic_raise(pic, pic->err);
  }

  pic_free(pic, buf);

  return pic_obj_value(str);
}
Example #7
0
static pic_value
pic_dict_dictionary_map(pic_state *pic)
{
  struct pic_proc *proc;
  struct pic_dict *dict;
  khiter_t it;
  khash_t(dict) *kh;
  pic_value ret = pic_nil_value();

  pic_get_args(pic, "ld", &proc, &dict);

  kh = &dict->hash;

  for (it = kh_begin(kh); it != kh_end(kh); ++it) {
    if (kh_exist(kh, it)) {
      pic_push(pic, pic_apply1(pic, proc, pic_obj_value(kh_key(kh, it))), ret);
    }
  }

  return pic_reverse(pic, ret);
}
Example #8
0
static pic_value
pic_vec_vector_to_list(pic_state *pic)
{
  struct pic_vector *vec;
  pic_value list;
  int n, start, end, i;

  n = pic_get_args(pic, "v|ii", &vec, &start, &end);

  switch (n) {
  case 1:
    start = 0;
  case 2:
    end = vec->len;
  }

  list = pic_nil_value();

  for (i = start; i < end; ++i) {
    pic_push(pic, vec->data[i], list);
  }
  return pic_reverse(pic, list);
}
Example #9
0
void
pic_add_feature(pic_state *pic, const char *feature)
{
  pic_push(pic, pic_intern_cstr(pic, feature), pic->features);
}