Code cleanup.

This commit is contained in:
yhirose 2015-08-06 18:27:38 -04:00
parent 6fefd8ea84
commit 5ebd01c846
2 changed files with 18 additions and 28 deletions

View File

@ -63,19 +63,6 @@ string format_error_message(const string& path, size_t ln, size_t col, const str
return ss.str(); return ss.str();
} }
bool read_file(const char* path, vector<char>& buff)
{
ifstream ifs(path, ios::in | ios::binary);
if (ifs.fail()) {
return false;
}
buff.resize(static_cast<unsigned int>(ifs.seekg(0, ios::end).tellg()));
if (!buff.empty()) {
ifs.seekg(0, ios::beg).read(&buff[0], static_cast<streamsize>(buff.size()));
}
return true;
}
/* /*
* Ast * Ast
*/ */
@ -96,23 +83,20 @@ struct SymbolScope
SymbolScope(shared_ptr<SymbolScope> outer) : outer(outer) {} SymbolScope(shared_ptr<SymbolScope> outer) : outer(outer) {}
bool has_symbol(const string& ident) const { bool has_symbol(const string& ident) const {
auto ret = constants.find(ident) != constants.end() || variables.find(ident) != variables.end(); auto ret = constants.count(ident) || variables.count(ident);
return ret ? true : (outer ? outer->has_symbol(ident) : false); return ret ? true : (outer ? outer->has_symbol(ident) : false);
} }
bool has_constant(const string& ident) const { bool has_constant(const string& ident) const {
auto ret = constants.find(ident) != constants.end(); return constants.count(ident) ? true : (outer ? outer->has_constant(ident) : false);
return ret ? true : (outer ? outer->has_constant(ident) : false);
} }
bool has_variable(const string& ident) const { bool has_variable(const string& ident) const {
auto ret = variables.find(ident) != variables.end(); return variables.count(ident) ? true : (outer ? outer->has_variable(ident) : false);
return ret ? true : (outer ? outer->has_variable(ident) : false);
} }
bool has_procedure(const string& ident) const { bool has_procedure(const string& ident) const {
auto ret = procedures.find(ident) != procedures.end(); return procedures.count(ident) ? true : (outer ? outer->has_procedure(ident) : false);
return ret ? true : (outer ? outer->has_procedure(ident) : false);
} }
map<string, int> constants; map<string, int> constants;
@ -225,14 +209,14 @@ struct Environment
auto it = scope->constants.find(ident); auto it = scope->constants.find(ident);
if (it != scope->constants.end()) { if (it != scope->constants.end()) {
return it->second; return it->second;
} else if (scope->variables.find(ident) != scope->variables.end()) { } else if (scope->variables.count(ident)) {
return variables.at(ident); return variables.at(ident);
} }
return outer->get_value(ident); return outer->get_value(ident);
} }
void set_variable(const string& ident, int val) { void set_variable(const string& ident, int val) {
if (scope->variables.find(ident) != scope->variables.end()) { if (scope->variables.count(ident)) {
variables[ident] = val; variables[ident] = val;
} else { } else {
outer->set_variable(ident, val); outer->set_variable(ident, val);
@ -431,10 +415,16 @@ int main(int argc, const char** argv)
// Read a source file into memory // Read a source file into memory
auto path = argv[1]; auto path = argv[1];
vector<char> source; vector<char> source;
if (!read_file(path, source)) {
ifstream ifs(path, ios::in | ios::binary);
if (ifs.fail()) {
cerr << "can't open the source file." << endl; cerr << "can't open the source file." << endl;
return -1; return -1;
} }
source.resize(static_cast<unsigned int>(ifs.seekg(0, ios::end).tellg()));
if (!source.empty()) {
ifs.seekg(0, ios::beg).read(&source[0], static_cast<streamsize>(source.size()));
}
// Setup a PEG parser // Setup a PEG parser
peg parser(grammar); peg parser(grammar);

View File

@ -1313,7 +1313,7 @@ inline void DefinitionReference::accept(Visitor& v) { v.visit(*this); }
inline void AssignIDToDefinition::visit(Holder& ope) { inline void AssignIDToDefinition::visit(Holder& ope) {
auto p = (void*)ope.outer_; auto p = (void*)ope.outer_;
if (ids.find(p) != ids.end()) { if (ids.count(p)) {
return; return;
} }
auto id = ids.size(); auto id = ids.size();
@ -1554,7 +1554,7 @@ private:
void visit(DefinitionReference& ope) override { void visit(DefinitionReference& ope) override {
if (ope.name_ == name_) { if (ope.name_ == name_) {
s_ = ope.s_; s_ = ope.s_;
} else if (refs_.find(ope.name_) != refs_.end()) { } else if (refs_.count(ope.name_)) {
; ;
} else { } else {
refs_.insert(ope.name_); refs_.insert(ope.name_);
@ -1645,7 +1645,7 @@ private:
auto ope = sv[baseId + 2].get<std::shared_ptr<Ope>>(); auto ope = sv[baseId + 2].get<std::shared_ptr<Ope>>();
auto& grammar = *data.grammar; auto& grammar = *data.grammar;
if (grammar.find(name) == grammar.end()) { if (!grammar.count(name)) {
auto& rule = grammar[name]; auto& rule = grammar[name];
rule <= ope; rule <= ope;
rule.name = name; rule.name = name;
@ -1729,7 +1729,7 @@ private:
const auto& ident = sv[baseId].get<std::string>(); const auto& ident = sv[baseId].get<std::string>();
if (data.references.find(ident) == data.references.end()) { if (!data.references.count(ident)) {
data.references[ident] = sv.s; // for error handling data.references[ident] = sv.s; // for error handling
} }
@ -1835,7 +1835,7 @@ private:
for (const auto& x : data.references) { for (const auto& x : data.references) {
const auto& name = x.first; const auto& name = x.first;
auto ptr = x.second; auto ptr = x.second;
if (grammar.find(name) == grammar.end()) { if (!grammar.count(name)) {
if (log) { if (log) {
auto line = line_info(s, ptr); auto line = line_info(s, ptr);
log(line.first, line.second, "'" + name + "' is not defined."); log(line.first, line.second, "'" + name + "' is not defined.");