From b9007109a1b6044f5b3239ccacc10946c94c46f4 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Sun, 16 Oct 2016 10:41:59 +0200 Subject: Add notion of scope to testscript model --- build2/test/script/parser | 1 + build2/test/script/parser.cxx | 7 ++++--- build2/test/script/script | 27 +++++++++++++++++++++------ build2/test/script/script.cxx | 23 ++++++++++++++++------- 4 files changed, 42 insertions(+), 16 deletions(-) (limited to 'build2') diff --git a/build2/test/script/parser b/build2/test/script/parser index 732b664..05b4d47 100644 --- a/build2/test/script/parser +++ b/build2/test/script/parser @@ -69,6 +69,7 @@ namespace build2 lexer* lexer_; script* script_; runner* runner_; + scope* scope_; }; } } diff --git a/build2/test/script/parser.cxx b/build2/test/script/parser.cxx index 57068c6..7d234eb 100644 --- a/build2/test/script/parser.cxx +++ b/build2/test/script/parser.cxx @@ -28,6 +28,7 @@ namespace build2 script_ = &s; runner_ = &r; + scope_ = script_; token t; type tt; @@ -118,8 +119,8 @@ namespace build2 fail (t) << "unexpected " << t; value& lhs (kind == type::assign - ? script_->assign (var) - : script_->append (var)); + ? scope_->assign (var) + : scope_->append (var)); // @@ Need to adjust to make strings the default type. // @@ -604,7 +605,7 @@ namespace build2 fail (l) << "qualified variable name"; const variable& var (script_->var_pool.insert (move (name))); - return script_->find (var); + return scope_->find (var); } } } diff --git a/build2/test/script/script b/build2/test/script/script index f5e5ef0..005d6e5 100644 --- a/build2/test/script/script +++ b/build2/test/script/script @@ -57,20 +57,22 @@ namespace build2 command_exit exit; }; - class script + class scope { public: - script (target& test_target, target& script_target); + scope* parent; // NULL for the root (script) scope. - public: - target& test_target; // Target we are testing. - target& script_target; // Target of the testscript file. + scope (scope& p): parent (&p) {} + + protected: + scope (): parent (nullptr) {} // For the root (script) scope. + // Variables. + // public: // Note that if we pass the variable name as a string, then it will // be looked up in the wrong pool. // - variable_pool var_pool; variable_map vars; // Lookup the variable starting from this scope, continuing with outer @@ -97,6 +99,19 @@ namespace build2 value& append (const variable&); }; + + class script: public scope + { + public: + script (target& test_target, target& script_target); + + public: + target& test_target; // Target we are testing. + target& script_target; // Target of the testscript file. + + public: + variable_pool var_pool; + }; } } } diff --git a/build2/test/script/script.cxx b/build2/test/script/script.cxx index 1b47846..b206e4f 100644 --- a/build2/test/script/script.cxx +++ b/build2/test/script/script.cxx @@ -43,11 +43,19 @@ namespace build2 } } - lookup script:: + lookup scope:: find (const variable& var) const { - if (const value* v = vars.find (var)) - return lookup (v, &vars); + // Search script scopes until we hit the root. + // + const scope* p (this); + + do + { + if (const value* v = p->vars.find (var)) + return lookup (v, &p->vars); + } + while (p->parent != nullptr ? (p = p->parent) : nullptr); // Switch to the corresponding buildfile variable. Note that we don't // want to insert a new variable into the pool (we might be running @@ -59,6 +67,7 @@ namespace build2 if (pvar == nullptr) return lookup (); + const script& s (static_cast (*p)); { const variable& var (*pvar); @@ -69,12 +78,12 @@ namespace build2 // value. In this case, presumably the override also affects the // script target and we will pick it up there. A bit fuzzy. // - auto p (test_target.find_original (var, true)); + auto p (s.test_target.find_original (var, true)); if (p.first) { if (var.override != nullptr) - p = test_target.base_scope ().find_override ( + p = s.test_target.base_scope ().find_override ( var, move (p), true); return p.first; @@ -86,11 +95,11 @@ namespace build2 // in different scopes which brings the question of which scopes we // should search. // - return script_target[var]; + return s.script_target[var]; } } - value& script:: + value& scope:: append (const variable& var) { lookup l (find (var)); -- cgit v1.1