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

typedef struct bf_state {
  int index;
  int codeindex;
  int loopdepth;
  int loopskip;
  char memory[1024];
  char code[65536];
  int loop[256];
} bf_state;

void incr(bf_state* state) {
  if (!state->loopskip) {
    ++state->memory[state->index];
  }
}

void decr(bf_state* state) {
  if (!state->loopskip) {
    --state->memory[state->index];
  }
}

void next(bf_state* state) {
  if (!state->loopskip) {
    if (++state->index >= sizeof(state->memory)) {
      fprintf(stderr, "bf: out of memory");
      exit(EXIT_FAILURE);
    }
  }
}

void prev(bf_state* state) {
  if (!state->loopskip) {
    if (--state->index < 0) {
      fprintf(stderr, "bf: negative address access");
      exit(EXIT_FAILURE);
    }
  }
}

void put(bf_state* state) {
  if (!state->loopskip) {
    putchar(state->memory[state->index]);
    fflush(stdout);
  }
}

void get(bf_state* state) {
  if (!state->loopskip) {
    state->memory[state->index] = getchar();
  }
}

void loopstart(bf_state* state) {
  if (!state->loopskip) {
    if (state->loopdepth >= sizeof(state->loop) - 1) {
      fprintf(stderr, "bf: too deep loops");
      exit(EXIT_FAILURE);
    }
  }
  state->loop[state->loopdepth++] = state->codeindex;
  if (!state->loopskip) {
    if (state->memory[state->index] == 0) {
      state->loopskip = state->loopdepth;
    }
  }
}

void loopend(bf_state* state) {
  if (--state->loopdepth < 0) {
    fprintf(stderr, "bf: too much loop close");
    exit(EXIT_FAILURE);
  }
  if (!state->loopskip) {
    if (state->memory[state->index] != 0) {
      state->codeindex = state->loop[state->loopdepth] - 1;
    }
  } else {
    if (state->memory[state->index] == 0) {
      if (state->loopskip == state->loopdepth + 1) {
        state->loopskip = 0;
      }
    }
  }
}

void clean(bf_state* state) {
  if (state->loopdepth == 0) {
    state->code[0] = '\0';
    state->codeindex = 0;
  }
}

void run(bf_state* state) {
  while (state->codeindex < sizeof(state->code)) {
    switch (state->code[state->codeindex++]) {
      case '+': incr(state); break;
      case '-': decr(state); break;
      case '>': next(state); break;
      case '<': prev(state); break;
      case '.': put(state);  break;
      case ',': get(state);  break;
      case '[': loopstart(state); break;
      case ']': loopend(state); break;
      case '\0': --state->codeindex; clean(state); return;
    }
  }
}

void cat(bf_state* state, char* buffer) {
  if (state->codeindex + strlen(buffer) < sizeof(state->code)) {
    strcat(state->code, buffer);
  } else {
    fprintf(stderr, "bf: input code too long");
    exit(EXIT_FAILURE);
  }
}

int main(int argc, char *argv[]) {
  char buffer[256];
  int i, bufferlen;
  FILE* fp = NULL;
  bf_state state = { 0, 0, 0, 0, { 0 }, { 0 }, { 0 } };
  if (argc >= 2 && strcmp(argv[1], "-") != 0 && strcmp(argv[1], "--") != 0)
    if ((fp = fopen(argv[1], "r")) == NULL) {
      fprintf(stderr, "bf: input file error");
      exit(EXIT_FAILURE);
    }
  if (fp == NULL) fp = stdin;
  while (fgets(buffer, sizeof(buffer), fp) != NULL) {
    cat(&state, buffer);
    run(&state);
  }<commit_msg>improve bf.c: new lines in the error messages<commit_after>#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct bf_state {
  int index;
  int codeindex;
  int loopdepth;
  int loopskip;
  char memory[1024];
  char code[65536];
  int loop[256];
} bf_state;

void incr(bf_state* state) {
  if (!state->loopskip) {
    ++state->memory[state->index];
  }
}

void decr(bf_state* state) {
  if (!state->loopskip) {
    --state->memory[state->index];
  }
}

void next(bf_state* state) {
  if (!state->loopskip) {
    if (++state->index >= sizeof(state->memory)) {
      fprintf(stderr, "bf: out of memory\n");
      exit(EXIT_FAILURE);
    }
  }
}

void prev(bf_state* state) {
  if (!state->loopskip) {
    if (--state->index < 0) {
      fprintf(stderr, "bf: negative address access\n");
      exit(EXIT_FAILURE);
    }
  }
}

void put(bf_state* state) {
  if (!state->loopskip) {
    putchar(state->memory[state->index]);
    fflush(stdout);
  }
}

void get(bf_state* state) {
  if (!state->loopskip) {
    state->memory[state->index] = getchar();
  }
}

void loopstart(bf_state* state) {
  if (!state->loopskip) {
    if (state->loopdepth >= sizeof(state->loop) - 1) {
      fprintf(stderr, "bf: too deep loops\n");
      exit(EXIT_FAILURE);
    }
  }
  state->loop[state->loopdepth++] = state->codeindex;
  if (!state->loopskip) {
    if (state->memory[state->index] == 0) {
      state->loopskip = state->loopdepth;
    }
  }
}

void loopend(bf_state* state) {
  if (--state->loopdepth < 0) {
    fprintf(stderr, "bf: too much loop close\n");
    exit(EXIT_FAILURE);
  }
  if (!state->loopskip) {
    if (state->memory[state->index] != 0) {
      state->codeindex = state->loop[state->loopdepth] - 1;
    }
  } else {
    if (state->memory[state->index] == 0) {
      if (state->loopskip == state->loopdepth + 1) {
        state->loopskip = 0;
      }
    }
  }
}

void clean(bf_state* state) {
  if (state->loopdepth == 0) {
    state->code[0] = '\0';
    state->codeindex = 0;
  }
}

void run(bf_state* state) {
  while (state->codeindex < sizeof(state->code)) {
    switch (state->code[state->codeindex++]) {
      case '+': incr(state); break;
      case '-': decr(state); break;
      case '>': next(state); break;
      case '<': prev(state); break;
      case '.': put(state);  break;
      case ',': get(state);  break;
      case '[': loopstart(state); break;
      case ']': loopend(state); break;
      case '\0': --state->codeindex; clean(state); return;
    }
  }
}

void cat(bf_state* state, char* buffer) {
  if (state->codeindex + strlen(buffer) < sizeof(state->code)) {
    strcat(state->code, buffer);
  } else {
    fprintf(stderr, "bf: input code too long\n");
    exit(EXIT_FAILURE);
  }
}

int main(int argc, char *argv[]) {
  char buffer[256];
  int i, bufferlen;
  FILE* fp = NULL;
  bf_state state = { 0, 0, 0, 0, { 0 }, { 0 }, { 0 } };
  if (argc >= 2 && strcmp(argv[1], "-") != 0 && strcmp(argv[1], "--") != 0)
    if ((fp = fopen(argv[1], "r")) == NULL) {
      fprintf(stderr, "bf: input file error\n");
      exit(EXIT_FAILURE);
    }
  if (fp == NULL) fp = stdin;
  while (fgets(buffer, sizeof(buffer), fp) != NULL) {
    cat(&state, buffer);
    run(&state);
  }
{'fraction_non_alphanumeric': 0.19668023802067022, 'fraction_numerical': 0.013153773880363295, 'mean_word_length': 2.9256299938537187, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 119, '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': '3409844', 'n_tokens_mistral': 2422, 'n_tokens_neox': 2410, 'n_words': 511}