From 99a9f9389a41a1df544ab51fc0170e48f10e34c8 Mon Sep 17 00:00:00 2001 From: thetedgt <44469837+thetedgt@users.noreply.github.com> Date: Thu, 29 Nov 2018 14:42:06 +0200 Subject: Added Homework 3 - BF Interpreter (#64) * homework 1 * Adding homework 3 - brainfuck interpreter * Add README.md --- homework/81625/Brainfuck-Interpretator/README.md | 3 + homework/81625/Brainfuck-Interpretator/src/BFI.cpp | 175 +++++++++++++++++++++ homework/81625/Brainfuck-Interpretator/src/BFI.h | 39 +++++ .../81625/Brainfuck-Interpretator/src/main.cpp | 37 +++++ homework/81625/Brainfuck-Interpretator/src/run.sh | 3 + 5 files changed, 257 insertions(+) create mode 100644 homework/81625/Brainfuck-Interpretator/README.md create mode 100644 homework/81625/Brainfuck-Interpretator/src/BFI.cpp create mode 100644 homework/81625/Brainfuck-Interpretator/src/BFI.h create mode 100644 homework/81625/Brainfuck-Interpretator/src/main.cpp create mode 100644 homework/81625/Brainfuck-Interpretator/src/run.sh 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<>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 +#include +#include +#include + +class BFI +{ +private: + static const unsigned MEMORY_SIZE = 2048; + std::string mCode; + unsigned mIP, mMemoryUsed; + unsigned char mMemory[MEMORY_SIZE]; + int mMP; + std::stack mStack; + std::vector 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 -- cgit v1.3.1-26-gb76a