<commit_before>/**
 * See Copyright Notice in picrin.h
 */

#include "picrin.h"

void
pic_panic(pic_state PIC_UNUSED(*pic), const char *msg)
{
  extern void abort();

#if DEBUG
  fprintf(stderr, "abort: %s\n", msg);
#else
  (void)msg;
#endif
  PIC_ABORT(pic);
}

void
pic_warnf(pic_state *pic, const char *fmt, ...)
{
  va_list ap;
  pic_value err_line;

  va_start(ap, fmt);
  err_line = pic_xvformat(pic, fmt, ap);
  va_end(ap);

  xfprintf(pic_stderr(pic)->file, "warn: %s\n", pic_str_cstr(pic, pic_str_ptr(pic_car(pic, err_line))));
}

void
pic_errorf(pic_state *pic, const char *fmt, ...)
{
  va_list ap;
  pic_value err_line, irrs;
  const char *msg;

  va_start(ap, fmt);
  err_line = pic_xvformat(pic, fmt, ap);
  va_end(ap);

  msg = pic_str_cstr(pic, pic_str_ptr(pic_car(pic, err_line)));
  irrs = pic_cdr(pic, err_line);

  pic_error(pic, msg, irrs);
}

const char *
pic_errmsg(pic_state *pic)
{
  pic_str *str;

  assert(! pic_undef_p(pic->err));

  if (! pic_error_p(pic->err)) {
    str = pic_format(pic, "~s", pic->err);
  } else {
    str = pic_error_ptr(pic->err)->msg;
  }

  return pic_str_cstr(pic, str);
}

static pic_value
native_exception_handler(pic_state *pic)
{
  pic_value err;
  struct pic_proc *cont;

  pic_get_args(pic, "o", &err);

  pic->err = err;

  cont = pic_proc_ptr(pic_attr_ref(pic, pic_obj_value(pic_get_proc(pic)), "@@escape"));

  pic_apply1(pic, cont, pic_false_value());

  PIC_UNREACHABLE();
}

void
pic_push_try(pic_state *pic, struct pic_escape *escape)
{
  struct pic_proc *cont, *handler;
  size_t xp_len;
  ptrdiff_t xp_offset;

  cont = pic_make_econt(pic, escape);

  handler = pic_make_proc(pic, native_exception_handler, "(native-exception-handler)");

  pic_attr_set(pic, pic_obj_value(handler), "@@escape", pic_obj_value(cont));

  if (pic->xp >= pic->xpend) {
    xp_len = (size_t)(pic->xpend - pic->xpbase) * 2;
    xp_offset = pic->xp - pic->xpbase;
    pic->xpbase = pic_realloc(pic, pic->xpbase, sizeof(struct pic_proc *) * xp_len);
    pic->xp = pic->xpbase + xp_offset;
    pic->xpend = pic->xpbase + xp_len;
  }

  *pic->xp++ = handler;
}

void
pic_pop_try(pic_state *pic)
{
  pic_value cont, escape;

  assert(pic->xp > pic->xpbase);

  cont = pic_attr_ref(pic, pic_obj_value(*--pic->xp), "@@escape");

  assert(pic_proc_p(cont));

  escape = pic_attr_ref(pic, cont, "@@escape");

  assert(pic_data_p(escape));

  ((struct pic_escape *)pic_data_ptr(escape)->data)->valid = false;
}

struct pic_error *
pic_make_error(pic_state *pic, pic_sym *type, const char *msg, pic_value irrs)
{
  struct pic_error *e;
  pic_str *stack;

  stack = pic_get_backtrace(pic);

  e = (struct pic_error *)pic_obj_alloc(pic, sizeof(struct pic_error), PIC_TT_ERROR);
  e->type = type;
  e->msg = pic_make_str_cstr(pic, msg);
  e->irrs = irrs;
  e->stack = stack;

  return e;
}

pic_value
pic_raise_continuable(pic_state *pic, pic_value err)
{
  struct pic_proc *handler;
  pic_value v;

  if (pic->xp == pic->xpbase) {
    pic_panic(pic, "no exception handler registered");
  }

  handler = *--pic->xp;

  pic_gc_protect(pic, pic_obj_value(handler));

  v = pic_apply1(pic, handler, err);

  *pic->xp++ = handler;

  return v;
}

void
pic_raise(pic_state *pic, pic_value err)
{
  pic_value val;

  val = pic_raise_continuable(pic, err);

  pic_pop_try(pic);

  pic_errorf(pic, "error handler returned with ~s on error ~s", val, err);
}

