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

// State of an executing program
typedef struct {
  int sp;
  int ip;
  int* code;
  int code_length;
  int* stack;
  int stack_length;
} State;

void execute(State* state);
void step(State* state, int opcode);
void trace(State* state, int opcode);

int main(void) {
  // TODO variable input
  int code_length = 6;
  int stack_length = 2;

  // iconst 7
  // iconst 5
  // add
  // print
  int code[] = { 0, 7, 0, 5, 1, 2 };
  int stack[stack_length];
  // end input

  State* state = malloc(sizeof(state));
  state->sp = -1;
  state->ip = 0;

  // TODO from input
  state->code_length = code_length;
  state->code = code;
  state->stack_length = stack_length;
  state->stack = stack;

  execute(state);
  return 0;
}

void execute(State* state) {
  while(state->ip < state->code_length) {
    int opcode = state->code[state->ip++];
    trace(state, opcode);
    step(state, opcode);
  }

  printf("done\n");
}

void step(State* state, int opcode) {
  int a, b;

  switch(opcode) {
    case 0: // iconst
      state->stack[++state->sp] = state->code[state->ip++];
      break;
    case 1: // add
      a = state->stack[state->sp--];
      b = state->stack[state->sp--];
      state->stack[++state->sp] = a + b;
      break;
    case 2: // printf
      printf("%d\n", state->stack[state->sp--]);
      break;
  }
}

void trace(State* state, int opcode) {
  printf("[TRACE] %d@%d", opcode, state->ip - 1);

  printf("\t\tstack [");
  for (int i = 0; i < state->sp; i ++) {
    printf("%d,", state->stack[i]);
  }
  printf("%d]\n", state->stack[state->sp]);
}
<commit_msg>C interpreter: Add comparison opcodes<commit_after>#include <stdio.h>
#include <stdlib.h>

// State of an executing program
typedef struct {
  int sp;
  int ip;
  int* code;
  int code_length;
  int* stack;
  int stack_length;
} State;

void execute(State* state);
void step(State* state, int opcode);
void trace(State* state, int opcode);

int main(void) {
  // TODO variable input
  int code_length = 6;
  int stack_length = 2;

  // iconst 7
  // iconst 5
  // add
  // print
  int code[] = { 0, 7, 0, 5, 1, 2 };
  int stack[stack_length];
  // end input

  State* state = malloc(sizeof(state));
  state->sp = -1;
  state->ip = 0;

  // TODO from input
  state->code_length = code_length;
  state->code = code;
  state->stack_length = stack_length;
  state->stack = stack;

  execute(state);
  return 0;
}

void execute(State* state) {
  while(state->ip < state->code_length) {
    int opcode = state->code[state->ip++];
    trace(state, opcode);
    step(state, opcode);
  }

  printf("done\n");
}

void step(State* state, int opcode) {
  int a, b;

  switch(opcode) {
    case 0: // iconst
      state->stack[++state->sp] = state->code[state->ip++];
      break;
    case 1: // add
      a = state->stack[state->sp--];
      b = state->stack[state->sp--];
      state->stack[++state->sp] = a + b;
      break;
    case 2: // printf
      printf("%d\n", state->stack[state->sp--]);
      break;
    case 8: // eq
      a = state->stack[state->sp--];
      b = state->stack[state->sp--];
      state->stack[++state->sp] = a == b;
      break;
    case 9: // ne
      a = state->stack[state->sp--];
      b = state->stack[state->sp--];
      state->stack[++state->sp] = a != b;
      break;
    case 10: // lt
      a = state->stack[state->sp--];
      b = state->stack[state->sp--];
      state->stack[++state->sp] = a < b;
      break;
    case 11: // gt
      a = state->stack[state->sp--];
      b = state->stack[state->sp--];
      state->stack[++state->sp] = a > b;
      break;
    case 12: // lte
      a = state->stack[state->sp--];
      b = state->stack[state->sp--];
      state->stack[++state->sp] = a <= b;
      break;
    case 13: // gte
      a = state->stack[state->sp--];
      b = state->stack[state->sp--];
      state->stack[++state->sp] = a >= b;
      break;
    default:
      fprintf(stderr, "Unknown opcode %d", opcode);
      exit(1);
  }
}

void trace(State* state, int opcode) {
  printf("[TRACE] %d@%d", opcode, state->ip - 1);

  printf("\t\tstack [");
  for (int i = 0; i < state->sp; i ++) {
    printf("%d,", state->stack[i]);
  }
  printf("%d]\n", state->stack[state->sp]);
}

{'fraction_non_alphanumeric': 0.1974988201982067, 'fraction_numerical': 0.011090136857008022, 'mean_word_length': 2.451954397394137, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 99, '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': '3776672', 'n_tokens_mistral': 1622, 'n_tokens_neox': 1554, 'n_words': 385}