HAI
I HAS A QUESTION
I'm trying to write a small && simple interpreter..of a lisp like language (Almost complete!)..and the following is what I'd build variable and symbol scopes on.
Any noticeable problem ?
Code:
namespace mm
{
template<typename T>
class scope
{
std::vector< std::map<std::string,T> > data;
public:
T* find(std::string s)
{
for(auto m=data.rbegin();m!=data.rend();m++)
{
auto p = (*m).find(s);
if(p!=(*m).end())return &((*p).second);
}
return nullptr;
};
void new_local(std::map<std::string,T> sm){data.push_back(sm);};
void exit_scope(){data.pop_back();};
};
}
Can the pointer returning be avoided ?
(Other than exceptions...since it'd complicate the error handling too much..Most running code will already be within some try `s)
PS: The following demonstrates my goal (It seems achieved..for now)
Code:
int main()
{
mm::scope<int> s;
std::map<std::string,int> s1 = {{"Hey",42},{"Foo",84}};
std::map<std::string,int> s2 = {{"Buddy",43},{"Bar",85}};
s.new_local(s1);s.new_local(s2);
std::cout<<*s.find("Foo");
return 0;
}
KTHXBYE