diff options
author | Marius Kintel <marius@kintel.net> | 2011-09-08 06:52:21 (GMT) |
---|---|---|
committer | Marius Kintel <marius@kintel.net> | 2011-09-08 06:52:21 (GMT) |
commit | 6659ad47b0b07b66b62ca79f3929d361733cc67a (patch) | |
tree | e4f0cbd7fa1e4b5dcb69daca43d3a7f33a1ed022 | |
parent | 5ce3d4ff8df274bbbb1b5dbd272179ad5765ded1 (diff) |
Updated boolean handling of if/else
-rw-r--r-- | RELEASE_NOTES | 1 | ||||
-rw-r--r-- | src/control.cc | 2 | ||||
-rw-r--r-- | src/value.cc | 24 | ||||
-rw-r--r-- | src/value.h | 2 |
4 files changed, 28 insertions, 1 deletions
diff --git a/RELEASE_NOTES b/RELEASE_NOTES index 7b9a000..97af740 100644 --- a/RELEASE_NOTES +++ b/RELEASE_NOTES @@ -8,6 +8,7 @@ o New import() statement reads the correct file format based on the filename ext (.stl, .dxf and .off is supported) o The color() statement now supports an alpha parameter, e.g. color(c=[1,0,0], alpha=0.4) o The color() statement now supports specifying colors as strings, e.g. color("Red") +o if() and else() can now take any value type as parameter. false, 0, empty string and empty vector or illegal value type will evaluate as false, everything else as true. Bugfixes: o square() crashed if any of the dimensions were zero diff --git a/src/control.cc b/src/control.cc index c39b74a..492b909 100644 --- a/src/control.cc +++ b/src/control.cc @@ -149,7 +149,7 @@ AbstractNode *ControlModule::evaluate(const Context*, const ModuleInstantiation if (type == IF) { const IfElseModuleInstantiation *ifelse = dynamic_cast<const IfElseModuleInstantiation*>(inst); - if (ifelse->argvalues.size() > 0 && ifelse->argvalues[0].type == Value::BOOL && ifelse->argvalues[0].b) { + if (ifelse->argvalues.size() > 0 && ifelse->argvalues[0].toBool()) { std::vector<AbstractNode *> evaluatednodes = ifelse->evaluateChildren(); node->children.insert(node->children.end(), evaluatednodes.begin(), evaluatednodes.end()); } diff --git a/src/value.cc b/src/value.cc index 34566bd..53fd6dc 100644 --- a/src/value.cc +++ b/src/value.cc @@ -361,6 +361,30 @@ std::string Value::toString() const return stream.str(); } +bool Value::toBool() const +{ + switch (this->type) { + case STRING: + return this->text.size() > 0; + break; + case VECTOR: + return this->vec.size() > 0; + break; + case RANGE: + return true; + break; + case NUMBER: + return this->num != 0; + break; + case BOOL: + return this->b; + break; + default: + return false; + break; + } +} + /*! Append a value to this vector. This must be of type VECTOR. diff --git a/src/value.h b/src/value.h index 2003460..fb6500c 100644 --- a/src/value.h +++ b/src/value.h @@ -61,6 +61,8 @@ public: std::string toString() const; + bool toBool() const; + void append(Value *val); private: |