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