All pastes #906520 Raw Edit

Untitled

public text v1 · immutable
·published 2008-02-16 19:35 UTC
rendered paste body
#ifndef ENV_HPP
#define ENV_HPP

#include <map>
#include <string>
#include <vector>

#include "util.hpp"

template <typename T>
struct Environment {
  typedef std::map<std::string, T> Frame;
  typedef std::vector<Frame> stack;
private:
  stack stack_;
public:
  void push_frame(const Frame& f) {
    stack_.push_back(f);
  }
  void pop_frame() {
    stack_.pop_back();
  }

  T lookup(const std::string& name) const {
    stack::const_reverse_iterator it;
    for(it = stack_.rbegin(); it != stack_.rend(); ++it) {
      Frame::const_iterator mit(it->find(name));
      if (mit != it->end()) {
        return *mit;
      }
    }
    error("Unbound variable name: " + name);
  }

  bool is_bound(const std::string& name) const {
    try {
      T t(lookup(name));
      return true;
    } catch (std::string) {
      return false;
    }
  }
};

#endif

/*

env.hpp: In member function ‘T Environment<T>::lookup(const std::string&) const’:
env.hpp:25: error: expected `;' before ‘it’
env.hpp:26: error: ‘it’ was not declared in this scope
env.hpp:27: error: expected `;' before ‘mit’
env.hpp:28: error: ‘mit’ was not declared in this scope
env.hpp: In member function ‘T Environment<T>::lookup(const std::string&) const [with T = ValuePtr]’:
value.hpp:108:   instantiated from here
env.hpp:25: error: dependent-name ‘std::vector<std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, T, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, T> > >,std::allocator<std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, T, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, T> > > > >::const_reverse_iterator’ is parsed as a non-type, but instantiation yields a type
env.hpp:25: note: say ‘typename std::vector<std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, T, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, T> > >,std::allocator<std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, T, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, T> > > > >::const_reverse_iterator’ if a type is meant
value.hpp:108:   instantiated from here
env.hpp:27: error: dependent-name ‘std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >,T,std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >,std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, T> > >::const_iterator’ is parsed as a non-type, but instantiation yields a type
env.hpp:27: note: say ‘typename std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >,T,std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >,std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, T> > >::const_iterator’ if a type is meant
make: *** [parser.o] Error 1
*/