From a70715ab8c31160f1be2a74d208681c2ad422bbb Mon Sep 17 00:00:00 2001 From: clifford Date: Tue, 30 Jun 2009 08:38:31 +0000 Subject: Clifford Wolf: Added assign() and if() Added first for() outline git-svn-id: http://svn.clifford.at/openscad/trunk@38 b57f626f-c46c-0410-a088-ec61d464b74c diff --git a/context.cc b/context.cc index 3a257c6..a7b537d 100644 --- a/context.cc +++ b/context.cc @@ -23,6 +23,8 @@ Context::Context(const Context *parent) { this->parent = parent; + functions_p = NULL; + modules_p = NULL; ctx_stack.append(this); } @@ -77,7 +79,7 @@ Value Context::lookup_variable(QString name) const Value Context::evaluate_function(QString name, const QVector &argnames, const QVector &argvalues) const { - if (functions_p->contains(name)) + if (functions_p && functions_p->contains(name)) return functions_p->value(name)->evaluate(this, argnames, argvalues); if (parent) return parent->evaluate_function(name, argnames, argvalues); @@ -89,7 +91,7 @@ AbstractNode *Context::evaluate_module(QString name, const QVector &arg { if (arg_context == NULL) arg_context = this; - if (modules_p->contains(name)) + if (modules_p && modules_p->contains(name)) return modules_p->value(name)->evaluate(this, argnames, argvalues, arg_children, arg_context); if (parent) return parent->evaluate_module(name, argnames, argvalues, arg_children, arg_context); diff --git a/control.cc b/control.cc index 82fa6bf..bfada90 100644 --- a/control.cc +++ b/control.cc @@ -36,13 +36,37 @@ public: virtual AbstractNode *evaluate(const Context *ctx, const QVector &call_argnames, const QVector &call_argvalues, const QVector arg_children, const Context *arg_context) const; }; +void for_eval(AbstractNode *node, int l, const QVector &call_argnames, const QVector &call_argvalues, const QVector arg_children, const Context *arg_context) +{ + if (call_argnames.size() > l) { + QString it_name = call_argnames[l]; + Value it_values = call_argvalues[l]; + Context c(arg_context); + if (it_values.type == Value::RANGE) { + for (double i = it_values.r_begin; i <= it_values.r_end; i += it_values.r_step) { + fprintf(stderr, "%f\n", i); + c.set_variable(it_name, Value(i)); + for_eval(node, l+1, call_argnames, call_argvalues, arg_children, &c); + } + } + else { + for_eval(node, l+1, call_argnames, call_argvalues, arg_children, &c); + } + } else { + foreach (ModuleInstanciation *v, arg_children) { + AbstractNode *n = v->evaluate(arg_context); + if (n != NULL) + node->children.append(n); + } + } +} + AbstractNode *ControlModule::evaluate(const Context*, const QVector &call_argnames, const QVector &call_argvalues, const QVector arg_children, const Context *arg_context) const { AbstractNode *node = new AbstractNode(); if (type == ASSIGN) { - /* FIXME */ Context c(arg_context); for (int i = 0; i < call_argnames.size(); i++) { if (!call_argnames[i].isEmpty()) @@ -57,10 +81,17 @@ AbstractNode *ControlModule::evaluate(const Context*, const QVector &ca if (type == FOR) { + for_eval(node, 0, call_argnames, call_argvalues, arg_children, arg_context); } if (type == IF) { + if (call_argvalues.size() > 0 && call_argvalues[0].type == Value::BOOL && call_argvalues[0].b) + foreach (ModuleInstanciation *v, arg_children) { + AbstractNode *n = v->evaluate(arg_context); + if (n != NULL) + node->children.append(n); + } } return node; @@ -68,8 +99,8 @@ AbstractNode *ControlModule::evaluate(const Context*, const QVector &ca void register_builtin_control() { - // builtin_modules["assign"] = new ControlModule(ASSIGN); + builtin_modules["assign"] = new ControlModule(ASSIGN); // builtin_modules["for"] = new ControlModule(FOR); - // builtin_modules["if"] = new ControlModule(IF); + builtin_modules["if"] = new ControlModule(IF); } diff --git a/openscad.pro b/openscad.pro index 0b0f6f1..1ec32fa 100644 --- a/openscad.pro +++ b/openscad.pro @@ -15,7 +15,7 @@ HEADERS += openscad.h SOURCES += openscad.cc mainwin.cc glview.cc SOURCES += value.cc expr.cc func.cc module.cc context.cc SOURCES += csgterm.cc polyset.cc csgops.cc transform.cc -SOURCES += primitives.cc +SOURCES += primitives.cc control.cc QT += opengl -- cgit v0.10.1