Skip to content

Instantly share code, notes, and snippets.

@TomerShech
Created August 26, 2019 09:31
Show Gist options
  • Save TomerShech/e7223c12e911abeecb848bc7efee05ca to your computer and use it in GitHub Desktop.
Save TomerShech/e7223c12e911abeecb848bc7efee05ca to your computer and use it in GitHub Desktop.
void interpret(char *ptr, char *file_contents)
{
int counter = 0;
int target = counter - 1;
while (*file_contents != '\0')
{
switch (*file_contents)
{
case INCREMENT_PTR:
++ptr;
break;
case DECREMENT_PTR:
--ptr;
break;
case INCREMENT_VALUE:
++*ptr;
break;
case DECREMENT_VALUE:
--*ptr;
break;
case PUT_VALUE:
putchar(*ptr);
break;
case GET_VALUE:
*ptr = getchar();
break;
case START_LOOP:
++counter;
if (*ptr == 0)
while (counter > 0)
{
++file_contents;
if (*file_contents == START_LOOP)
++counter;
else if (*file_contents == END_LOOP)
--counter;
}
break;
case END_LOOP:
while (counter != target)
{
--file_contents;
if (*file_contents == END_LOOP)
++counter; // entered an inner loop
else if (*file_contents == START_LOOP)
--counter; // exited a loop
}
--file_contents;
break;
default:
printf("Unrecognized character: %c\n", *file_contents);
break;
}
file_contents++;
}
printf(file_contents);
printf("\n%d\n", counter);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment