diff options
author | Evan Martin <martine@danga.com> | 2010-11-14 19:08:15 (GMT) |
---|---|---|
committer | Evan Martin <martine@danga.com> | 2010-11-14 19:08:21 (GMT) |
commit | b6870609e68fc607422a594c22834e255e7d6c60 (patch) | |
tree | a15d73e0d0e42011d22676af73df4b9e38a4fa32 /eval_env.h | |
parent | a160b2628499f63f103a0deb39ee8670627bde32 (diff) | |
download | Ninja-b6870609e68fc607422a594c22834e255e7d6c60.zip Ninja-b6870609e68fc607422a594c22834e255e7d6c60.tar.gz Ninja-b6870609e68fc607422a594c22834e255e7d6c60.tar.bz2 |
nested file-scoped binding environments
Diffstat (limited to 'eval_env.h')
-rw-r--r-- | eval_env.h | 29 |
1 files changed, 26 insertions, 3 deletions
@@ -1,8 +1,31 @@ +// A scope for variable lookups. +struct Env { + virtual string LookupVariable(const string& var) = 0; +}; + +// A standard scope, which contains a mapping of variables to values +// as well as a pointer to a parent scope. +struct BindingEnv : public Env { + virtual string LookupVariable(const string& var) { + map<string, string>::iterator i = bindings_.find(var); + if (i != bindings_.end()) + return i->second; + if (parent_) + return parent_->LookupVariable(var); + return ""; + } + void AddBinding(const string& key, const string& val) { + bindings_[key] = val; + } + + map<string, string> bindings_; + Env* parent_; +}; + +// A tokenized string that contains variable references. +// Can be evaluated relative to an Env. struct EvalString { - struct Env { - virtual string Evaluate(const string& var) = 0; - }; bool Parse(const string& input, string* err); string Evaluate(Env* env) const; |