/* * Run the given brainfuck string. * * @param code The brainfuck string to run. * @return EXIT_SUCCESS if no errors are encountered, otherwise EXIT_FAILURE. */ int run_string(char *code) { BrainfuckState *state = brainfuck_state(); BrainfuckExecutionContext *context = brainfuck_context(BRAINFUCK_TAPE_SIZE); BrainfuckInstruction *instruction = brainfuck_parse_string(code); brainfuck_add(state, instruction); brainfuck_execute(state->root, context); brainfuck_destroy_context(context); brainfuck_destroy_state(state); return EXIT_SUCCESS; }
/* * Run the brainfuck interpreter in interactive mode. */ void run_interactive_console() { printf("brainfuck %s (%s, %s)\n", BRAINFUCK_VERSION, __DATE__, __TIME__); BrainfuckState *state = brainfuck_state(); BrainfuckExecutionContext *context = brainfuck_context(BRAINFUCK_TAPE_SIZE); BrainfuckInstruction *instruction; printf(">> "); while(1) { fflush(stdout); instruction = brainfuck_parse_stream_until(stdin, '\n'); brainfuck_add(state, instruction); brainfuck_execute(instruction, context); printf("\n>> "); } }
/* * Runs the given brainfuck file. * * @param file The brainfuck file to run. * @return EXIT_SUCCESS if no errors are encountered, otherwise EXIT_FAILURE. */ int run_file(FILE *file) { BrainfuckState *state = brainfuck_state(); BrainfuckExecutionContext *context = brainfuck_context(BRAINFUCK_TAPE_SIZE); if (file == NULL) { brainfuck_destroy_context(context); brainfuck_destroy_state(state); return EXIT_FAILURE; } brainfuck_add(state, brainfuck_parse_stream(file)); brainfuck_execute(state->root, context); brainfuck_destroy_context(context); brainfuck_destroy_state(state); fclose(file); return EXIT_SUCCESS; }