<commit_before>#include <stdio.h>
#include <stdlib.h>

#include "picrin.h"
#include "picrin/pair.h"

void pic_init_bool(pic_state *);
void pic_init_pair(pic_state *);
void pic_init_port(pic_state *);
void pic_init_number(pic_state *);
void pic_init_time(pic_state *);
void pic_init_system(pic_state *);
void pic_init_file(pic_state *);
void pic_init_proc(pic_state *);
void pic_init_symbol(pic_state *);
void pic_init_vector(pic_state *);
void pic_init_blob(pic_state *);
void pic_init_cont(pic_state *);
void pic_init_char(pic_state *);
void pic_init_error(pic_state *);
void pic_init_str(pic_state *);

void
pic_load_stdlib(pic_state *pic)
{
  static const char *fn = "piclib/built-in.scm";
  FILE *file;
  int n, i;
  pic_value v, vs;
  struct pic_proc *proc;

  file = fopen(fn, "r");
  if (file == NULL) {
    fputs("fatal error: could not read built-in.scm", stderr);
    abort();
  }

  n = pic_parse_file(pic, file, &vs);
  if (n < 0) {
    fputs("fatal error: built-in.scm broken", stderr);
    abort();
  }

  for (i = 0; i < n; ++i, vs = pic_cdr(pic, vs)) {
    v = pic_car(pic, vs);

    proc = pic_codegen(pic, v);
    if (proc == NULL) {
      fputs(pic->errmsg, stderr);
      fputs("fatal error: built-in.scm compilation failure", stderr);
      abort();
    }

    v = pic_apply(pic, proc, pic_nil_value());
    if (pic_undef_p(v)) {
      fputs(pic->errmsg, stderr);
      fputs("fatal error: built-in.scm evaluation failure", stderr);
      abort();
    }
  }

#if DEBUG
  puts("successfully loaded stdlib");
#endif
}

#define DONE pic_gc_arena_restore(pic, ai);

void
pic_init_core(pic_state *pic)
{
  int ai;

  ai = pic_gc_arena_preserve(pic);
  pic_init_bool(pic); DONE;
  pic_init_pair(pic); DONE;
  pic_init_port(pic); DONE;
  pic_init_number(pic); DONE;
  pic_init_time(pic); DONE;
  pic_init_system(pic); DONE;
  pic_init_file(pic); DONE;
  pic_init_proc(pic); DONE;
  pic_init_symbol(pic); DONE;
  pic_init_vector(pic); DONE;
  pic_init_blob(pic); DONE;
  pic_init_cont(pic); DONE;
  pic_init_char(pic); DONE;
  pic_init_error(pic); DONE;
  pic_init_str(pic); DONE;

  pic_load_stdlib(pic); DONE;
}<commit_msg>add feature function<commit_after>#include <stdio.h>
#include <stdlib.h>

#include "picrin.h"
#include "picrin/pair.h"

void pic_init_bool(pic_state *);
void pic_init_pair(pic_state *);
void pic_init_port(pic_state *);
void pic_init_number(pic_state *);
void pic_init_time(pic_state *);
void pic_init_system(pic_state *);
void pic_init_file(pic_state *);
void pic_init_proc(pic_state *);
void pic_init_symbol(pic_state *);
void pic_init_vector(pic_state *);
void pic_init_blob(pic_state *);
void pic_init_cont(pic_state *);
void pic_init_char(pic_state *);
void pic_init_error(pic_state *);
void pic_init_str(pic_state *);

void
pic_load_stdlib(pic_state *pic)
{
  static const char *fn = "piclib/built-in.scm";
  FILE *file;
  int n, i;
  pic_value v, vs;
  struct pic_proc *proc;

  file = fopen(fn, "r");
  if (file == NULL) {
    fputs("fatal error: could not read built-in.scm", stderr);
    abort();
  }

  n = pic_parse_file(pic, file, &vs);
  if (n < 0) {
    fputs("fatal error: built-in.scm broken", stderr);
    abort();
  }

  for (i = 0; i < n; ++i, vs = pic_cdr(pic, vs)) {
    v = pic_car(pic, vs);

    proc = pic_codegen(pic, v);
    if (proc == NULL) {
      fputs(pic->errmsg, stderr);
      fputs("fatal error: built-in.scm compilation failure", stderr);
      abort();
    }

    v = pic_apply(pic, proc, pic_nil_value());
    if (pic_undef_p(v)) {
      fputs(pic->errmsg, stderr);
      fputs("fatal error: built-in.scm evaluation failure", stderr);
      abort();
    }
  }

#if DEBUG
  puts("successfully loaded stdlib");
#endif
}

#define PUSH_SYM(pic, lst, name)		\
  lst = pic_cons(pic, pic_symbol_value(pic_intern_cstr(pic, name)), lst)

static pic_value
pic_features(pic_state *pic)
{
  pic_value fs = pic_nil_value();

  pic_get_args(pic, "");

  PUSH_SYM(pic, fs, "r7rs");
  PUSH_SYM(pic, fs, "ieee-float");
  PUSH_SYM(pic, fs, "picrin");

  return fs;
}

#define DONE pic_gc_arena_restore(pic, ai);

void
pic_init_core(pic_state *pic)
{
  int ai;

  ai = pic_gc_arena_preserve(pic);
  pic_init_bool(pic); DONE;
  pic_init_pair(pic); DONE;
  pic_init_port(pic); DONE;
  pic_init_number(pic); DONE;
  pic_init_time(pic); DONE;
  pic_init_system(pic); DONE;
  pic_init_file(pic); DONE;
  pic_init_proc(pic); DONE;
  pic_init_symbol(pic); DONE;
  pic_init_vector(pic); DONE;
  pic_init_blob(pic); DONE;
  pic_init_cont(pic); DONE;
  pic_init_char(pic); DONE;
  pic_init_error(pic); DONE;
  pic_init_str(pic); DONE;

  pic_load_stdlib(pic); DONE;

  pic_defun(pic, "features", pic_features);
}
{'fraction_non_alphanumeric': 0.1491736424125349, 'fraction_numerical': 0.0010731916720326251, 'mean_word_length': 4.5017709563164106, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '2579569', 'n_tokens_mistral': 1963, 'n_tokens_neox': 1903, 'n_words': 383}