Created
January 21, 2016 01:59
-
-
Save Heasummn/ebbf27459a169b2c314f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> // IO | |
#include <string> // String variable | |
#include <fstream> // File IO | |
#define HEAPSIZE 30000 | |
// Prototypes | |
void lexer(std::string); | |
void parser(char); | |
void help(); | |
void argHandler(int, char*[]); | |
// Prototypes for all the different BF commands | |
void ptrIncr(); | |
void ptrDcrm(); | |
void brkOpen(); | |
void brkClose(); | |
void valIncr(); | |
void valDcrm(); | |
void print(); | |
void input(); | |
// global variables | |
char array[HEAPSIZE] = {0}; | |
char* arrPtr = array; | |
const char* commandPtr; | |
int main(int argc, char* argv[]) { | |
argHandler(argc, argv); | |
} | |
void lexer(std::string program) { | |
commandPtr = program.c_str(); // Is there any other way to do this? | |
// This makes sure we keep going as long as there is a command to interpret | |
// It seems a little hacky, is there any other way to do this? | |
while(*commandPtr) { | |
parser(*commandPtr); | |
commandPtr++; | |
} | |
} | |
void parser(char command) { | |
switch(command) { | |
case '>': | |
ptrIncr(); | |
break; | |
case '<': | |
ptrDcrm(); | |
break; | |
case '+': | |
valIncr(); | |
break; | |
case '-': | |
valDcrm(); | |
break; | |
case '[': | |
brkOpen(); | |
break; | |
case ']': | |
brkClose(); | |
break; | |
case '.': | |
print(); | |
break; | |
case ',': | |
input(); | |
break; | |
} | |
} | |
void argHandler(int argc, char* argv[]) { | |
std::string program; | |
if (argc == 1) { | |
help(); | |
while(program != "exit") { | |
std::cout << "BF> "; | |
std::cin >> program; | |
if(program == "help") { | |
help(); | |
} | |
lexer(program); | |
std::cout << "\n"; | |
} | |
} else { | |
std::ifstream file(argv[1]); // Read in the file | |
if(!file.is_open()) | |
std::cout << "ERROR: Opening file has failed"; | |
std::string program((std::istreambuf_iterator<char>(file)), | |
std::istreambuf_iterator<char>()); // Found from SO, please explain | |
lexer(program); | |
std::cout << "\n"; | |
} | |
} | |
void help() { | |
std::cout << "To exit, please type \"exit\", or hit ctrl-c. To see this again, please type \"help\"." << std::endl; | |
} | |
void ptrIncr() { // There should be some way for this to overflow | |
arrPtr++; | |
} | |
void ptrDcrm() { // There should be some way for this to underflow | |
arrPtr--; | |
} | |
void valIncr() { | |
(*arrPtr) = ((*arrPtr) + 1) % 255; | |
} | |
void valDcrm() { | |
(*arrPtr) = ((*arrPtr) - 1) % 255; | |
} | |
void brkOpen() { | |
if((int)*arrPtr == 0) { | |
int depth = 1; | |
while(depth != 0) { | |
commandPtr++; | |
if(*commandPtr == '[') | |
depth++; | |
else if(*commandPtr == ']') | |
depth--; | |
} | |
} | |
} | |
void brkClose() { | |
if(!( (int)*arrPtr == 0 )) { | |
int depth = 1; | |
while(depth != 0) { | |
commandPtr--; | |
if(*commandPtr == '[') | |
depth--; | |
else if(*commandPtr == ']') | |
depth++; | |
} | |
} | |
} | |
void print() { | |
if((int)*arrPtr <= 31) { | |
std::cout << ""; | |
} else { | |
std::cout << *arrPtr; | |
} | |
} | |
void input() { | |
std::cin >> *arrPtr; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bf: | |
g++ -Wall -o bf.exe bf.cpp | |
clean: | |
rm -f *.exe |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment