diff options
| Commit | 99a9f9389a41a1df544ab51fc0170e48f10e34c8 (patch) | |
|---|---|---|
| Author | 2018-11-29 14:42:06 +0200 | |
| Committer | 2018-11-29 14:42:06 +0200 | |
| Files | 0ee5061e2034b2a8708af6b2af0caf8fe96f139b | |
| IPL-99a9f9389a41a1df544ab51fc0170e48f10e34c8.tar IPL-99a9f9389a41a1df544ab51fc0170e48f10e34c8.tar.gz IPL-99a9f9389a41a1df544ab51fc0170e48f10e34c8.zip | ||
| Parent | 72d9bbcc3154142af79fb686d9dc7e4d1fa1badb (diff) | |
Added Homework 3 - BF Interpreter (#64)
* homework 1
* Adding homework 3 - brainfuck interpreter
* Add README.md
5 files changed, 257 insertions, 0 deletions
| -rw-r--r-- | homework/81625/Brainfuck-Interpretator/README.md | 3 | ||||
| -rw-r--r-- | homework/81625/Brainfuck-Interpretator/src/BFI.cpp | 175 | ||||
| -rw-r--r-- | homework/81625/Brainfuck-Interpretator/src/BFI.h | 39 | ||||
| -rw-r--r-- | homework/81625/Brainfuck-Interpretator/src/main.cpp | 37 | ||||
| -rw-r--r-- | homework/81625/Brainfuck-Interpretator/src/run.sh | 3 |
diff --git a/homework/81625/Brainfuck-Interpretator/README.md b/homework/81625/Brainfuck-Interpretator/README.md new file mode 100644 index 0000000..e7000be --- /dev/null +++ b/homework/81625/Brainfuck-Interpretator/README.md @@ -0,0 +1,3 @@ +# IPL Homework 3 - Brainfuck Interpreter +# How to run: +Execute the 'run.sh' script from the src folder diff --git a/homework/81625/Brainfuck-Interpretator/src/BFI.cpp b/homework/81625/Brainfuck-Interpretator/src/BFI.cpp new file mode 100644 index 0000000..23d8c3e --- /dev/null +++ b/homework/81625/Brainfuck-Interpretator/src/BFI.cpp @@ -0,0 +1,175 @@ +#include "BFI.h" + +void BFI::moveMPLeft() +{ + --mMP; +} + +void BFI::moveMPRight() +{ + ++mMP; + if(mMP > mMemoryUsed) mMemoryUsed = mMP; +} + +void BFI::incCellAtMP() +{ + if(mMP >= 0 && mMP < MEMORY_SIZE) + { + ++mMemory[mMP]; + } + else + { + std::cout << "Out of memory bounds!!! MP: " << mMP << " IP: " << mIP << " " << mCode[mIP] << std::endl; + exit(1); + } +} + +void BFI::decCellAtMP() +{ + if(mMP >= 0 && mMP < MEMORY_SIZE) + { + --mMemory[mMP]; + } + else + { + std::cout << "Out of memory bounds!!! MP: " << mMP << " IP: " << mIP << " " << mCode[mIP] << std::endl; + exit(1); + } +} + +void BFI::printCellAtMP() +{ + if(mMP >= 0 && mMP < MEMORY_SIZE) + { + if(mMemory[mMP] == 10) + { + std::cout<<"\n"; + } + else + { + std::cout<<mMemory[mMP]; + } + } + else + { + std::cout << "Out of memory bounds!!! MP: " << mMP << " IP: " << mIP << " " << mCode[mIP] << std::endl; + exit(1); + } +} + +void BFI::readToCellAtMP() +{ + unsigned char c; + std::cin>>c; + if(mMP >= 0 && mMP < MEMORY_SIZE) + { + mMemory[mMP] = c; + } + else + { + std::cout << "Out of memory bounds!!! MP: " << mMP << " IP: " << mIP << " " << mCode[mIP] << std::endl; + exit(1); + } +} +void BFI::loopStart() +{ + if(mMP >= 0 && mMP < MEMORY_SIZE) + { + if(mMemory[mMP] == 0) + { + mIP = mBrackets[mIP]; + } + } + else + { + std::cout << "Out of memory bounds!!! MP: " << mMP << " IP: " << mIP << " " << mCode[mIP] << std::endl; + exit(1); + } +} + +void BFI::loopEnd() +{ + if(mMP >= 0 && mMP < MEMORY_SIZE) + { + if(mMemory[mMP] != 0) + { + mIP = mBrackets[mIP]; + } + } + else + { + std::cout << "Out of memory bounds!!! MP: " << mMP << " IP: " << mIP << " " << mCode[mIP] << std::endl; + exit(1); + } +} + +void BFI::interpret() +{ + while(mCode[mIP] != '\0') + { + if(mCode[mIP] == '[') + { + mStack.push(mIP); + } + else if(mCode[mIP] == ']') + { + if(mStack.empty()) + { + std::cout << "Unexpected closing bracket!!!\n"; + exit(1); + } + mBrackets[mIP] = mStack.top(); + mBrackets[mStack.top()] = mIP; + mStack.pop(); + } + ++mIP; + } + if(!mStack.empty()) + { + std::cout << "Unclosed opening bracket!!!\n"; + exit(1); + } + mIP = 0; + while(mCode[mIP] != '\0') + { + switch(mCode[mIP]) + { + case '>': moveMPRight(); break; + case '<': moveMPLeft(); break; + case '+': incCellAtMP(); break; + case '-': decCellAtMP(); break; + case '.': printCellAtMP(); break; + case ',': readToCellAtMP(); break; + case '[': loopStart(); break; + case ']': loopEnd(); break; + } + ++mIP; + } +} + +void BFI::printUsedMemory() +{ + unsigned cols = 16; + unsigned rows = mMemoryUsed/cols; + unsigned lastRow = mMemoryUsed%cols; + unsigned ind = 0; + std::cout.fill('0'); + for(unsigned i = 0; i < rows; ++i) + { + std::cout << std::setw(4) << ind << ": "; + for(unsigned j = 0; j < cols; j++) + { + std::cout << std::setw(3) << (unsigned)mMemory[ind++] << " "; + } + std::cout << std::endl; + } + if(lastRow != 0) + { + std::cout << std::setw(4) << ind << ": "; + while(ind <= mMemoryUsed) + { + std::cout << std::setw(3) << (unsigned)mMemory[ind++] << " "; + } + std::cout << std::endl; + } +} diff --git a/homework/81625/Brainfuck-Interpretator/src/BFI.h b/homework/81625/Brainfuck-Interpretator/src/BFI.h new file mode 100644 index 0000000..a058567 --- /dev/null +++ b/homework/81625/Brainfuck-Interpretator/src/BFI.h @@ -0,0 +1,39 @@ +#ifndef BFI_H +#define BFI_H + +#include<iostream> +#include<stack> +#include<vector> +#include<iomanip> + +class BFI +{ +private: + static const unsigned MEMORY_SIZE = 2048; + std::string mCode; + unsigned mIP, mMemoryUsed; + unsigned char mMemory[MEMORY_SIZE]; + int mMP; + std::stack<unsigned> mStack; + std::vector<unsigned> mBrackets; + + void moveMPLeft(); + void moveMPRight(); + void incCellAtMP(); + void decCellAtMP(); + void printCellAtMP(); + void readToCellAtMP(); + void loopStart(); + void loopEnd(); + +public: + BFI(const std::string& code) : + mCode(code), mIP(0), mMP(0), mMemoryUsed(0),mMemory{0} + { + mBrackets.reserve(mCode.length()); + } + void interpret(); + void printUsedMemory(); +}; + +#endif // BFI_H diff --git a/homework/81625/Brainfuck-Interpretator/src/main.cpp b/homework/81625/Brainfuck-Interpretator/src/main.cpp new file mode 100644 index 0000000..7003a78 --- /dev/null +++ b/homework/81625/Brainfuck-Interpretator/src/main.cpp @@ -0,0 +1,37 @@ +#include "BFI.h" + +int main() +{ + std::string helloWorld = "++++++++++[>+>+++>+++++++>++++++++++<<<<-]>>>++.>+.+++++++..+++.<<++.>+++++++++++++++.>.+++.------.--------.<<+.<."; + std::string squares = "++++[>+++++<-]>[<+++++>-]+<+[>[>+>+<<-]++>>[<<+>>-]>>>[-]++>[-]+>>>+[[-]++++++>>>]<<<[[<++++++++<++>>-]+<.<[>----<-]<]<<[>>>>>[>>>[-]+++++++++<[>-<-]+++++++++>[-[<->-]+[<<<]]<[>+<-]>]<<-]<<-]"; + std::string beer = ">++++++++++[<++++++++++>-]<->>>>>+++[>+++>+++<<-]<<<<+<[>[>+>+<<-]>>[-<<+>>]++++" + ">+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<[[-]>>>>>>[[-]<++++++++++<->>]<-[>+>+<<-]>[<+>" + "-]+>[[-]<->]<<<<<<<<<->>]<[>+>+<<-]>>[-<<+>>]+>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<" + "<[>>+>+<<<-]>>>[-<<<+>>>]++>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<[>+<[-]]<[>>+<<[-]]" + ">>[<<+>>[-]]<<<[>>+>+<<<-]>>>[-<<<+>>>]++++>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<[>+" + "<[-]]<[>>+<<[-]]>>[<<+>>[-]]<<[[-]>>>++++++++[>>++++++<<-]>[<++++++++[>++++++<-]" + ">.<++++++++[>------<-]>[<<+>>-]]>.<<++++++++[>>------<<-]<[->>+<<]<++++++++[<+++" + "+>-]<.>+++++++[>+++++++++<-]>+++.<+++++[>+++++++++<-]>.+++++..--------.-------.+" + "+++++++++++++>>[>>>+>+<<<<-]>>>>[-<<<<+>>>>]>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<<<" + "[>>>+>+<<<<-]>>>>[-<<<<+>>>>]+>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<<[>>+<<[-]]>[>+<" + "[-]]++>>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<+<[[-]>-<]>[<<<<<<<.>>>>>>>[-]]<<<<<<<<<" + ".>>----.---------.<<.>>----.+++..+++++++++++++.[-]<<[-]]<[>+>+<<-]>>[-<<+>>]+>+<" + "[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<<[>>+>+<<<-]>>>[-<<<+>>>]++++>+<[-<->]<[[-]>>-<<]" + ">>[[-]<<+>>]<<[>+<[-]]<[>>+<<[-]]>>[<<+>>[-]]<<[[-]>++++++++[<++++>-]<.>++++++++" + "++[>+++++++++++<-]>+.-.<<.>>++++++.------------.---.<<.>++++++[>+++<-]>.<++++++[" + ">----<-]>++.+++++++++++..[-]<<[-]++++++++++.[-]]<[>+>+<<-]>>[-<<+>>]+++>+<[-<->]" + "<[[-]>>-<<]>>[[-]<<+>>]<<[[-]++++++++++.>+++++++++[>+++++++++<-]>+++.+++++++++++" + "++.++++++++++.------.<++++++++[>>++++<<-]>>.<++++++++++.-.---------.>.<-.+++++++" + "++++.++++++++.---------.>.<-------------.+++++++++++++.----------.>.<+++++++++++" + "+.---------------.<+++[>++++++<-]>..>.<----------.+++++++++++.>.<<+++[>------<-]" + ">-.+++++++++++++++++.---.++++++.-------.----------.[-]>[-]<<<.[-]]<[>+>+<<-]>>[-" + "<<+>>]++++>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<[[-]++++++++++.[-]<[-]>]<+<]"; + std::string triangle = ">++++[<++++++++>-]>++++++++[>++++<-]>>++>>>+>>>+<<<<<<<<<<[-[->+<]>[-<+>>>.<<]>>>[[->++++++++[>++++<-]>.<<[->+<]+>[->++++++++++<<+>]>.[-]>]]+<<<[-[->+<]+>[-<+>>>-[->+<]++>[-<->]<<<]<<<<]++++++++++.+++.[-]<]+++++"; + + + BFI bfi(beer); + bfi.interpret(); + bfi.printUsedMemory(); + return 0; +} + diff --git a/homework/81625/Brainfuck-Interpretator/src/run.sh b/homework/81625/Brainfuck-Interpretator/src/run.sh new file mode 100644 index 0000000..edbb3e4 --- /dev/null +++ b/homework/81625/Brainfuck-Interpretator/src/run.sh @@ -0,0 +1,3 @@ +g++ -std=c++11 main.cpp BFI.cpp -o run +./run +rm run |
