Beispiel #1
0
/**
 * Reads a character, converts it to an instruction and repeats until the given character
 * 	occurs and will then return a linked list containing all instructions.
 *
 * @param stream The stream to read from.
 * @param until If this character is found in the stream, we will quit reading and return.
 * @param The head of the linked list containing the instructions.
 */
BrainfuckInstruction * brainfuck_parse_stream_until(FILE *stream, const int until) {
	BrainfuckInstruction *instruction = (BrainfuckInstruction *) malloc(sizeof(BrainfuckInstruction));
	instruction->next = 0;
	instruction->loop = 0;
	BrainfuckInstruction *root = instruction;
	char ch;
	char temp;
	while ((ch = fgetc(stream)) != until) {
		instruction->type = ch;
		instruction->difference = 1;
		switch(ch) {
		case BRAINFUCK_TOKEN_PLUS:
		case BRAINFUCK_TOKEN_MINUS:
			while ((temp = fgetc(stream)) != until && (temp == BRAINFUCK_TOKEN_PLUS 
					|| temp == BRAINFUCK_TOKEN_MINUS)) {
				if (temp == ch) {
					instruction->difference++;
				} else {
					instruction->difference--;
				}
			}
			ungetc(temp, stream);
			break;
		case BRAINFUCK_TOKEN_NEXT:
		case BRAINFUCK_TOKEN_PREVIOUS:
			while ((temp = fgetc(stream)) != until && (temp == BRAINFUCK_TOKEN_NEXT 
					|| temp == BRAINFUCK_TOKEN_PREVIOUS)) {
				if (temp == ch) {
					instruction->difference++;
				} else {
					instruction->difference--;
				}
			}
			ungetc(temp, stream);
			break;
		case BRAINFUCK_TOKEN_OUTPUT:
		case BRAINFUCK_TOKEN_INPUT:
			while ((temp = fgetc(stream)) != until && temp == ch)
				instruction->difference++;
			ungetc(temp, stream);
			break;
		case BRAINFUCK_TOKEN_LOOP_START:
			instruction->loop = brainfuck_parse_stream_until(stream, until);
			break;
		case BRAINFUCK_TOKEN_LOOP_END:
			return root;
		case BRAINFUCK_TOKEN_BREAK:
			break;
		default:
			continue;
		}
		instruction->next = (BrainfuckInstruction *) malloc(sizeof(BrainfuckInstruction));
		instruction->next->next = 0;
		instruction->next->loop = 0;
		instruction = instruction->next;
	}
	instruction->type = BRAINFUCK_TOKEN_LOOP_END;
	return root;
}
Beispiel #2
0
/*
 * 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>> ");
	}
}
Beispiel #3
0
/**
 * Reads a character, converts it to an instruction and repeats until the EOF character
 * 	occurs and will then return a linked list containing all instructions.
 *
 * @param head The head of the linked list that contains the instructions. 
 * @param stream The stream to read from.
 * @param The head of the linked list containing the instructions.
 */
BrainfuckInstruction * brainfuck_parse_stream(FILE *stream) {
	return brainfuck_parse_stream_until(stream, EOF);
}