76 lines
1.6 KiB
C++
76 lines
1.6 KiB
C++
#pragma once
|
|
/*
|
|
* Created by Brett on 17/11/23.
|
|
* Licensed under GNU General Public License V3.0
|
|
* See LICENSE file for license detail
|
|
*/
|
|
|
|
#ifndef BRAINFUCK_MISC_BF_INTERPRETER_H
|
|
#define BRAINFUCK_MISC_BF_INTERPRETER_H
|
|
|
|
#include <cstdint>
|
|
#include <cstring>
|
|
#include <string>
|
|
#include <bf_tokenizer.h>
|
|
|
|
class brainfuck_interpreter
|
|
{
|
|
private:
|
|
size_t _size;
|
|
size_t _dp;
|
|
char* _data;
|
|
|
|
void check_size();
|
|
|
|
public:
|
|
brainfuck_interpreter(): _size(30000), _dp(0), _data(new char[_size])
|
|
{std::memset(_data, 0, _size);}
|
|
|
|
void increment_dp(size_t amount = 1)
|
|
{
|
|
_dp += amount;
|
|
}
|
|
|
|
void decrement_dp(size_t amount = 1)
|
|
{
|
|
_dp -= amount;
|
|
}
|
|
|
|
void increment(char amount = 1)
|
|
{
|
|
check_size();
|
|
_data[_dp] += amount;
|
|
}
|
|
|
|
void decrement(char amount = 1)
|
|
{
|
|
check_size();
|
|
_data[_dp] -= amount;
|
|
}
|
|
|
|
template<typename OutStream>
|
|
void print(OutStream& stream)
|
|
{
|
|
stream << _data[_dp];
|
|
}
|
|
|
|
template<typename InStream>
|
|
void read(InStream& stream)
|
|
{
|
|
_data[_dp] = stream.get();
|
|
}
|
|
|
|
char is()
|
|
{
|
|
return _data[_dp];
|
|
}
|
|
|
|
~brainfuck_interpreter()
|
|
{ delete[] _data; }
|
|
};
|
|
|
|
void interpret_bf(const std::string& program);
|
|
void interpret_bf(bf_tokenizer& tokenizer);
|
|
|
|
#endif //BRAINFUCK_MISC_BF_INTERPRETER_H
|