/** * find matching symbol */ TokenPtr SourceScope::findSymbol(TokenPtr token) { // find symbol in current scope for (auto & sym : m_symbols) { if (sym->getLexeme() == token->getLexeme()) return sym; } // parent scope if (auto p = m_parent.lock()) return p->findSymbol(token); // didn't find return nullptr; }
/** * Add new identifier. * return false if identifier with the given name already in the list */ bool SourceScope::addSymbol(TokenPtr token) { // find existing auto iter = std::find_if(m_symbols.begin(), m_symbols.end(), [&](const TokenPtr & p) { return p->getLexeme() == token->getLexeme(); }); // foung means it contains symbol already if (iter != m_symbols.end()) return false; // add it m_symbols.push_back(token); // success return true; }