Add the beginnings of a scope system

This commit is contained in:
Jason Turner 2009-06-05 22:39:49 +00:00
parent 2a4b1acfd1
commit 1dbaa4062c

View File

@ -9,6 +9,7 @@
#include <stdexcept> #include <stdexcept>
#include <vector> #include <vector>
#include <iostream> #include <iostream>
#include <deque>
#include "boxed_value.hpp" #include "boxed_value.hpp"
#include "type_info.hpp" #include "type_info.hpp"
@ -20,6 +21,13 @@ class BoxedCPP_System
public: public:
typedef std::multimap<std::string, boost::shared_ptr<Proxy_Function> > Function_Map; typedef std::multimap<std::string, boost::shared_ptr<Proxy_Function> > Function_Map;
typedef std::map<std::string, Type_Info> Type_Name_Map; typedef std::map<std::string, Type_Info> Type_Name_Map;
typedef std::map<std::string, Boxed_Value> Scope;
BoxedCPP_System()
{
m_scopes.push_back(Scope());
}
void register_function(const boost::shared_ptr<Proxy_Function> &f, const std::string &name) void register_function(const boost::shared_ptr<Proxy_Function> &f, const std::string &name)
{ {
@ -36,19 +44,36 @@ class BoxedCPP_System
template<typename Class> template<typename Class>
void add_object(const std::string &name, const Class &obj) void add_object(const std::string &name, const Class &obj)
{ {
//m_objects.insert(std::make_pair(name, Boxed_Value(obj))); m_scopes.back()[name] = Boxed_Value(obj);
m_objects[name] = Boxed_Value(obj);
} }
void new_scope()
{
m_scopes.push_back(Scope());
}
void pop_scope()
{
if (m_scopes.size() == 1)
{
m_scopes.pop_back();
} else {
throw std::range_error("Unable to pop global stack");
}
}
Boxed_Value get_object(const std::string &name) const Boxed_Value get_object(const std::string &name) const
{ {
std::map<std::string, Boxed_Value>::const_iterator itr = m_objects.find(name); for (size_t i = m_scopes.size()-1; i >= 0; --i)
if (itr != m_objects.end())
{ {
return itr->second; std::map<std::string, Boxed_Value>::const_iterator itr = m_scopes[i].find(name);
} else { if (itr != m_scopes[i].end())
throw std::range_error("Object not known: " + name); {
return itr->second;
}
} }
throw std::range_error("Object not known: " + name);
} }
template<typename Type> template<typename Type>
@ -76,7 +101,7 @@ class BoxedCPP_System
} }
private: private:
std::map<std::string, Boxed_Value > m_objects; std::deque<Scope> m_scopes;
Function_Map m_functions; Function_Map m_functions;
Type_Name_Map m_types; Type_Name_Map m_types;