diff options
author | clifford <clifford@b57f626f-c46c-0410-a088-ec61d464b74c> | 2009-06-30 08:38:31 (GMT) |
---|---|---|
committer | clifford <clifford@b57f626f-c46c-0410-a088-ec61d464b74c> | 2009-06-30 08:38:31 (GMT) |
commit | a70715ab8c31160f1be2a74d208681c2ad422bbb (patch) | |
tree | 51e09a6b66aae4d6a4a59e588b8429dea38ab448 | |
parent | ed687599bf1ca36869dc64dab2b9969f1d96b4c1 (diff) |
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
-rw-r--r-- | context.cc | 6 | ||||
-rw-r--r-- | control.cc | 37 | ||||
-rw-r--r-- | openscad.pro | 2 |
3 files changed, 39 insertions, 6 deletions
@@ -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<QString> &argnames, const QVector<Value> &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<QString> &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); @@ -36,13 +36,37 @@ public: virtual AbstractNode *evaluate(const Context *ctx, const QVector<QString> &call_argnames, const QVector<Value> &call_argvalues, const QVector<ModuleInstanciation*> arg_children, const Context *arg_context) const; }; +void for_eval(AbstractNode *node, int l, const QVector<QString> &call_argnames, const QVector<Value> &call_argvalues, const QVector<ModuleInstanciation*> 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<QString> &call_argnames, const QVector<Value> &call_argvalues, const QVector<ModuleInstanciation*> 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<QString> &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<QString> &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 |