summaryrefslogtreecommitdiffstats
path: root/eval_env.h
diff options
context:
space:
mode:
authorEvan Martin <martine@danga.com>2010-11-14 19:08:15 (GMT)
committerEvan Martin <martine@danga.com>2010-11-14 19:08:21 (GMT)
commitb6870609e68fc607422a594c22834e255e7d6c60 (patch)
treea15d73e0d0e42011d22676af73df4b9e38a4fa32 /eval_env.h
parenta160b2628499f63f103a0deb39ee8670627bde32 (diff)
downloadNinja-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.h29
1 files changed, 26 insertions, 3 deletions
diff --git a/eval_env.h b/eval_env.h
index 2d1a79e..5d51aa6 100644
--- a/eval_env.h
+++ b/eval_env.h
@@ -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;