Brain_Fuck/include/bf_interpreter.h

78 lines
1.7 KiB
C
Raw Permalink Normal View History

2023-11-17 00:25:52 -05:00
#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>
2023-11-22 17:13:31 -05:00
#include <bf_tokenizer.h>
2023-11-17 00:25:52 -05:00
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);}
2023-11-22 17:13:31 -05:00
void increment_dp(size_t amount = 1)
2023-11-17 00:25:52 -05:00
{
2023-11-22 17:13:31 -05:00
_dp += amount;
2023-11-17 00:25:52 -05:00
}
2023-11-22 17:13:31 -05:00
void decrement_dp(size_t amount = 1)
2023-11-17 00:25:52 -05:00
{
2023-11-22 17:13:31 -05:00
_dp -= amount;
2023-11-17 00:25:52 -05:00
}
2023-11-22 17:13:31 -05:00
void increment(char amount = 1)
2023-11-17 00:25:52 -05:00
{
check_size();
2023-11-22 17:13:31 -05:00
_data[_dp] += amount;
2023-11-17 00:25:52 -05:00
}
2023-11-22 17:13:31 -05:00
void decrement(char amount = 1)
2023-11-17 00:25:52 -05:00
{
check_size();
2023-11-22 17:13:31 -05:00
_data[_dp] -= amount;
2023-11-17 00:25:52 -05:00
}
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);
2023-11-22 17:13:31 -05:00
void interpret_bf(bf_tokenizer& tokenizer);
2023-11-24 13:53:27 -05:00
void interpret_bf_dynamic(bf_tokenizer& tokenizer);
void interpret_bf_matching(bf_tokenizer& tokenizer);
2023-11-17 00:25:52 -05:00
#endif //BRAINFUCK_MISC_BF_INTERPRETER_H