more C code removed
This commit is contained in:
parent
f07ac236bc
commit
2a6c809031
2 changed files with 16 additions and 39 deletions
|
|
@ -3,8 +3,20 @@
|
||||||
#include <cstdbool>
|
#include <cstdbool>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <list>
|
#include <list>
|
||||||
|
#include <map>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
static const std::map<std::string, Token::Type> keywords = {
|
||||||
|
{"and", Token::Type::AND}, {"class", Token::Type::CLASS},
|
||||||
|
{"else", Token::Type::ELSE}, {"false", Token::Type::FALSE},
|
||||||
|
{"for", Token::Type::FOR}, {"fun", Token::Type::FUN},
|
||||||
|
{"if", Token::Type::IF}, {"nil", Token::Type::NIL},
|
||||||
|
{"or", Token::Type::OR}, {"print", Token::Type::PRINT},
|
||||||
|
{"return", Token::Type::RETURN}, {"super", Token::Type::SUPER},
|
||||||
|
{"this", Token::Type::THIS}, {"true", Token::Type::TRUE},
|
||||||
|
{"var", Token::Type::VAR}, {"while", Token::Type::WHILE},
|
||||||
|
};
|
||||||
|
|
||||||
Scanner::Scanner(std::string s)
|
Scanner::Scanner(std::string s)
|
||||||
: had_error(false), current_pos(0), start(0), current_line(1), source(s),
|
: had_error(false), current_pos(0), start(0), current_line(1), source(s),
|
||||||
token_list(std::list<Token>()) {}
|
token_list(std::list<Token>()) {}
|
||||||
|
|
@ -117,11 +129,11 @@ void Scanner::identifier() {
|
||||||
|
|
||||||
std::string text = source.substr(start + 1, current_pos - start);
|
std::string text = source.substr(start + 1, current_pos - start);
|
||||||
|
|
||||||
const Token::Type *tokentype = get_keyword_token(text);
|
auto it = keywords.find(text);
|
||||||
if (tokentype == NULL) {
|
if (it != keywords.end()) {
|
||||||
add_token(Token::Type::IDENTIFIER);
|
add_token(it->second);
|
||||||
} else {
|
} else {
|
||||||
add_token(*tokentype);
|
add_token(Token::Type::IDENTIFIER);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -39,38 +39,3 @@ public:
|
||||||
void error(std::string message);
|
void error(std::string message);
|
||||||
void report(std::string where, std::string message);
|
void report(std::string where, std::string message);
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
const std::string key;
|
|
||||||
const Token::Type value;
|
|
||||||
} Item;
|
|
||||||
|
|
||||||
static const Item keywords[] = {
|
|
||||||
{"and", Token::Type::AND}, {"class", Token::Type::CLASS},
|
|
||||||
{"else", Token::Type::ELSE}, {"false", Token::Type::FALSE},
|
|
||||||
{"for", Token::Type::FOR}, {"fun", Token::Type::FUN},
|
|
||||||
{"if", Token::Type::IF}, {"nil", Token::Type::NIL},
|
|
||||||
{"or", Token::Type::OR}, {"print", Token::Type::PRINT},
|
|
||||||
{"return", Token::Type::RETURN}, {"super", Token::Type::SUPER},
|
|
||||||
{"this", Token::Type::THIS}, {"true", Token::Type::TRUE},
|
|
||||||
{"var", Token::Type::VAR}, {"while", Token::Type::WHILE}};
|
|
||||||
|
|
||||||
inline static const Token::Type *get_keyword_token(std::string key) {
|
|
||||||
int low = 0;
|
|
||||||
int high = sizeof(keywords) / sizeof(Item);
|
|
||||||
|
|
||||||
while (low < high) {
|
|
||||||
int mid = (low + high) / 2;
|
|
||||||
|
|
||||||
int c = keywords[mid].key.compare(key);
|
|
||||||
if (c == 0) {
|
|
||||||
return &keywords[mid].value;
|
|
||||||
}
|
|
||||||
if (c < 0) {
|
|
||||||
low = mid + 1;
|
|
||||||
} else {
|
|
||||||
high = mid;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue