diff options
Diffstat (limited to 'mtxexpression.cc')
-rw-r--r-- | mtxexpression.cc | 105 |
1 files changed, 91 insertions, 14 deletions
diff --git a/mtxexpression.cc b/mtxexpression.cc index 72dc761..26e341a 100644 --- a/mtxexpression.cc +++ b/mtxexpression.cc @@ -32,14 +32,19 @@ using namespace std; -MtxExpression::MtxExpression() +struct mtx_operator MtxExpression::operators[] = { -} + {"=", MTXEXPRESSION_FUNCTION_ASSIGNMENT, MTXEXPRESSION_RIGHT_TO_LEFT}, + {NULL, MTXEXPRESSION_FUNCTION_NULL, MTXEXPRESSION_ASSOCIATIVITY_NULL}, +}; MtxExpression::MtxExpression(MtxLanguage *language, mtx_expression_t &lexical_elements, bool silent) { + if (lexical_elements.empty()) + throw MtxException("syntax error"); + /* * See mtxsymbol.h. */ @@ -65,19 +70,85 @@ MtxExpression::MtxExpression(MtxLanguage *language, else expressions.push_back(new MtxSymbol(language, *lexical_elements.begin())); + + function = MTXEXPRESSION_FUNCTION_NULL; } else { - mtx_expression_t::iterator iterator; + mtx_expression_t first_operand, second_operand; + size_t i; + + i = 0; - for (iterator = lexical_elements.begin(); - iterator != lexical_elements.end(); - iterator++) + while (operators[i].str != NULL) { + if (operators[i].associativity == MTXEXPRESSION_RIGHT_TO_LEFT) + { + mtx_expression_t::iterator iterator, tmp; + + iterator = find(lexical_elements.begin(), lexical_elements.end(), + operators[i].str); + + if (iterator == lexical_elements.end()) + { + i++; + continue; + } + + for (tmp = lexical_elements.begin(); + tmp != iterator; + tmp++) + first_operand.push_back(*tmp); + + for (tmp = iterator + 1; + tmp != lexical_elements.end(); + tmp++) + second_operand.push_back(*tmp); + } + + else + { + mtx_expression_t::reverse_iterator iterator, tmp; + + iterator = find(lexical_elements.rbegin(), + lexical_elements.rend(), + operators[i].str); + + if (iterator == lexical_elements.rend()) + { + i++; + continue; + } + + for (tmp = lexical_elements.rbegin(); + tmp != iterator; + tmp++) + second_operand.push_back(*tmp); + + for (tmp = iterator + 1; + tmp != lexical_elements.rend(); + tmp++) + first_operand.push_back(*tmp); + } + + break; } - throw MtxException("interpretation not yet implemented"); + /* + * An operator was found. + */ + if (operators[i].str != NULL) + { + function = operators[i].id; + expressions.push_back(new MtxExpression(language, first_operand, + silent)); + expressions.push_back(new MtxExpression(language, second_operand, + silent)); + } + + else + throw MtxException("operator/function not yet implemented"); } } @@ -92,19 +163,25 @@ MtxExpression::~MtxExpression() } MtxSymbol -MtxExpression::getSymbol() const +MtxExpression::getSymbol(bool create) const { - if (function.length() == 0 && expressions.size() == 1) - return (*expressions.begin())->getSymbol(); + if (function == MTXEXPRESSION_FUNCTION_NULL && expressions.size() == 1) + return (*expressions.begin())->getSymbol(create); else { - MtxRational array[] = {MtxRational(54.254), MtxRational(84.36), MtxRational(46.23), MtxRational(-89.15)}; - MtxMatrix matrix(array, 2, 2); MtxSymbol symbol; - symbol = MtxSymbol(language, "matrix", matrix); - return symbol; + switch ((size_t)function) + { + case MTXEXPRESSION_FUNCTION_ASSIGNMENT: + symbol = expressions[1]->getSymbol(); + symbol.setName(expressions[0]->getSymbol(true).getName()); + language->setSymbol(symbol); + return symbol; + } + + throw MtxException("nonexistent function"); } } |