Skip to content

Instantly share code, notes, and snippets.

@6167656e74323431
Last active April 22, 2020 00:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 6167656e74323431/3dc51a771612ad431b4c901809d013b2 to your computer and use it in GitHub Desktop.
Save 6167656e74323431/3dc51a771612ad431b4c901809d013b2 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
int loopCheck(FILE *file)
{
int close_counter = 0;
int open_counter = 0;
char current_char;
while ((current_char = fgetc(file)) != EOF)
{
if (current_char == '[')
open_counter++;
else if (current_char == ']')
close_counter++;
}
if (open_counter != close_counter)
return 1;
return 0;
}
int usableChars(FILE *file)
{
int characters = 0;
char current_char;
while ((current_char = fgetc(file)) != EOF)
{
if (current_char == '+' || current_char == '-' || current_char == '>' || current_char == '<'
|| current_char == '[' || current_char == ']' || current_char == '.' || current_char == ',')
characters++;
}
return characters;
}
int main(int argc, char const *argv[])
{
// declare varaibles
FILE *code_file;
int char_count, command_pointer;
char current_char;
if (argv[1] == NULL) // check if ifle exists
{
printf("Error: No file specified.\n");
return 0;
}
code_file = fopen(argv[1], "r"); // open file
if (code_file == NULL) // check if ifle exists
{
printf("Error: File not found.\n");
return 0;
}
rewind(code_file); // Check for errors
if (loopCheck(code_file))
{
printf("Error: Mismatched loops.\n");
return 0;
}
rewind(code_file); // set count
char_count = usableChars(code_file);
char *text = malloc(char_count + 1); // reserve space for data
if (text == NULL)
{
printf("Error: Memory cannot be allocated.\n");
return(0);
}
// load file
command_pointer = 0;
rewind(code_file);
while ((current_char = fgetc(code_file)) != EOF)
{
if (current_char == '+' || current_char == '-' || current_char == '>' || current_char == '<'
|| current_char == '[' || current_char == ']' || current_char == '.' || current_char == ',')
{
*(text + command_pointer) = current_char;
command_pointer++;
}
}
*(text + command_pointer) = 'x'; // end of program
fclose(code_file); // close file
unsigned char bss[256] = {0}; // var date
unsigned char bss_pointer = 0;
command_pointer = 0;
int stack_pointer = 0;
int stack[1024] = {0};
// run code
while (*(text + command_pointer) != 'x')
{
switch (*(text + command_pointer))
{
case '+' :
bss[bss_pointer]++;
command_pointer++;
break;
case '-' :
bss[bss_pointer]--;
command_pointer++;
break;
case '<' :
bss_pointer--;
command_pointer++;
break;
case '>' :
bss_pointer++;
command_pointer++;
break;
case '.' :
printf("%c", bss[bss_pointer]);
command_pointer++;
break;
case ',' :
bss[bss_pointer] = getchar();
command_pointer++;
break;
case '[' :
if (stack_pointer == 1024)
{
printf("\nError: Stack Overflow.");
return 0;
} else {
stack[stack_pointer] = command_pointer;
stack_pointer++;
command_pointer++;
break;
}
case ']' :
if (bss[bss_pointer] == 0)
{
stack[stack_pointer] = 0;
stack_pointer--;
command_pointer++;
} else
command_pointer = stack[stack_pointer - 1] + 1;
break;
}
}
return 0;
}
@ehhthing
Copy link

ehhthing commented May 17, 2018

this is a brainfuck interpreter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment