diff --git a/language/pl0/pl0.cc b/language/pl0/pl0.cc index 8ce8896..221d3e5 100644 --- a/language/pl0/pl0.cc +++ b/language/pl0/pl0.cc @@ -63,19 +63,6 @@ string format_error_message(const string& path, size_t ln, size_t col, const str return ss.str(); } -bool read_file(const char* path, vector& buff) -{ - ifstream ifs(path, ios::in | ios::binary); - if (ifs.fail()) { - return false; - } - buff.resize(static_cast(ifs.seekg(0, ios::end).tellg())); - if (!buff.empty()) { - ifs.seekg(0, ios::beg).read(&buff[0], static_cast(buff.size())); - } - return true; -} - /* * Ast */ @@ -96,23 +83,20 @@ struct SymbolScope SymbolScope(shared_ptr outer) : outer(outer) {} 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); } bool has_constant(const string& ident) const { - auto ret = constants.find(ident) != constants.end(); - return ret ? true : (outer ? outer->has_constant(ident) : false); + return constants.count(ident) ? true : (outer ? outer->has_constant(ident) : false); } bool has_variable(const string& ident) const { - auto ret = variables.find(ident) != variables.end(); - return ret ? true : (outer ? outer->has_variable(ident) : false); + return variables.count(ident) ? true : (outer ? outer->has_variable(ident) : false); } bool has_procedure(const string& ident) const { - auto ret = procedures.find(ident) != procedures.end(); - return ret ? true : (outer ? outer->has_procedure(ident) : false); + return procedures.count(ident) ? true : (outer ? outer->has_procedure(ident) : false); } map constants; @@ -225,14 +209,14 @@ struct Environment auto it = scope->constants.find(ident); if (it != scope->constants.end()) { return it->second; - } else if (scope->variables.find(ident) != scope->variables.end()) { + } else if (scope->variables.count(ident)) { return variables.at(ident); } return outer->get_value(ident); } void set_variable(const string& ident, int val) { - if (scope->variables.find(ident) != scope->variables.end()) { + if (scope->variables.count(ident)) { variables[ident] = val; } else { outer->set_variable(ident, val); @@ -431,10 +415,16 @@ int main(int argc, const char** argv) // Read a source file into memory auto path = argv[1]; vector source; - if (!read_file(path, source)) { + + ifstream ifs(path, ios::in | ios::binary); + if (ifs.fail()) { cerr << "can't open the source file." << endl; return -1; } + source.resize(static_cast(ifs.seekg(0, ios::end).tellg())); + if (!source.empty()) { + ifs.seekg(0, ios::beg).read(&source[0], static_cast(source.size())); + } // Setup a PEG parser peg parser(grammar); diff --git a/peglib.h b/peglib.h index 67cd13c..5d6702d 100644 --- a/peglib.h +++ b/peglib.h @@ -1313,7 +1313,7 @@ inline void DefinitionReference::accept(Visitor& v) { v.visit(*this); } inline void AssignIDToDefinition::visit(Holder& ope) { auto p = (void*)ope.outer_; - if (ids.find(p) != ids.end()) { + if (ids.count(p)) { return; } auto id = ids.size(); @@ -1554,7 +1554,7 @@ private: void visit(DefinitionReference& ope) override { if (ope.name_ == name_) { s_ = ope.s_; - } else if (refs_.find(ope.name_) != refs_.end()) { + } else if (refs_.count(ope.name_)) { ; } else { refs_.insert(ope.name_); @@ -1645,7 +1645,7 @@ private: auto ope = sv[baseId + 2].get>(); auto& grammar = *data.grammar; - if (grammar.find(name) == grammar.end()) { + if (!grammar.count(name)) { auto& rule = grammar[name]; rule <= ope; rule.name = name; @@ -1729,7 +1729,7 @@ private: const auto& ident = sv[baseId].get(); - if (data.references.find(ident) == data.references.end()) { + if (!data.references.count(ident)) { data.references[ident] = sv.s; // for error handling } @@ -1835,7 +1835,7 @@ private: for (const auto& x : data.references) { const auto& name = x.first; auto ptr = x.second; - if (grammar.find(name) == grammar.end()) { + if (!grammar.count(name)) { if (log) { auto line = line_info(s, ptr); log(line.first, line.second, "'" + name + "' is not defined.");