diff options
author | Marius Kintel <marius@kintel.net> | 2013-04-19 21:52:01 (GMT) |
---|---|---|
committer | Marius Kintel <marius@kintel.net> | 2013-04-26 21:45:03 (GMT) |
commit | 9a297ecee57549a4eb3919bde4f7b41a548724de (patch) | |
tree | b1f523e93f528559e9ada2d38d76be7a0362f9d4 /src/modcontext.cc | |
parent | b4568a09df8be0f212ba1c4fd1660459900e3cf0 (diff) |
Refactoring: Split out FileModule from Module, Added LocalScope renamed some confusing 'evaluate' methods to 'instantiate', added FileContext (not yet used)
Diffstat (limited to 'src/modcontext.cc')
-rw-r--r-- | src/modcontext.cc | 55 |
1 files changed, 31 insertions, 24 deletions
diff --git a/src/modcontext.cc b/src/modcontext.cc index 83ad7c2..df341f9 100644 --- a/src/modcontext.cc +++ b/src/modcontext.cc @@ -8,16 +8,9 @@ #include <boost/foreach.hpp> ModuleContext::ModuleContext(const class Module *module, const Context *parent, const EvalContext *evalctx) - : Context(parent) + : Context(parent), functions_p(NULL), modules_p(NULL), usedlibs_p(NULL) { - if (module) { - setModule(*module, evalctx); - } - else { - this->functions_p = NULL; - this->modules_p = NULL; - this->usedlibs_p = NULL; - } + if (module) setModule(*module, evalctx); } ModuleContext::~ModuleContext() @@ -29,15 +22,19 @@ void ModuleContext::setModule(const Module &module, const EvalContext *evalctx) this->setVariables(module.definition_arguments, evalctx); this->evalctx = evalctx; + // FIXME: Hack - split out file context into separate class? + const FileModule *fm = dynamic_cast<const FileModule*>(&module); + if (fm) { + this->usedlibs_p = &(fm->usedlibs); + if (!fm->modulePath().empty()) this->document_path = fm->modulePath(); + } + // FIXME: Don't access module members directly - this->functions_p = &module.functions; - this->modules_p = &module.modules; - this->usedlibs_p = &module.usedlibs; - BOOST_FOREACH(const Assignment &ass, module.assignments) { + this->functions_p = &module.scope.functions; + this->modules_p = &module.scope.modules; + BOOST_FOREACH(const Assignment &ass, module.scope.assignments) { this->set_variable(ass.first, ass.second->evaluate(this)); } - - if (!module.modulePath().empty()) this->document_path = module.modulePath(); } /*! @@ -45,6 +42,8 @@ void ModuleContext::setModule(const Module &module, const EvalContext *evalctx) */ void ModuleContext::registerBuiltin() { + // FIXME: built-ins only contains variables, setModule isn't really needed for this + // FIXME: Where to put set_variable from setModule? this->setModule(Builtins::instance()->getRootModule()); this->set_constant("PI",Value(M_PI)); } @@ -75,22 +74,22 @@ Value ModuleContext::evaluate_function(const std::string &name, const EvalContex } if (this->usedlibs_p) { - BOOST_FOREACH(const ModuleContainer::value_type &m, *this->usedlibs_p) { - if (m.second->functions.find(name) != m.second->functions.end()) { + BOOST_FOREACH(const FileModule::ModuleContainer::value_type &m, *this->usedlibs_p) { + if (m.second->scope.functions.find(name) != m.second->scope.functions.end()) { ModuleContext ctx(m.second, this->parent); // FIXME: Set document path #if 0 && DEBUG PRINTB("New lib Context for %s func:", name); ctx.dump(NULL, NULL); #endif - return m.second->functions[name]->evaluate(&ctx, evalctx); + return m.second->scope.functions[name]->evaluate(&ctx, evalctx); } } } return Context::evaluate_function(name, evalctx); } -AbstractNode *ModuleContext::evaluate_module(const ModuleInstantiation &inst, const EvalContext *evalctx) const +AbstractNode *ModuleContext::instantiate_module(const ModuleInstantiation &inst, const EvalContext *evalctx) const { if (this->modules_p && this->modules_p->find(inst.name()) != this->modules_p->end()) { AbstractModule *m = this->modules_p->find(inst.name())->second; @@ -98,25 +97,25 @@ AbstractNode *ModuleContext::evaluate_module(const ModuleInstantiation &inst, co if (!replacement.empty()) { PRINTB("DEPRECATED: The %s() module will be removed in future releases. Use %s() instead.", inst.name() % replacement); } - return m->evaluate(this, &inst, evalctx); + return m->instantiate(this, &inst, evalctx); } if (this->usedlibs_p) { - BOOST_FOREACH(const ModuleContainer::value_type &m, *this->usedlibs_p) { + BOOST_FOREACH(const FileModule::ModuleContainer::value_type &m, *this->usedlibs_p) { assert(m.second); - if (m.second->modules.find(inst.name()) != m.second->modules.end()) { + if (m.second->scope.modules.find(inst.name()) != m.second->scope.modules.end()) { ModuleContext ctx(m.second, this->parent); // FIXME: Set document path #if 0 && DEBUG PRINT("New lib Context:"); ctx.dump(NULL, &inst); #endif - return m.second->modules[inst.name()]->evaluate(&ctx, &inst, evalctx); + return m.second->scope.modules[inst.name()]->instantiate(&ctx, &inst, evalctx); } } } - return Context::evaluate_module(inst, evalctx); + return Context::instantiate_module(inst, evalctx); } #ifdef DEBUG @@ -150,3 +149,11 @@ void ModuleContext::dump(const AbstractModule *mod, const ModuleInstantiation *i } #endif + +FileContext::FileContext(const class FileModule &module, + const Context *parent, const EvalContext *evalctx) + :ModuleContext(&module, parent, evalctx) +{ + +} + |