Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fixed problem with [ and ] instructions

  • Loading branch information...
commit a6efbae16668b90becd3e3c5b0358e36108c35d4 1 parent 75e88d6
@j0hn authored
Showing with 126 additions and 123 deletions.
  1. +126 −123 src/interpreter.c
View
249 src/interpreter.c
@@ -3,8 +3,10 @@
*
* @author Fabian M.
*/
+
// Includes.
#include <stdio.h>
+#include <stdbool.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
@@ -16,9 +18,9 @@
// Program
typedef struct
{
- /**
- * The file to interpret.
- */
+ /**
+ * The file to interpret.
+ */
char *file;
/**
* The amount of memory cells to reserve.
@@ -30,23 +32,23 @@ typedef struct
* Output the given char.
*/
void interpreter_output_char(char c) {
- printf("%c", c);
- fflush(stdout);
+ printf("%c", c);
+ fflush(stdout);
}
/**
* Generates error message.
*/
void interpreter_error(char* message) {
- printf("Error at line %i, file \"%s\": %s\n",__LINE__, __FILE__, message);
- exit(EXIT_FAILURE);
+ printf("Error at line %i, file \"%s\": %s\n", __LINE__, __FILE__, message);
+ exit(EXIT_FAILURE);
}
/*
* Prints out the version of the program.
*/
void interpreter_version() {
- printf("Brainfuck interpreter, version %s.\n\n", interpreter_VERSION);
+ printf("Brainfuck interpreter, version %s.\n\n", VERSION);
printf("For more info, try \"./brainfuck -h\".\n");
exit(EXIT_SUCCESS);
@@ -56,7 +58,7 @@ void interpreter_version() {
* Prints out the help of the program.
*/
void interpreter_help() {
- printf("Brainfuck interpreter, version %s.\n\n", interpreter_VERSION);
+ printf("Brainfuck interpreter, version %s.\n\n", VERSION);
printf("Usage: ./brainfuck <input_file>.\n");
printf("\t\"-v\" Prints out the version of the program.\n");
@@ -64,6 +66,7 @@ void interpreter_help() {
exit(EXIT_SUCCESS);
}
+
/**
* Parses the command line arguments.
*
@@ -72,19 +75,19 @@ void interpreter_help() {
* @param char *argv[], the arguments passed string.
*/
void parse_args(Program *program, int argc, char *argv[]) {
- // Clear program->file value.
- program->file = "";
- // Make it compatible with C90.
- int i = 1;
+ // Clear program->file value.
+ program->file = "";
+ // Make it compatible with C90.
+ int i = 1;
for(; i < argc; i++) {
if(!strcmp("-v", argv[i])) {
- interpreter_version();
+ interpreter_version();
} else if(!strcmp("-h", argv[i])) {
- interpreter_help();
+ interpreter_help();
} else {
- program->file = argv[i];
- }
+ program->file = argv[i];
+ }
}
}
@@ -92,111 +95,112 @@ void parse_args(Program *program, int argc, char *argv[]) {
* Interprets the file given at startup.
*/
void interpret(Program *program) {
- if(!program->file || program->file == "") {
- printf("Missing required arguments, interpreter terminating. \n");
- interpreter_help();
- exit(EXIT_FAILURE);
- }
-
- // Open file wrapper
- FILE *input = fopen(program->file, "r");
-
- if(!input) {
- interpreter_error("Could not access input file.", "interpret");
- }
-
- // Set the amount of cells to 30000
- program->cells = 30000;
-
- // Contains all memory.
- int data[program->cells];
- // Points to the current Loop through all characters.index.
- int dataPointer = 0;
-
- // Get size of file.
- fseek(input, 0, SEEK_END); // seek to end of file
- int file_size = ftell(input); // get current file pointer
- fseek(input, 0, SEEK_SET); // seek back to beginning of file
-
- // Current char.
- char c;
- // Array that contains all characters.
- char *chars[file_size];
- // Index at the character array.
- int charPointer = 0;
-
- // Get file contents.
- while((c = fgetc(input)) != EOF) {
- chars[charPointer] = (char) c;
- charPointer++;
- }
-
- int i = 0;
-
- // Loop through all characters.
- for (charPointer = 0; charPointer < file_size; charPointer++) {
- c = chars[charPointer];
- switch(c) {
- case '>':
- if ((dataPointer + 1) > sizeof(data)) {
- interpreter_error("Data pointer is too big.");
- }
- dataPointer++;
- break;
- case '<':
- if ((dataPointer - 1) < 0) {
- interpreter_error("Data pointer is negative.");
- }
- dataPointer--;
- break;
- case '+':
- data[dataPointer]++;
- break;
- case '-':
- data[dataPointer]--;
- break;
- case '.':
- interpreter_output_char(data[dataPointer]);
- break;
- case ',':
- data[dataPointer] = (int) fgetc(stdin);
- break;
- case '[': {
- i = 1;
- while (i > 0) {
- char next = chars[++charPointer];
- if (next == '[')
- i++;
- else if (next == ']')
- i--;
- }
- }
- break;
- case ']': {
- i = 1;
- while (i > 0) {
- char previous = chars[--charPointer];
- if (previous == '[')
- i--;
- else if (previous == ']')
- i++;
- }
- charPointer--;
- }
- break;
- // Allow hashtags (#).
- case '#': {
- i = 1;
- while (i > 0) {
- char next = chars[++charPointer];
-
- if (next == '\n')
- i--;
- }
- }
- break;
- }
- }
+ if(!program->file || program->file == "") {
+ printf("Missing required arguments, interpreter terminating. \n");
+ interpreter_help();
+ exit(EXIT_FAILURE);
+ }
+
+ // Open file wrapper
+ FILE *input = fopen(program->file, "r");
+
+ if(!input) {
+ interpreter_error("Could not access input file.");
+ }
+
+ // Set the amount of cells to 30000
+ program->cells = 30000;
+
+ // Contains all memory.
+ int data[program->cells];
+ // Points to the current Loop through all characters.index.
+ int dataPointer = 0;
+
+ // Get size of file.
+ fseek(input, 0, SEEK_END); // seek to end of file
+ int file_size = ftell(input); // get current file pointer
+ fseek(input, 0, SEEK_SET); // seek back to beginning of file
+
+ // Current char.
+ char c;
+ // Array that contains all characters.
+ char *chars[file_size];
+ // Index at the character array.
+ int charPointer = 0;
+
+ // Get file contents.
+ while((c = fgetc(input)) != EOF) {
+ chars[charPointer] = (char) c;
+ charPointer++;
+ }
+
+ int i = 0;
+
+ // Loop through all characters.
+ for (charPointer = 0; charPointer < file_size; charPointer++) {
+ c = chars[charPointer];
+ switch(c) {
+ case '>':
+ if ((dataPointer + 1) > sizeof(data)) {
+ interpreter_error("Data pointer is too big.");
+ }
+ dataPointer++;
+ break;
+ case '<':
+ if ((dataPointer - 1) < 0) {
+ interpreter_error("Data pointer is negative.");
+ }
+ dataPointer--;
+ break;
+ case '+':
+ data[dataPointer]++;
+ break;
+ case '-':
+ data[dataPointer]--;
+ break;
+ case '.':
+ interpreter_output_char(data[dataPointer]);
+ break;
+ case ',':
+ data[dataPointer] = (int) fgetc(stdin);
+ break;
+ case '[':
+ if(data[dataPointer] == 0){
+ bool stop = false;
+
+ while (!stop) {
+ char next = chars[++charPointer];
+ if (next == ']'){
+ stop = true;
+ }
+ }
+ }
+ break;
+ case ']':
+ if(data[dataPointer] != 0){
+ bool stop = false;
+
+ while (!stop) {
+ char next = chars[--charPointer];
+ if (next == '['){
+ stop = true;
+ }
+ }
+ }
+ break;
+ // Allow hashtags (#).
+ case '#': {
+ i = 1;
+ while (i > 0) {
+ char next = chars[++charPointer];
+
+ if (next == '\n')
+ i--;
+ }
+ }
+ break;
+ }
+ }
}
/**
@@ -210,4 +214,3 @@ int main(int argc, char *argv[]) {
interpret(&program);
return 0;
}
-
Please sign in to comment.
Something went wrong with that request. Please try again.