summaryrefslogtreecommitdiff
path: root/src/localscope.cc
diff options
context:
space:
mode:
authorMarius Kintel <marius@kintel.net>2013-04-24 13:17:25 (GMT)
committerMarius Kintel <marius@kintel.net>2013-04-26 21:45:03 (GMT)
commit9b740b558dd627412e01a3521d5372d95e294af2 (patch)
treef34ebfa719303629e27d5fe84c177b5ac38adfbe /src/localscope.cc
parent9a297ecee57549a4eb3919bde4f7b41a548724de (diff)
Further refactoring of scope/context mechanisms. Mostly related to the new FileContext class. Not quite there yet, but almost
Diffstat (limited to 'src/localscope.cc')
-rw-r--r--src/localscope.cc67
1 files changed, 67 insertions, 0 deletions
diff --git a/src/localscope.cc b/src/localscope.cc
new file mode 100644
index 0000000..c4001f5
--- /dev/null
+++ b/src/localscope.cc
@@ -0,0 +1,67 @@
+#include "localscope.h"
+#include "modcontext.h"
+#include "module.h"
+#include "typedefs.h"
+#include "expression.h"
+#include "function.h"
+
+#include <boost/foreach.hpp>
+
+LocalScope::LocalScope()
+{
+}
+
+LocalScope::~LocalScope()
+{
+ BOOST_FOREACH (ModuleInstantiation *v, children) delete v;
+ BOOST_FOREACH (const Assignment &v, assignments) delete v.second;
+ BOOST_FOREACH (FunctionContainer::value_type &f, functions) delete f.second;
+ BOOST_FOREACH (AbstractModuleContainer::value_type &m, modules) delete m.second;
+}
+
+std::string LocalScope::dump(const std::string &indent) const
+{
+ std::stringstream dump;
+ BOOST_FOREACH(const FunctionContainer::value_type &f, this->functions) {
+ dump << f.second->dump(indent, f.first);
+ }
+ BOOST_FOREACH(const AbstractModuleContainer::value_type &m, this->modules) {
+ dump << m.second->dump(indent, m.first);
+ }
+ BOOST_FOREACH(const Assignment &ass, this->assignments) {
+ dump << indent << ass.first << " = " << *ass.second << ";\n";
+ }
+ BOOST_FOREACH(const ModuleInstantiation *inst, this->children) {
+ dump << inst->dump(indent);
+ }
+ return dump.str();
+}
+
+// FIXME: Two parameters here is a hack. Rather have separate types of scopes, or check the type of the first parameter. Note const vs. non-const
+std::vector<AbstractNode*> LocalScope::instantiateChildren(const Context *evalctx, FileContext *filectx) const
+{
+ Context *c = filectx;
+
+ if (!c) {
+ c = new Context(evalctx);
+
+ // FIXME: If we make c a ModuleContext, child() doesn't work anymore
+ // c->functions_p = &this->functions;
+ // c->modules_p = &this->modules;
+
+ BOOST_FOREACH (const Assignment &ass, this->assignments) {
+ c->set_variable(ass.first, ass.second->evaluate(c));
+ }
+ }
+
+ std::vector<AbstractNode*> childnodes;
+ BOOST_FOREACH (ModuleInstantiation *modinst, this->children) {
+ AbstractNode *node = modinst->evaluate(c);
+ if (node) childnodes.push_back(node);
+ }
+
+ if (c != filectx) delete c;
+
+ return childnodes;
+}
+
contact: Jan Huwald // Impressum