Brain_Fuck/include/bf_tokenizer.h

108 lines
2.0 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_TOKENIZER_H
#define BRAINFUCK_MISC_BF_TOKENIZER_H
#include <vector>
#include <string>
#include <optional>
enum class bf_token
{
INC_DP,
DEC_DP,
INC_DV,
DEC_DV,
PRINT,
READ,
OPEN,
CLOSE
};
struct bf_token_t
{
bf_token type;
int64_t offset;
std::optional<uint64_t> name = {};
explicit bf_token_t(bf_token type, int64_t offset = 1): type(type), offset(offset)
{}
};
class bf_tokenizer
{
private:
std::vector<bf_token_t> tokens;
size_t conditionalCount = 0;
size_t currentIndex = 0;
void tokenize(const std::string& program);
void bf_name();
public:
explicit bf_tokenizer(const std::string& program)
{
tokenize(program);
bf_name();
if (hasNext())
{
// skip past comments
if (peek().type == bf_token::OPEN)
while (hasNext() && peek().type != bf_token::CLOSE)
advance();
}
}
inline bool hasNext()
{
return currentIndex < tokens.size();
}
inline size_t advance()
{
return currentIndex++;
}
inline const bf_token_t& next()
{
return tokens[advance()];
}
inline const bf_token_t& peek()
{
return tokens[currentIndex];
}
inline std::vector<bf_token_t>& data()
{
return tokens;
}
void print(size_t index);
inline void print()
{
print(currentIndex);
}
};
enum ast_token
{
INC_DP, DEC_DP, INC_VAL, DEC_VAL, PRINT, READ, WHILE
};
struct ast_token_t
{
ast_token type;
};
#endif //BRAINFUCK_MISC_BF_TOKENIZER_H