<commit_before>#include <stdio.h>

#include "picrin.h"

#if PIC_ENABLE_READLINE
# include <string.h>
# include <stdlib.h>
# include <readline/readline.h>
# include <readline/history.h>
#endif

#define CODE_MAX_LENGTH 1024
#define LINE_MAX_LENGTH 256

int
main()
{
  pic_state *pic;
  char code[CODE_MAX_LENGTH] = "", line[LINE_MAX_LENGTH];
  char *read_line, *prompt;
  pic_value v;
  struct pic_proc *proc;
  int ai;
  bool r;

#if ! PIC_ENABLE_READLINE
  char last_char;
  int char_index;
#endif

  pic = pic_open();

  ai = pic_gc_arena_preserve(pic);

  while (1) {
    prompt = code[0] == '\0' ? "> " : "* ";

#if PIC_ENABLE_READLINE
    read_line = readline(prompt);
    if (read_line == NULL) {
      line[0] = '\0';
    }
    else {
      strncpy(line, read_line, LINE_MAX_LENGTH - 1);
      add_history(read_line);
      free(read_line);
    }
#else
    printf(prompt);

    char_index = 0;
    while ((last_char = getchar()) != '\n') {
      if (last_char == EOF)
	goto eof;
      if (char_index == LINE_MAX_LENGTH)
	goto overflow;
      line[char_index++] = last_char;
    }
    line[char_index] = '\0';
#endif

    if (strlen(code) + strlen(line) >= CODE_MAX_LENGTH)
      goto overflow;
    strcat(code, line);

    /* read */
    r = pic_parse(pic, code, &v);
    if (! r) {			/* wait for more input */
      pic_gc_arena_restore(pic, ai);
      continue;
    }
    code[0] = '\0';
    if (pic_undef_p(v)) {	/* parse error */
      pic_gc_arena_restore(pic, ai);
      continue;
    }

#if DEBUG
    printf("[read: ");
    pic_debug(pic, v);
    printf("]\n");
#endif

    /* eval */
    proc = pic_codegen(pic, v, pic->global_env);
    v = pic_run(pic, proc, pic_nil_value());

    /* print */
    printf("=> ");
    pic_debug(pic, v);
    printf("\n");

    pic_gc_arena_restore(pic, ai);
  }

#if ! PIC_ENABLE_READLINE
 eof:
  puts("");
  goto exit;
#endif

 overflow:
  puts("** [fatal] line input overflow");
  goto exit;

 exit:
  pic_close(pic);

  return 0;
}<commit_msg>exit repl with ^D<commit_after>#include <stdio.h>

#include "picrin.h"

#if PIC_ENABLE_READLINE
# include <string.h>
# include <stdlib.h>
# include <readline/readline.h>
# include <readline/history.h>
#endif

#define CODE_MAX_LENGTH 1024
#define LINE_MAX_LENGTH 256

int
main()
{
  pic_state *pic;
  char code[CODE_MAX_LENGTH] = "", line[LINE_MAX_LENGTH];
  char *read_line, *prompt;
  pic_value v;
  struct pic_proc *proc;
  int ai;
  bool r;

#if ! PIC_ENABLE_READLINE
  char last_char;
  int char_index;
#endif

  pic = pic_open();

  ai = pic_gc_arena_preserve(pic);

  while (1) {
    prompt = code[0] == '\0' ? "> " : "* ";

#if PIC_ENABLE_READLINE
    read_line = readline(prompt);
    if (read_line == NULL) {
      goto eof;
    }
    else {
      strncpy(line, read_line, LINE_MAX_LENGTH - 1);
      add_history(read_line);
      free(read_line);
    }
#else
    printf(prompt);

    char_index = 0;
    while ((last_char = getchar()) != '\n') {
      if (last_char == EOF)
	goto eof;
      if (char_index == LINE_MAX_LENGTH)
	goto overflow;
      line[char_index++] = last_char;
    }
    line[char_index] = '\0';
#endif

    if (strlen(code) + strlen(line) >= CODE_MAX_LENGTH)
      goto overflow;
    strcat(code, line);

    /* read */
    r = pic_parse(pic, code, &v);
    if (! r) {			/* wait for more input */
      pic_gc_arena_restore(pic, ai);
      continue;
    }
    code[0] = '\0';
    if (pic_undef_p(v)) {	/* parse error */
      pic_gc_arena_restore(pic, ai);
      continue;
    }

#if DEBUG
    printf("[read: ");
    pic_debug(pic, v);
    printf("]\n");
#endif

    /* eval */
    proc = pic_codegen(pic, v, pic->global_env);
    v = pic_run(pic, proc, pic_nil_value());

    /* print */
    printf("=> ");
    pic_debug(pic, v);
    printf("\n");

    pic_gc_arena_restore(pic, ai);
  }

 eof:
  puts("");
  goto exit;

 overflow:
  puts("** [fatal] line input overflow");
  goto exit;

 exit:
  pic_close(pic);

  return 0;
}
{'fraction_non_alphanumeric': 0.15945330296127563, 'fraction_numerical': 0.008605416350291066, 'mean_word_length': 2.8294573643410854, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 21, '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': '1614476', 'n_tokens_mistral': 1587, 'n_tokens_neox': 1524, 'n_words': 336}