diff options
author | clifford <clifford@b57f626f-c46c-0410-a088-ec61d464b74c> | 2009-07-01 08:06:06 (GMT) |
---|---|---|
committer | clifford <clifford@b57f626f-c46c-0410-a088-ec61d464b74c> | 2009-07-01 08:06:06 (GMT) |
commit | 27ecd0b1d0bf0114b465beeb4db482f0f1f87c52 (patch) | |
tree | fbf5c869321f15b75b6f5c1c2299b5f5fd960e18 /expr.cc | |
parent | b7ca4bfdb38b8caf153f6d93b1d3959f8ffd53ca (diff) |
Clifford Wolf:
Another cleanup in expression handling
git-svn-id: http://svn.clifford.at/openscad/trunk@43 b57f626f-c46c-0410-a088-ec61d464b74c
Diffstat (limited to 'expr.cc')
-rw-r--r-- | expr.cc | 197 |
1 files changed, 105 insertions, 92 deletions
@@ -22,7 +22,6 @@ Expression::Expression() { - type = 0; const_value = NULL; } @@ -36,124 +35,138 @@ Expression::~Expression() Value Expression::evaluate(const Context *context) const { - switch (type) - { - case '*': + if (type == "!") + return ! children[0]->evaluate(context); + if (type == "&&") + return children[0]->evaluate(context) && children[1]->evaluate(context); + if (type == "||") + return children[0]->evaluate(context) || children[1]->evaluate(context); + if (type == "*") return children[0]->evaluate(context) * children[1]->evaluate(context); - case '/': + if (type == "/") return children[0]->evaluate(context) / children[1]->evaluate(context); - case '%': + if (type == "%") return children[0]->evaluate(context) % children[1]->evaluate(context); - case '+': + if (type == "+") return children[0]->evaluate(context) + children[1]->evaluate(context); - case '-': + if (type == "-") return children[0]->evaluate(context) - children[1]->evaluate(context); - case '?': - { - Value v = children[0]->evaluate(context); - if (v.type == Value::BOOL) - return children[v.b ? 1 : 2]->evaluate(context); - return Value(); + if (type == "<") + return children[0]->evaluate(context) < children[1]->evaluate(context); + if (type == "<=") + return children[0]->evaluate(context) <= children[1]->evaluate(context); + if (type == "==") + return children[0]->evaluate(context) == children[1]->evaluate(context); + if (type == "!=") + return children[0]->evaluate(context) != children[1]->evaluate(context); + if (type == ">=") + return children[0]->evaluate(context) >= children[1]->evaluate(context); + if (type == ">") + return children[0]->evaluate(context) > children[1]->evaluate(context); + if (type == "?:") { + Value v = children[0]->evaluate(context); + if (v.type == Value::BOOL) + return children[v.b ? 1 : 2]->evaluate(context); + return Value(); + } + if (type == "[]") { + Value v1 = children[0]->evaluate(context); + Value v2 = children[1]->evaluate(context); + if (v1.type == Value::VECTOR && v2.type == Value::NUMBER) { + int i = v2.num; + if (i < v1.vec.size()) + return *v1.vec[i]; } - case 'I': + return Value(); + } + if (type == "I") return children[0]->evaluate(context).inv(); - case 'C': + if (type == "C") return *const_value; - case 'R': - { - Value v1 = children[0]->evaluate(context); - Value v2 = children[1]->evaluate(context); - Value v3 = children[2]->evaluate(context); - if (v1.type == Value::NUMBER && v2.type == Value::NUMBER && v3.type == Value::NUMBER) { - Value r = Value(); - r.type = Value::RANGE; - r.range_begin = v1.num; - r.range_step = v2.num; - r.range_end = v3.num; - return r; - } - return Value(); + if (type == "R") { + Value v1 = children[0]->evaluate(context); + Value v2 = children[1]->evaluate(context); + Value v3 = children[2]->evaluate(context); + if (v1.type == Value::NUMBER && v2.type == Value::NUMBER && v3.type == Value::NUMBER) { + Value r = Value(); + r.type = Value::RANGE; + r.range_begin = v1.num; + r.range_step = v2.num; + r.range_end = v3.num; + return r; } - case 'V': - { - Value v; - v.type = Value::VECTOR; - for (int i = 0; i < children.size(); i++) - v.vec.append(new Value(children[i]->evaluate(context))); - return v; - } - case 'L': + return Value(); + } + if (type == "V") { + Value v; + v.type = Value::VECTOR; + for (int i = 0; i < children.size(); i++) + v.vec.append(new Value(children[i]->evaluate(context))); + return v; + } + if (type == "L") return context->lookup_variable(var_name); - case 'N': - { - Value v = children[0]->evaluate(context); + if (type == "N") + { + Value v = children[0]->evaluate(context); - if (v.type == Value::VECTOR && var_name == QString("x")) - return *v.vec[0]; - if (v.type == Value::VECTOR && var_name == QString("y")) - return *v.vec[1]; - if (v.type == Value::VECTOR && var_name == QString("z")) - return *v.vec[2]; + if (v.type == Value::VECTOR && var_name == QString("x")) + return *v.vec[0]; + if (v.type == Value::VECTOR && var_name == QString("y")) + return *v.vec[1]; + if (v.type == Value::VECTOR && var_name == QString("z")) + return *v.vec[2]; - if (v.type == Value::RANGE && var_name == QString("begin")) - return Value(v.range_begin); - if (v.type == Value::RANGE && var_name == QString("step")) - return Value(v.range_step); - if (v.type == Value::RANGE && var_name == QString("end")) - return Value(v.range_end); + if (v.type == Value::RANGE && var_name == QString("begin")) + return Value(v.range_begin); + if (v.type == Value::RANGE && var_name == QString("step")) + return Value(v.range_step); + if (v.type == Value::RANGE && var_name == QString("end")) + return Value(v.range_end); - return Value(); - } - case 'F': - { - QVector<Value> argvalues; - for (int i=0; i < children.size(); i++) - argvalues.append(children[i]->evaluate(context)); - return context->evaluate_function(call_funcname, call_argnames, argvalues); - } - default: - abort(); + return Value(); } + if (type == "F") { + QVector<Value> argvalues; + for (int i=0; i < children.size(); i++) + argvalues.append(children[i]->evaluate(context)); + return context->evaluate_function(call_funcname, call_argnames, argvalues); + } + abort(); } QString Expression::dump() const { - switch (type) - { - case '*': - case '/': - case '%': - case '+': - case '-': + if (type == "*" || type == "/" || type == "%" || type == "+" || type == "-" || + type == "<" || type == "<=" || type == "==" || type == "!=" || type == ">=" || type == ">") return QString("(%1 %2 %3)").arg(children[0]->dump(), QString(type), children[1]->dump()); - case '?': + if (type == "?:") return QString("(%1 ? %2 : %3)").arg(children[0]->dump(), children[1]->dump(), children[2]->dump()); - case 'I': + if (type == "[]") + return QString("(%1[%2])").arg(children[0]->dump(), children[1]->dump()); + if (type == "I") return QString("(-%1)").arg(children[0]->dump()); - case 'C': + if (type == "C") return const_value->dump(); - case 'R': + if (type == "R") return QString("[%1 : %2 : %3]").arg(children[0]->dump(), children[1]->dump(), children[2]->dump()); - case 'V': + if (type == "V") return QString("[%1, %2, %3]").arg(children[0]->dump(), children[1]->dump(), children[2]->dump()); - case 'L': + if (type == "L") return var_name; - case 'N': + if (type == "N") return QString("(%1.%2)").arg(children[0]->dump(), var_name); - case 'F': - { - QString text = call_funcname + QString("("); - for (int i=0; i < children.size(); i++) { - if (i > 0) - text += QString(", "); - if (!call_argnames[i].isEmpty()) - text += call_argnames[i] + QString(" = "); - text += children[i]->dump(); - } - return text + QString(")"); + if (type == "F") { + QString text = call_funcname + QString("("); + for (int i=0; i < children.size(); i++) { + if (i > 0) + text += QString(", "); + if (!call_argnames[i].isEmpty()) + text += call_argnames[i] + QString(" = "); + text += children[i]->dump(); } - default: - abort(); + return text + QString(")"); } + abort(); } |