From 702d5c3b570bd4a75fc4aa211688addd78bf592e Mon Sep 17 00:00:00 2001 From: Brett Date: Mon, 13 Nov 2023 14:20:43 -0500 Subject: [PATCH] get fucked --- src/main.cpp | 43 +++++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 5671f41..ef17bba 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,22 +10,22 @@ class brainfuck size_t _dp; char* _data; - void check_size(){ + void check_size() + { if (_dp >= _size) { char* tmp = new char[_size * 2]; - std::memset(tmp, 0, _size * 2); + std::memset(&tmp[_size], 0, _size); std::memcpy(tmp, _data, _size); _size = _size * 2; delete[] _data; _data = tmp; } } + public: - brainfuck(): _size(30000), _dp(0), _data(new char[_size]) - { - std::memset(_data, 0, _size); - } + brainfuck(): _size(2), _dp(0), _data(new char[_size]) + {std::memset(_data, 0, _size);} void increment_dp() { @@ -44,28 +44,37 @@ class brainfuck _data[_dp]++; } - void decrement(){ + void decrement() + { check_size(); _data[_dp]--; } - void print(){ + void print() + { std::cout << _data[_dp]; } - void read(){ + void read() + { std::cin >> _data[_dp]; } - char is(){ + char is() + { return _data[_dp]; } + + ~brainfuck() + { delete[] _data; } }; template -void match(functor f, int sp, size_t& index, const std::string& program){ +void match(functor f, int sp, size_t& index, const std::string& program) +{ - while (f(index) < program.size()){ + while (f(index) < program.size()) + { if (program[index] == '[') sp++; if (program[index] == ']') @@ -85,9 +94,11 @@ int main() brainfuck fuck; size_t index = 0; - while (index < program.size()){ + while (index < program.size()) + { auto c = program[index]; - switch (c){ + switch (c) + { case '>': fuck.increment_dp(); break; @@ -108,11 +119,11 @@ int main() break; case '[': if (fuck.is() == 0) - match([](size_t& idx){return ++idx;}, 1, index, program); + match([](size_t& idx) { return ++idx; }, 1, index, program); break; case ']': if (fuck.is() != 0) - match([](size_t& idx){return --idx;}, -1, index, program); + match([](size_t& idx) { return --idx; }, -1, index, program); break; default: break;