void Function::reinit(const Eref &e, ProcPtr p) { if (!_valid){ cout << "Error: Function::reinit() - invalid parser state. Will do nothing." << endl; return; } if (trim(_parser.GetExpr(), " \t\n\r").length() == 0){ cout << "Error: no expression set. Will do nothing." << endl; setExpr(e, "0.0"); _valid = false; } _value = 0.0; _lastValue = 0.0; _rate = 0.0; switch (_mode){ case 1: { valueOut()->send(e, _value); break; } case 2: { derivativeOut()->send(e, 0.0); break; } case 3: { rateOut()->send(e, _rate); break; } default: { valueOut()->send(e, _value); derivativeOut()->send(e, 0.0); rateOut()->send(e, _rate); break; } } }
Function::Function(const Function& rhs): _numVar(rhs._numVar), _lastValue(rhs._lastValue), _value(rhs._value), _rate(rhs._rate), _mode(rhs._mode), _stoich(0) { static Eref er; _independent = rhs._independent; _parser.SetVarFactory(_functionAddVar, this); // Adding pi and e, the defaults are `_pi` and `_e` _parser.DefineConst(_T("pi"), (mu::value_type)M_PI); _parser.DefineConst(_T("e"), (mu::value_type)M_E); // Copy the constants mu::valmap_type cmap = rhs._parser.GetConst(); if (cmap.size()){ mu::valmap_type::const_iterator item = cmap.begin(); for (; item!=cmap.end(); ++item){ _parser.DefineConst(item->first, item->second); } } setExpr(er, rhs.getExpr( er )); // Copy the values from the var pointers in rhs assert(_varbuf.size() == rhs._varbuf.size()); for (unsigned int ii = 0; ii < rhs._varbuf.size(); ++ii){ _varbuf[ii]->value = rhs._varbuf[ii]->value; } assert(_pullbuf.size() == rhs._pullbuf.size()); for (unsigned int ii = 0; ii < rhs._pullbuf.size(); ++ii){ *_pullbuf[ii] = *(rhs._pullbuf[ii]); } }
Function& Function::operator=(const Function rhs) { static Eref er; _clearBuffer(); _mode = rhs._mode; _lastValue = rhs._lastValue; _value = rhs._value; _rate = rhs._rate; _independent = rhs._independent; // Adding pi and e, the defaults are `_pi` and `_e` _parser.DefineConst(_T("pi"), (mu::value_type)M_PI); _parser.DefineConst(_T("e"), (mu::value_type)M_E); // Copy the constants mu::valmap_type cmap = rhs._parser.GetConst(); if (cmap.size()){ mu::valmap_type::const_iterator item = cmap.begin(); for (; item!=cmap.end(); ++item){ _parser.DefineConst(item->first, item->second); } } // Copy the values from the var pointers in rhs setExpr(er, rhs.getExpr( er )); assert(_varbuf.size() == rhs._varbuf.size()); for (unsigned int ii = 0; ii < rhs._varbuf.size(); ++ii){ _varbuf[ii]->value = rhs._varbuf[ii]->value; } assert(_pullbuf.size() == rhs._pullbuf.size()); for (unsigned int ii = 0; ii < rhs._pullbuf.size(); ++ii){ *_pullbuf[ii] = *(rhs._pullbuf[ii]); } return *this; }
CDGNode *copyToPathNode(CDGNode * pathNode, CDGNode * node) { assert(NULL != pathNode); setID(pathNode, getID(node)); setExpr(pathNode, getExpr(node)); setOutcome(pathNode, getOutcome(node)); return pathNode; }
void Func::reinit(const Eref &e, ProcPtr p) { if (!_valid){ cout << "Error: Func::reinit() - invalid parser state. Will do nothing." << endl; return; } if (trim(_parser.GetExpr(), " \t\n\r").length() == 0){ cout << "Error: no expression set. Will do nothing." << endl; setExpr("0.0"); _valid = false; } }
Func::Func(const Func& rhs): _mode(rhs._mode) { _varbuf.reserve(VARMAX); _parser.SetVarFactory(_addVar, this); // Adding pi and e, the defaults are `_pi` and `_e` _parser.DefineConst(_T("pi"), (mu::value_type)M_PI); _parser.DefineConst(_T("e"), (mu::value_type)M_E); setExpr(rhs.getExpr()); vector <string> vars = rhs.getVars(); for (unsigned int ii = 0; ii < vars.size(); ++ii){ setVar(vars[ii], rhs.getVar(vars[ii])); } }
Func& Func::operator=(const Func rhs) { _clearBuffer(); _mode = rhs._mode; // Adding pi and e, the defaults are `_pi` and `_e` _parser.DefineConst(_T("pi"), (mu::value_type)M_PI); _parser.DefineConst(_T("e"), (mu::value_type)M_E); setExpr(rhs.getExpr()); vector <string> vars = rhs.getVars(); for (unsigned int ii = 0; ii < vars.size(); ++ii){ setVar(vars[ii], rhs.getVar(vars[ii])); } return *this; }
CDGNode *newNode(int id, int score, int outcome, const char *expr, CDGNode * trueNodeSet, CDGNode * falseNodeSet, CDGNode * parent, CDGNode * next) { CDGNode *node; node = (CDGNode *) malloc(sizeof(CDGNode)); assert(NULL != node); setID(node, id); setScore(node, score); setOutcome(node, outcome); setExpr(node, expr); setTrueNodeSet(node, trueNodeSet); setFalseNodeSet(node, falseNodeSet); setParent(node, parent); setNextNode(node, next); return node; }
FloatDatum::FloatDatum(QString name, QString expr, QObject *parent) : FloatDatum(name, parent) { setExpr(expr); }
void setArray(int id, const char *expr) { setExpr(nodes[id], expr); }
void OOOperatorDescriptorList::initializeWithDefaultOperators() { using OD = OOOperatorDescriptor; // Unary operators add(new OD("preincrement", "++ expr", 1, 2, OD::RightAssociative, OD::unary<UnaryOperation::PREINCREMENT>)); add(new OD("predecrement", "-- expr", 1, 2, OD::RightAssociative, OD::unary<UnaryOperation::PREDECREMENT>)); add(new OD("postincrement", "expr ++", 1, 2, OD::LeftAssociative, OD::unary<UnaryOperation::POSTINCREMENT>)); add(new OD("postdecrement", "expr --", 1, 2, OD::LeftAssociative, OD::unary<UnaryOperation::POSTDECREMENT>)); add(new OD("unary plus", "+ expr", 1, 2, OD::RightAssociative, OD::unary<UnaryOperation::PLUS>)); add(new OD("unary minus", "- expr", 1, 2, OD::RightAssociative, OD::unary<UnaryOperation::MINUS>)); add(new OD("not", "! expr", 1, 2, OD::RightAssociative, OD::unary<UnaryOperation::NOT>)); add(new OD("complement", "~ expr", 1, 2, OD::RightAssociative, OD::unary<UnaryOperation::COMPLEMENT>)); add(new OD("parenthesis", "( expr )", 1, 1, OD::NotAssociative, OD::unary<UnaryOperation::PARENTHESIS>)); add(new OD("dereference", "* expr", 1, 2, OD::RightAssociative, OD::unary<UnaryOperation::DEREFERENCE>)); add(new OD("addressof", "& expr", 1, 2, OD::RightAssociative, OD::unary<UnaryOperation::ADDRESSOF>)); // Binary operators add(new OD("times", "expr * expr", 2, 3, OD::LeftAssociative, OD::binary<BinaryOperation::TIMES>)); add(new OD("divide", "expr / expr", 2, 3, OD::LeftAssociative, OD::binary<BinaryOperation::DIVIDE>)); add(new OD("remainder", "expr % expr", 2, 3, OD::LeftAssociative, OD::binary<BinaryOperation::REMAINDER>)); add(new OD("plus", "expr + expr", 2, 4, OD::LeftAssociative, OD::binary<BinaryOperation::PLUS>)); add(new OD("minus", "expr - expr", 2, 4, OD::LeftAssociative, OD::binary<BinaryOperation::MINUS>)); add(new OD("left shift", "expr << expr", 2, 5, OD::LeftAssociative, OD::binary<BinaryOperation::LEFT_SHIFT>)); add(new OD("right shift signed", "expr >> expr", 2, 5, OD::LeftAssociative, OD::binary<BinaryOperation::RIGHT_SHIFT_SIGNED>)); add(new OD("righ shift unsigned", "expr >>> expr", 2, 5, OD::LeftAssociative, OD::binary<BinaryOperation::RIGHT_SHIFT_UNSIGNED>)); add(new OD("less", "expr < expr", 2, 6, OD::LeftAssociative, OD::binary<BinaryOperation::LESS>)); add(new OD("greater", "expr > expr", 2, 6, OD::LeftAssociative, OD::binary<BinaryOperation::GREATER>)); add(new OD("less equals", "expr <= expr", 2, 6, OD::LeftAssociative, OD::binary<BinaryOperation::LESS_EQUALS>)); add(new OD("greater equals", "expr >= expr", 2, 6, OD::LeftAssociative, OD::binary<BinaryOperation::GREATER_EQUALS>)); add(new OD("equals", "expr == expr", 2, 7, OD::LeftAssociative, OD::binary<BinaryOperation::EQUALS>)); add(new OD("not equals", "expr != expr", 2, 7, OD::LeftAssociative, OD::binary<BinaryOperation::NOT_EQUALS>)); add(new OD("bitwise xor", "expr ^ expr", 2, 9, OD::LeftAssociative, OD::binary<BinaryOperation::XOR>)); add(new OD("bitwise and", "expr & expr", 2, 8, OD::LeftAssociative, OD::binary<BinaryOperation::AND>)); add(new OD("bitwise or", "expr | expr", 2, 10, OD::LeftAssociative, OD::binary<BinaryOperation::OR>)); add(new OD("conditional and", "expr && expr", 2, 11, OD::LeftAssociative, OD::binary<BinaryOperation::CONDITIONAL_AND>)); add(new OD("conditional or", "expr || expr", 2, 12, OD::LeftAssociative, OD::binary<BinaryOperation::CONDITIONAL_OR>)); add(new OD("array index", "expr [ expr ]", 2, 1, OD::LeftAssociative, OD::binary<BinaryOperation::ARRAY_INDEX>)); // Ternary operator add(new OD("conditional expression", "expr ? expr : expr", 3, 13, OD::RightAssociative, [](const QList<Expression*>& operands) -> Expression* { auto opr = new ConditionalExpression(); opr->setCondition(operands.first()); opr->setTrueExpression(operands.at(1)); opr->setFalseExpression(operands.last()); return opr; })); // Assignment add(new OD("assign", "expr = expr", 2, 14, OD::RightAssociative, OD::assignment<AssignmentExpression::ASSIGN>)); add(new OD("assign", "expr += expr", 2, 14, OD::RightAssociative, OD::assignment<AssignmentExpression::PLUS_ASSIGN>)); add(new OD("assign", "expr -= expr", 2, 14, OD::RightAssociative, OD::assignment<AssignmentExpression::MINUS_ASSIGN>)); add(new OD("assign", "expr *= expr", 2, 14, OD::RightAssociative, OD::assignment<AssignmentExpression::TIMES_ASSIGN>)); add(new OD("assign", "expr /= expr", 2, 14, OD::RightAssociative, OD::assignment<AssignmentExpression::DIVIDE_ASSIGN>)); add(new OD("assign", "expr &= expr", 2, 14, OD::RightAssociative, OD::assignment<AssignmentExpression::BIT_AND_ASSIGN>)); add(new OD("assign", "expr |= expr", 2, 14, OD::RightAssociative, OD::assignment<AssignmentExpression::BIT_OR_ASSIGN>)); add(new OD("assign", "expr ^= expr", 2, 14, OD::RightAssociative, OD::assignment<AssignmentExpression::BIT_XOR_ASSIGN>)); add(new OD("assign", "expr %= expr", 2, 14, OD::RightAssociative, OD::assignment<AssignmentExpression::REMAINDER_ASSIGN>)); add(new OD("assign", "expr <<= expr", 2, 14, OD::RightAssociative, OD::assignment<AssignmentExpression::LEFT_SHIFT_ASSIGN>)); add(new OD("assign", "expr >>= expr", 2, 14, OD::RightAssociative, OD::assignment<AssignmentExpression::RIGHT_SHIFT_SIGNED_ASSIGN>)); add(new OD("assign", "expr >>>= expr", 2, 14, OD::RightAssociative, OD::assignment<AssignmentExpression::RIGHT_SHIFT_UNSIGNED_ASSIGN>)); // Others add(new OD( "cast", "( expr ) expr", 2, 2, OD::RightAssociative, [](const QList<Expression*>& operands) -> Expression* { auto opr = new OOModel::CastExpression(); opr->setType(operands.first()); opr->setExpr(operands.last()); return opr; })); add(new OD( "comma", "expr , expr", 2, 50, OD::LeftAssociative, [](const QList<Expression*>& operands) -> Expression* { auto opr = new OOModel::CommaExpression(); opr->setLeft(operands.first()); opr->setRight(operands.last()); return opr; })); add(new OD( "initializer", "{ expr }", 1, 0, OD::NotAssociative, [](const QList<Expression*>& operands) -> Expression* { auto opr = new OOModel::ArrayInitializer(); for(auto e: operands) { if (auto comma = dynamic_cast<OOModel::CommaExpression*>(e)) { for(auto ee : comma->allSubOperands(true)) opr->values()->append(ee); SAFE_DELETE(comma); } else opr->values()->append(e); } return opr; })); add(new OD( "new object", "new SPACE expr", 1, 2, OD::RightAssociative, [](const QList<Expression*>& operands) -> Expression* { auto opr = new OOModel::NewExpression(); opr->setNewType( operands.first()); return opr; })); add(new OD( "new array", "new SPACE expr [ expr ]", 2, 2, OD::RightAssociative, [](const QList<Expression*>& operands) -> Expression* { auto opr = new OOModel::NewExpression(); opr->setNewType( operands.first()); opr->setAmount(operands.last()); return opr; })); add(new OD( "delete object", "delete SPACE expr", 1, 2, OD::RightAssociative, [](const QList<Expression*>& operands) -> Expression* { auto expr = new OOModel::DeleteExpression(); expr->setExpr( operands.first()); return expr; })); add(new OD( "delete array", "delete [] SPACE expr", 1, 2, OD::RightAssociative, [](const QList<Expression*>& operands) -> Expression* { auto expr = new OOModel::DeleteExpression(); expr->setIsArray(true); expr->setExpr( operands.first()); return expr; })); add(new OD( "member", "expr . id", 2, 1, OD::LeftAssociative, [](const QList<Expression*>& operands) -> Expression* { Q_ASSERT(operands.size() == 2); auto ref = dynamic_cast<OOModel::ReferenceExpression*>( operands[1]); Q_ASSERT(ref); auto r = new OOModel::ReferenceExpression( ref->name(), operands.first() ); SAFE_DELETE(ref); return r; })); add(new OD( "member", "expr . id < expr >", 3, 1, OD::LeftAssociative, [](const QList<Expression*>& operands) -> Expression* { Q_ASSERT(operands.size() == 3 ); auto ref = dynamic_cast<OOModel::ReferenceExpression*>( operands[1]); Q_ASSERT(ref); auto r = new OOModel::ReferenceExpression( ref->name(), operands.first() ); SAFE_DELETE(ref); if (auto comma = dynamic_cast<OOModel::CommaExpression*>(operands.last())) { for(auto arg : comma->allSubOperands(true)) r->typeArguments()->append(arg); SAFE_DELETE(comma); } else if (!dynamic_cast<OOModel::EmptyExpression*>(operands.last()) ) r->typeArguments()->append(operands.last()); return r; })); add(new OD( "call", "expr ( expr )", 2, 1, OD::LeftAssociative, [](const QList<Expression*>& operands) -> Expression* { Q_ASSERT(operands.size() == 2); auto ref = dynamic_cast<OOModel::ReferenceExpression*>( operands.first()); Q_ASSERT(ref); auto opr = new OOModel::MethodCallExpression(); opr->setRef(ref); if (auto comma = dynamic_cast<OOModel::CommaExpression*>(operands.last())) { for(auto arg : comma->allSubOperands(true)) opr->arguments()->append(arg); SAFE_DELETE(comma); } else if (!dynamic_cast<OOModel::EmptyExpression*>(operands.last()) ) opr->arguments()->append(operands.last()); return opr; })); add(new OD( "type arguments", "id < expr >", 2, 0, OD::LeftAssociative, [](const QList<Expression*>& operands) -> Expression* { Q_ASSERT(operands.size() == 2); auto ref = dynamic_cast<OOModel::ReferenceExpression*>( operands.first()); Q_ASSERT(ref); if (auto comma = dynamic_cast<OOModel::CommaExpression*>(operands.last())) { for(auto arg : comma->allSubOperands(true)) ref->typeArguments()->append(arg); SAFE_DELETE(comma); } else if (!dynamic_cast<OOModel::EmptyExpression*>(operands.last()) ) ref->typeArguments()->append(operands.last()); return ref; })); add(new OD( "array type", "expr []", 1, 1, OD::LeftAssociative, [](const QList<Expression*>& operands) -> Expression* { auto at = new OOModel::ArrayTypeExpression(); at->setTypeExpression(operands.first()); return at; })); add(new OD( "pointer type", "expr *", 1, 1, OD::LeftAssociative, [](const QList<Expression*>& operands) -> Expression* { auto at = new OOModel::PointerTypeExpression(); at->setTypeExpression(operands.first()); return at; })); add(new OD( "reference type", "expr &", 1, 1, OD::LeftAssociative, [](const QList<Expression*>& operands) -> Expression* { auto at = new OOModel::ReferenceTypeExpression(); at->setTypeExpression(operands.first()); return at; })); add(new OD( "const qualifier", "const SPACE expr", 1, 1, OD::RightAssociative, [](const QList<Expression*>& operands) -> Expression* { auto at = new OOModel::TypeQualifierExpression(); at->setQualifier(OOModel::Type::CONST); at->setTypeExpression(operands.first()); return at; })); add(new OD( "volatile qualifier", "volatile SPACE expr", 1, 1, OD::RightAssociative, [](const QList<Expression*>& operands) -> Expression* { auto at = new OOModel::TypeQualifierExpression(); at->setQualifier(OOModel::Type::VOLATILE); at->setTypeExpression(operands.first()); return at; })); // Variable declaration auto varDeclFunction = [](const QList<Expression*>& operands) -> Expression* { auto vd = new OOModel::VariableDeclarationExpression(); vd->decl()->setTypeExpression( operands.first() ); auto ref = dynamic_cast<OOModel::ReferenceExpression*>(operands[1]); vd->decl()->setName( ref->name()); SAFE_DELETE(ref); if (operands.size() > 2) vd->decl()->setInitialValue(operands[2]); return vd; }; add(new OD( "variable decl", "expr SPACE id", 2, 40, OD::RightAssociative, varDeclFunction)); add(new OD( "variable decl and initialization", "expr SPACE id = expr", 3, 40, OD::RightAssociative, varDeclFunction)); add(new OD( "throw", "throw SPACE expr", 1, 30, OD::RightAssociative, [](const QList<Expression*>& operands) -> Expression* { auto expr = new OOModel::ThrowExpression(); expr->setExpr( operands.first()); return expr; })); // Command descriptors add(new CommandDescriptor( "command without params", "\\ id SPACE", 1, 0, OD::NotAssociative)); add(new CommandDescriptor( "command with params", "\\ id ( expr )", 2, 0, OD::NotAssociative)); add(new CompoundObjectDescriptor( "compound object", CompoundObjectDescriptor::compoundSignature(), 1, 0, OD::NotAssociative)); }