Prevented memory under and overun

This commit is contained in:
id101010
2016-11-10 02:08:40 +01:00
parent 619a559f06
commit f5d7aa90c4

15
bfckr.c
View File

@@ -15,11 +15,12 @@
// Amount of memory on the band tape
#define MEMORY_SIZE 10000
#define MAX_INPUT_SIZE 1000
#define MAX_INPUT_SIZE 10000
// Memory initialized with zeros and its pointer
char memory[MEMORY_SIZE] = {0};
char *p = memory;
int memcnt = 0;
// Prototypes
void bfuck_parser(char *input);
@@ -44,11 +45,23 @@ void bfuck_parser(char *input)
for(size_t i = 0; input[i] != 0; i++) { // where i is the instruction pointer
switch(input[i]) {
case '>':
if(memcnt >= MEMORY_SIZE) { // prevent overrun
p = &memory[0];
memcnt = 0;
} else {
++p; // increment data pointer
++memcnt;
}
break;
case '<':
if(memcnt < 0) { // prevent underun
p = &memory[MEMORY_SIZE - 1];
memcnt = MEMORY_SIZE - 1;
} else {
--p; // decrement data pointer
--memcnt;
}
break;
case '+':