Brain_Fuck/include/bf_tokenizer.h

108 lines
2.0 KiB
C
Raw Permalink 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-24 14:41:45 -05:00
struct bf_token_t
2023-11-17 02:06:09 -05:00
{
2023-11-20 19:24:45 -05:00
bf_token type;
int64_t offset;
2023-11-20 17:26:37 -05:00
std::optional<uint64_t> name = {};
2023-11-17 02:06:09 -05:00
2023-11-24 14:41:45 -05:00
explicit bf_token_t(bf_token type, int64_t offset = 1): type(type), offset(offset)
2023-11-20 17:26:37 -05:00
{}
2023-11-17 02:06:09 -05:00
};
2023-11-22 17:13:31 -05:00
class bf_tokenizer
2023-11-20 17:26:37 -05:00
{
private:
2023-11-24 14:41:45 -05:00
std::vector<bf_token_t> tokens;
2023-11-20 17:26:37 -05:00
size_t conditionalCount = 0;
size_t currentIndex = 0;
void tokenize(const std::string& program);
void bf_name();
public:
2023-11-22 17:13:31 -05:00
explicit bf_tokenizer(const std::string& program)
2023-11-20 17:26:37 -05:00
{
tokenize(program);
bf_name();
2023-11-24 14:41:45 -05:00
if (hasNext())
{
2023-11-22 17:26:19 -05:00
// skip past comments
if (peek().type == bf_token::OPEN)
while (hasNext() && peek().type != bf_token::CLOSE)
advance();
}
2023-11-20 17:26:37 -05:00
}
2023-11-24 14:41:45 -05:00
inline bool hasNext()
{
2023-11-20 17:26:37 -05:00
return currentIndex < tokens.size();
}
2023-11-24 14:41:45 -05:00
inline size_t advance()
{
2023-11-20 19:24:45 -05:00
return currentIndex++;
2023-11-20 17:26:37 -05:00
}
2023-11-24 14:41:45 -05:00
inline const bf_token_t& next()
{
2023-11-20 19:24:45 -05:00
return tokens[advance()];
2023-11-20 17:26:37 -05:00
}
2023-11-24 14:41:45 -05:00
inline const bf_token_t& peek()
{
2023-11-20 17:26:37 -05:00
return tokens[currentIndex];
}
2023-11-24 14:41:45 -05:00
inline std::vector<bf_token_t>& data()
{
2023-11-22 17:26:19 -05:00
return tokens;
}
2023-11-20 19:24:45 -05:00
void print(size_t index);
2023-11-24 14:41:45 -05:00
inline void print()
{
2023-11-20 19:24:45 -05:00
print(currentIndex);
}
2023-11-20 17:26:37 -05:00
};
2023-11-17 00:30:48 -05:00
2023-11-24 14:41:45 -05:00
enum ast_token
{
INC_DP, DEC_DP, INC_VAL, DEC_VAL, PRINT, READ, WHILE
};
struct ast_token_t
{
ast_token type;
};
2023-11-17 00:30:48 -05:00
#endif //BRAINFUCK_MISC_BF_TOKENIZER_H