diff --git a/interpreter.c b/interpreter.c old mode 100755 new mode 100644 index 82279b8..5d216ce --- a/interpreter.c +++ b/interpreter.c @@ -28,7 +28,7 @@ static void (*bfi_lexer_init)(void); static char bfi_memory[NB_MEMORY_WORDS]; /* Reading/Writing Head, which points to a memory word */ -static char *bfi_head; +static char *bfi_head; /************************************************************************** * PRIVATE INTERFACE * @@ -41,13 +41,13 @@ static int bfi_execution_error(char* reason, int code) { int i; printf( COLOR_RST "\n" COLOR_ERR "%s" COLOR_MSG "\n", reason); - + printf( COLOR_RST "At position : %s" COLOR_MSG "\n", (bfi_program + bfi_token_start)); - + for(i = 0; i < MEMORY_DUMP_SIZE; i++) printf("%s%02x", i%4==0? (i%16==0?"\n": "::"): " ", bfi_memory[i]); printf(COLOR_RST "\n"); - + return code; } @@ -56,7 +56,7 @@ static int bfi_execution_error(char* reason, int code) * to be executed. * Hypotheses: bfi_token_start points just after the opening bracket. */ -static int bfi_skip_forward(void) +static void bfi_skip_forward(void) { int context = 0; int next = -1; @@ -70,17 +70,15 @@ static int bfi_skip_forward(void) /* Closing bracket */ case JNB: context--; if(context == 0) - return next; + return; break; /* End of the program or unknown instruction. */ case EOT: case SOT: case UKW: - return next; + return; } } - - return next; } /** @@ -88,18 +86,18 @@ static int bfi_skip_forward(void) * to be executed. * Hypotheses: bfi_token_start points just before the closing bracket. */ -int bfi_skip_backward(void) +static void bfi_skip_backward(void) { int context = 0; int prev = -1; - + while((prev = bfi_read_previous_token(bfi_program, &bfi_token_start, bfi_length)) != SOT) { switch(prev) { /* Opening bracket */ case JZF: context--; if(context == 0) - return prev; + return; break; /* Closing bracket */ case JNB: context++; @@ -108,11 +106,9 @@ int bfi_skip_backward(void) case EOT: case SOT: case UKW: - return prev; + return; } } - - return prev; } /** @@ -124,13 +120,13 @@ int bfi_skip_backward(void) static int bfi_initialize(enum bfi_dialects dialect) { int i; - + /* Reset memory state. */ for(i = 0; i < NB_MEMORY_WORDS; i++) bfi_memory[i] = 0; - + bfi_head = bfi_memory; - + /* Initialize the parser functions. */ switch(dialect) { @@ -144,7 +140,7 @@ static int bfi_initialize(enum bfi_dialects dialect) bfi_lexer_init = heu_lexer_init; bfi_read_previous_token = heu_read_previous_token; bfi_read_next_token = heu_read_next_token; - + break; default: return -1; @@ -164,7 +160,7 @@ int bfi_execute(char* program, size_t length, enum bfi_dialects dialect, char pr int next = EOT; bfi_program = program; bfi_length = length; - + if(bfi_initialize(dialect) != 0) return -UNKNOWN_DIALECT; @@ -174,13 +170,13 @@ int bfi_execute(char* program, size_t length, enum bfi_dialects dialect, char pr { case MVL: --bfi_head; - + if(bfi_head < bfi_memory) return bfi_execution_error("HEAD Got past the start of memory!", -INVALID_MEMORY_ADDRESS); break; case MVR: ++bfi_head; - + if(bfi_head >= bfi_memory + NB_MEMORY_WORDS) return bfi_execution_error("HEAD Got past the end of memory!", -INVALID_MEMORY_ADDRESS); break; @@ -201,21 +197,23 @@ int bfi_execute(char* program, size_t length, enum bfi_dialects dialect, char pr break; case JZF: if(*bfi_head == 0) - next = bfi_skip_forward(); + bfi_skip_forward(); break; case JNB: if(*bfi_head != 0) - next = bfi_skip_backward(); + bfi_skip_backward(); break; case UKW: default: return bfi_execution_error("Unknown keyword !", -INVALID_OPERATION); } } - + bfi_execution_error("End of Program", *bfi_head); + /* Ensure there is an end of line */ printf("\n"); + return (int)*bfi_head; } diff --git a/main.c b/main.c old mode 100755 new mode 100644 index 4ee485e..4bb84b3 --- a/main.c +++ b/main.c @@ -171,7 +171,7 @@ int main(int argc, const char *argv[]) { perror("init"); printf(COLOR_ERR "Error during the loading of '%s'." COLOR_RST "\n", options.filename); - return 1; + return err; } /* Print the program as loaded into memory. */