void
pic_throw(pic_state *pic, pic_sym *type, const char *msg, pic_value irrs)
{
  struct pic_error *e;<commit_msg>don't do pop_try in pic_raise<commit_after>/**
 * See Copyright Notice in picrin.h
 */

#include "picrin.h"

void
pic_panic(pic_state PIC_UNUSED(*pic), const char *msg)
{
  extern void abort();

#if DEBUG
  fprintf(stderr, "abort: %s\n", msg);
#else
  (void)msg;
#endif
  PIC_ABORT(pic);
}

void
pic_warnf(pic_state *pic, const char *fmt, ...)
{
  va_list ap;
  pic_value err_line;

  va_start(ap, fmt);
  err_line = pic_xvformat(pic, fmt, ap);
  va_end(ap);

  xfprintf(pic_stderr(pic)->file, "warn: %s\n", pic_str_cstr(pic, pic_str_ptr(pic_car(pic, err_line))));
}

void
pic_errorf(pic_state *pic, const char *fmt, ...)
{
  va_list ap;
  pic_value err_line, irrs;
  const char *msg;

  va_start(ap, fmt);
  err_line = pic_xvformat(pic, fmt, ap);
  va_end(ap);

  msg = pic_str_cstr(pic, pic_str_ptr(pic_car(pic, err_line)));
  irrs = pic_cdr(pic, err_line);

  pic_error(pic, msg, irrs);
}

const char *
pic_errmsg(pic_state *pic)
{
  pic_str *str;

  assert(! pic_undef_p(pic->err));

  if (! pic_error_p(pic->err)) {
    str = pic_format(pic, "~s", pic->err);
  } else {
    str = pic_error_ptr(pic->err)->msg;
  }

  return pic_str_cstr(pic, str);
}

static pic_value
native_exception_handler(pic_state *pic)
{
  pic_value err;
  struct pic_proc *cont;

  pic_get_args(pic, "o", &err);

  pic->err = err;

  cont = pic_proc_ptr(pic_attr_ref(pic, pic_obj_value(pic_get_proc(pic)), "@@escape"));

  pic_apply1(pic, cont, pic_false_value());

  PIC_UNREACHABLE();
}

void
pic_push_try(pic_state *pic, struct pic_escape *escape)
{
  struct pic_proc *cont, *handler;
  size_t xp_len;
  ptrdiff_t xp_offset;

  cont = pic_make_econt(pic, escape);

  handler = pic_make_proc(pic, native_exception_handler, "(native-exception-handler)");

  pic_attr_set(pic, pic_obj_value(handler), "@@escape", pic_obj_value(cont));

  if (pic->xp >= pic->xpend) {
    xp_len = (size_t)(pic->xpend - pic->xpbase) * 2;
    xp_offset = pic->xp - pic->xpbase;
    pic->xpbase = pic_realloc(pic, pic->xpbase, sizeof(struct pic_proc *) * xp_len);
    pic->xp = pic->xpbase + xp_offset;
    pic->xpend = pic->xpbase + xp_len;
  }

  *pic->xp++ = handler;
}

void
pic_pop_try(pic_state *pic)
{
  pic_value cont, escape;

  assert(pic->xp > pic->xpbase);

  cont = pic_attr_ref(pic, pic_obj_value(*--pic->xp), "@@escape");

  assert(pic_proc_p(cont));

  escape = pic_attr_ref(pic, cont, "@@escape");

  assert(pic_data_p(escape));

  ((struct pic_escape *)pic_data_ptr(escape)->data)->valid = false;
}

struct pic_error *
pic_make_error(pic_state *pic, pic_sym *type, const char *msg, pic_value irrs)
{
  struct pic_error *e;
  pic_str *stack;

  stack = pic_get_backtrace(pic);

  e = (struct pic_error *)pic_obj_alloc(pic, sizeof(struct pic_error), PIC_TT_ERROR);
  e->type = type;
  e->msg = pic_make_str_cstr(pic, msg);
  e->irrs = irrs;
  e->stack = stack;

  return e;
}

pic_value
pic_raise_continuable(pic_state *pic, pic_value err)
{
  struct pic_proc *handler;
  pic_value v;

  if (pic->xp == pic->xpbase) {
    pic_panic(pic, "no exception handler registered");
  }

  handler = *--pic->xp;

  pic_gc_protect(pic, pic_obj_value(handler));

  v = pic_apply1(pic, handler, err);

  *pic->xp++ = handler;

  return v;
}

void
pic_raise(pic_state *pic, pic_value err)
{
  pic_value val;

  val = pic_raise_continuable(pic, err);

  --pic->xp;

  pic_errorf(pic, "error handler returned with ~s on error ~s", val, err);
}

void
pic_throw(pic_state *pic, pic_sym *type, const char *msg, pic_value irrs)
{
  struct pic_error *e;
{'fraction_non_alphanumeric': 0.15717344753747323, 'fraction_numerical': 0.0008565310492505353, 'mean_word_length': 4.380952380952381, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 74, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6468575', 'n_tokens_mistral': 2930, 'n_tokens_neox': 2849, 'n_words': 593}