-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhack_map.cpp
79 lines (67 loc) · 2.42 KB
/
hack_map.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
79
#include "hack_map.h"
#include "error.h"
#include <algorithm>
#include <utility>
std::string HackMap::get_keyword (Keyword value) {
return keywords[value];
}
std::string HackMap::get_token (TokenType value) {
return tokens[value];
}
template <typename T> bool HackMap::contains (T target, std::vector<T> values) {
return std::any_of (values.begin (), values.end (),
[&] (const auto& value) { return value == target; });
}
Keyword HackMap::get_keyword (const std::string& target) {
for (auto& [key, value] : keywords)
if (value == target)
return key;
throw Error ("HackMap::get_keyword: Keyword not found");
}
bool HackMap::contains_keyword (std::string target) {
return std::any_of (keywords.begin (), keywords.end (),
[&] (const auto& pair) { return pair.second == target; });
}
bool HackMap::contains_symbol (char test) {
return contains (test, symbols);
}
bool HackMap::contains_operator (char test) {
return contains (test, operators);
}
bool HackMap::contains_keyword_constant (std::string target) {
return contains (std::move (target), keyword_constants);
}
std::map<Keyword, std::string> HackMap::keywords = {
{ Keyword::CLASS, "class" },
{ Keyword::METHOD, "method" },
{ Keyword::FUNCTION, "function" },
{ Keyword::CONSTRUCTOR, "constructor" },
{ Keyword::INT, "int" },
{ Keyword::BOOL, "bool" },
{ Keyword::CHAR, "char" },
{ Keyword::VOID, "void" },
{ Keyword::VAR, "var" },
{ Keyword::STATIC, "static" },
{ Keyword::FIELD, "field" },
{ Keyword::LET, "let" },
{ Keyword::DO, "do" },
{ Keyword::IF, "if" },
{ Keyword::ELSE, "else" },
{ Keyword::WHILE, "while" },
{ Keyword::RETURN, "return" },
{ Keyword::TRUE, "true" },
{ Keyword::FALSE, "false" },
{ Keyword::THIS, "this" },
{ Keyword::NULL_keyword, "null" },
};
std::map<TokenType, std::string> HackMap::tokens = {
{ TokenType::SYMBOL, "symbol" },
{ TokenType::IDENTIFIER, "identifier" },
{ TokenType::KEYWORD, "keyword" },
{ TokenType::INT_CONST, "int_constant" },
{ TokenType::STRING_CONST, "string_constant" },
};
std::vector<char> HackMap::symbols = { '(', ')', '{', '}', '[', ']', ',', ';',
'.', '+', '-', '*', '/', '&', '|', '<', '>', '=', '~' };
std::vector<char> HackMap::operators = { '+', '-', '*', '/', '&', '|', '<', '>', '~' };
std::vector<std::string> HackMap::keyword_constants = { "true", "false", "null", "this" };