double Formulaeditor::expression(int& nPosition, QString& strCharacter) { QString strOperator; double erg = simple_expression(nPosition, strCharacter); while (strCharacter == "+" || strCharacter == "-") { strOperator = strCharacter; char_n(nPosition, strCharacter); if (strOperator == "+") erg += simple_expression(nPosition, strCharacter); else if (strOperator == "-") erg -= simple_expression(nPosition, strCharacter); } return erg; }
std::list<SimpleExpression> Parser::simple_expression_list(){ std::list<SimpleExpression> se; if (lexer->getToken() == TOK_AND){ match(TOK_AND, "expression_list_aux"); se.push_back(simple_expression()); std::list<SimpleExpression> exprlist (simple_expression_list()); /* copy exprlist into se as merge requires operator< or something */ for (std::list<SimpleExpression>::iterator i = exprlist.begin(); i != exprlist.end(); ++i) se.push_back(*i); } return se; }
/* expression -> var "=" expression | simple-expression * var -> ID ["["expression"]"]*/ static TreeNode * expression (void) { TreeNode * t = newExpNode(IdK); TreeNode * p = NULL; TreeNode * newNode = NULL; #define CALL 1 #define ARRAY 2 int factorType = 0; while (token == COMMENT) unexpectedTokenHandling(); if (token == ID) { if (t != NULL) t->attr.name = copyString(tokenString); match (ID); /* var -> ID "["expression"]" */ if (token == SLPAREN) { factorType = ARRAY; match (SLPAREN); t->child[0] = expression(); match (SRPAREN); /* ID "["expression"]" "=" expression */ if (token == ASSIGN) { match (ASSIGN); newNode = newStmtNode(AssignK); if (t != NULL) { newNode->child[0] = t; t= newNode; t->child[1] = expression(); if (token != SEMI) unexpectedTokenHandling(); } } } /* simple-expression => call => ID "("args")" */ else if (token == LPAREN) { factorType = CALL; match (LPAREN); if (t != NULL) { t->nodekind = ExpK; t->kind.exp = CallK; t->child[0] = args(); } match (RPAREN); } /* var | call is followed by assignment, operation, * semi-colon or comma. */ while (token == COMMENT) unexpectedTokenHandling(); if (token == ASSIGN) { if ((factorType == CALL) || (factorType == ARRAY)) unexpectedTokenHandling(); /* ID "=" expression */ else { match (ASSIGN); newNode = newStmtNode(AssignK); if (t != NULL) { newNode->child[0] = t; t= newNode; t->child[1] = expression(); if (token != SEMI) unexpectedTokenHandling(); } } } /* ID [("("args")"|"["expression"]")] * op additivie-expression */ else if (isOp (token)) { newNode = newExpNode (OpK); if (newNode != NULL) { newNode->child[0] = t; newNode->attr.op = token; t = newNode; } match (token); if (t != NULL) t->child[1] = additive_expression(); } else if ((token != SEMI) && (token != COMMA) && (token != RPAREN)) unexpectedTokenHandling(); } else if ((token == LPAREN) || (token == NUM)) t = simple_expression(); else unexpectedTokenHandling(); return t; }
Expression* Parser::expression(){ Expression* e = new Expression(); e->addSimpleExpression( simple_expression() ); e->addSimpleExpression( simple_expression_list()); return e; }