-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.h
63 lines (50 loc) · 1.19 KB
/
lexer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#ifndef LEXER_INCLUDED
#define LEXER_INCLUDED
#include <string>
#include <optional>
#include <iostream>
#include <unordered_map>
#include <variant>
namespace BallLang {
enum TokenType {
ENDOFFILE,
ENDOFLINE,
DEF,
EXTERN,
IDENTIFIER,
NUMBER,
OPEN_PAREN,
CLOSE_PAREN,
BINOP,
COMMA,
ERROR,
};
const std::unordered_map<char, int> BinopPrecedence{
{'<', 10},
{'+', 20},
{'-', 20},
{'*', 40}
};
struct Token {
Token(TokenType type, const double value):
type(type), value(value)
{
if (type != NUMBER) std::cout << "ERROR: expected number" << std::endl;
}
Token(TokenType type, std::string&& value):
type(type), value(std::move(value))
{
if (type != IDENTIFIER) std::cout << "ERROR: expected identifier" << std::endl;
}
Token(TokenType type, const char value):
type(type), value(value)
{
if (type != BINOP) std::cout << "ERROR: expected BINOP" << std::endl;
}
Token(TokenType type): type(type) {} // still allocating space for variant here, could change structure
const TokenType type;
std::variant<std::string, double, char> value;
};
Token getTok();
}
#endif