Brain_Fuck/include/bf_tokenizer.h

77 lines
1.4 KiB
C
Raw Normal View History

2023-11-17 00:30:48 -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_TOKENIZER_H
#define BRAINFUCK_MISC_BF_TOKENIZER_H
#include <vector>
#include <string>
2023-11-17 02:06:09 -05:00
#include <optional>
2023-11-17 00:30:48 -05:00
enum class bf_token
{
INC_DP,
DEC_DP,
INC_DV,
DEC_DV,
PRINT,
READ,
OPEN,
CLOSE
};
2023-11-17 02:06:09 -05:00
struct token_t
{
bf_token token;
2023-11-20 17:26:37 -05:00
std::optional<uint64_t> name = {};
2023-11-17 02:06:09 -05:00
2023-11-20 17:26:37 -05:00
explicit token_t(bf_token token): token(token)
{}
2023-11-17 02:06:09 -05:00
};
2023-11-20 17:26:37 -05:00
typedef std::optional<std::reference_wrapper<const token_t>> optional_token;
2023-11-17 00:30:48 -05:00
2023-11-20 17:26:37 -05:00
class tokenizer
{
private:
std::vector<token_t> tokens;
size_t conditionalCount = 0;
size_t currentIndex = 0;
void tokenize(const std::string& program);
void bf_name();
public:
explicit tokenizer(const std::string& program)
{
tokenize(program);
bf_name();
}
inline bool hasNext(){
return currentIndex < tokens.size();
}
inline void advance(){
currentIndex++;
}
2023-11-20 17:52:26 -05:00
inline const token_t& next() {
2023-11-20 17:26:37 -05:00
return tokens[currentIndex++];
}
2023-11-20 17:52:26 -05:00
inline const token_t& peek(){
2023-11-20 17:26:37 -05:00
return tokens[currentIndex];
}
2023-11-20 17:52:26 -05:00
void print();
2023-11-20 17:26:37 -05:00
void print(size_t index = 0);
};
2023-11-17 00:30:48 -05:00
#endif //BRAINFUCK_MISC_BF_TOKENIZER_H