// Accepts valid Brainfun programs, which consist of the characters: // '>' '<' '+' '-' '.' ',' '[' ']' '~' '!' // Any other character in a Brainfun is considered to be a comment and should be ignored // // A Brainfun program is considered invalid if: // - there are mismatching opening and closing square brackets. // If a Brainfun program is found to be invalid, the exception Error is raised at the main // // Meaning of each character: // |-----------|--------------------------------------------------------------------------------------------| // | Character | Meaning | // |-----------|--------------------------------------------------------------------------------------------| // | > | Increment the data pointer by one (to point to the next cell to the right). | // |-----------|--------------------------------------------------------------------------------------------| // | < | Decrement the data pointer by one (to point to the next cell to the left). | // |-----------|--------------------------------------------------------------------------------------------| // | + | Increment the byte at the data pointer by one. | // |-----------|--------------------------------------------------------------------------------------------| // | - | Decrement the byte at the data pointer by one. | // |-----------|--------------------------------------------------------------------------------------------| // | . | Output the byte at the data pointer. | // |-----------|--------------------------------------------------------------------------------------------| // | , | Accept one byte of input, storing its value in the byte at the data pointer. | // |-----------|--------------------------------------------------------------------------------------------| // | [ | If the byte at the data pointer is zero, then instead of moving the instruction pointer | // | | forward to the next command, jump it forward to the command after the matching ] command. | // |-----------|--------------------------------------------------------------------------------------------| // | ] | If the byte at the data pointer is nonzero, then instead of moving the instruction pointer | // | | forward to the next command, jump it back to the command after the matching [ command. | // |-----------|--------------------------------------------------------------------------------------------| // Table taken from https://en.wikipedia.org/wiki/Brainfuck // // After each character is read, the Brainfun coroutine writes a corresponding line of C code to the output #pragma once #include #include #include _Coroutine Brainfun { std::ostream * out; // output stream char ch; // character passed by cocaller // YOU ADD MEMBERS HERE void main(); // coroutine main public: _Exception Error {}; // thrown at main if program is invalid _Exception EndOfFile {}; // thrown at coroutine to indicate EOF void next( char c ) { ch = c; // communication input resume(); // activate } // Brainfun::next // You may add a public construtor // DO NOT ADD ANY OTHER PUBLIC MEMBERS }; // Brainfun // Local Variables: // // mode: C++ // // compile-command: "make brainfun" // // End: //