-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.cpp
78 lines (66 loc) · 1.88 KB
/
token.cpp
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include<iostream>
#include<map>
using namespace std;
struct TokenType {
const string ILLEGAL = "ILLEGAL";
const string EoF = "EOF";
// Identifiers + literals
const string IDENT = "IDENTIFIER";
const string INT = "INT";
const string STRING = "STRING";
// Operators
const string ASSIGN = "ASSIGN";
const string PLUS = "PLUS";
const string MINUS = "MINUS";
const string SURPRISE = "SURPRISE";
const string ASTERISK = "ASTERISK";
const string SLASH = "SLASH";
const string EQ = "EQ";
const string NOT_EQ = "NOT_EQ";
const string LESS = "LESS_THAN";
const string GREATER = "GREATER_THAN";
// Delimiters
const string COMMA = "COMMA";
const string SEMICOLON = "SEMICOLON";
const string COLON = ":";
const string LPAREN = "LEFT_PARENTHESIS";
const string RPAREN = "RIGHT_PARENTHESIS";
const string LBRACE = "LEFT_BRACE";
const string RBRACE = "RIGHT_BRACE";
const string LBRACKET = "LEFT_BRACKET";
const string RBRACKET = "RIGHT_BRACKET";
// Keywords
const string FUNCTION = "FUNCTION";
const string LET = "LET";
const string TRUE = "TRUE";
const string FALSE = "FALSE";
const string IF = "IF";
const string ELSE = "ELSE";
const string RETURN = "RETURN";
} types;
struct Token {
string type;
string literal;
};
Token NewToken(string type, char c) {
string literal = {c};
if (c == 0) literal = "";
Token tok = {type: type, literal: literal};
return tok;
};
map<string, string> literalToType = {
{"let", types.LET},
{"fn", types.FUNCTION},
{"true", types.TRUE},
{"false", types.FALSE},
{"if", types.IF},
{"else", types.ELSE},
{"return", types.RETURN}
};
string getType(string literal) {
if (literalToType.count(literal)) {
return literalToType.find(literal)->second;
} else {
return types.IDENT;
}
}