c-repl/src/include/tokenize.h

47 lines
913 B
C
Raw Normal View History

2023-10-27 14:46:32 +02:00
#ifndef TOKENIZE_H
#define TOKENIZE_H
2023-10-27 14:37:03 +02:00
#include <vector>
2023-11-10 13:42:53 +01:00
#include <variant>
2023-10-27 17:53:58 +02:00
#include <string>
2023-11-15 11:59:38 +01:00
#include <stdexcept>
2023-10-27 14:37:03 +02:00
using namespace std;
enum class TokenType { Type, Identifier, Int, Plus, Minus, DoublePlus, DoubleMinus, Star, Slash, Percent, Equal, Semicolon, LParenthese, RParenthese };
2023-10-27 14:37:03 +02:00
enum class Type { Int };
2023-11-10 17:35:33 +01:00
using TokenData = variant<int, string, Type>;
2023-10-27 14:37:03 +02:00
2023-11-15 14:06:03 +01:00
struct CodePosition {
int line;
int column;
};
2023-10-27 14:37:03 +02:00
struct Token {
TokenType type;
2023-11-10 17:50:00 +01:00
TokenData data { };
2023-11-15 14:06:03 +01:00
CodePosition pos;
2023-10-27 14:46:32 +02:00
};
2023-11-15 13:40:37 +01:00
class TokenError : public runtime_error {
2023-11-15 11:59:38 +01:00
public:
2023-11-15 13:40:37 +01:00
explicit TokenError(const string& message)
: runtime_error(message) {}
2023-11-15 11:59:38 +01:00
};
2023-11-10 17:35:33 +01:00
/*
Parses a string into a vector of tokens
*/
2023-11-15 14:06:03 +01:00
vector<Token> tokenize(vector<string> str);
2023-11-10 17:35:33 +01:00
2023-11-10 19:04:24 +01:00
/*
Format and print a Token
*/
void _debug_print_token(Token token);
2023-11-10 17:35:33 +01:00
/*
Formats a list of tokens and prints it
*/
void _debug_print_tokens(vector<Token> tokens);
2023-10-27 14:46:32 +02:00
#endif