diff options
author | Adrian Kummerländer | 2013-10-19 21:41:19 +0200 |
---|---|---|
committer | Adrian Kummerländer | 2013-10-19 21:41:19 +0200 |
commit | 69ce370c9ebf92caea64ef1ba28a502affdad3d5 (patch) | |
tree | 35d84b2c3080622f990bfe75396966c861d75e92 /src/nodes.cc | |
parent | c292b29f3bb87cf11edf899c023cfb574c20ed6c (diff) | |
download | SimpleParser-69ce370c9ebf92caea64ef1ba28a502affdad3d5.tar SimpleParser-69ce370c9ebf92caea64ef1ba28a502affdad3d5.tar.gz SimpleParser-69ce370c9ebf92caea64ef1ba28a502affdad3d5.tar.bz2 SimpleParser-69ce370c9ebf92caea64ef1ba28a502affdad3d5.tar.lz SimpleParser-69ce370c9ebf92caea64ef1ba28a502affdad3d5.tar.xz SimpleParser-69ce370c9ebf92caea64ef1ba28a502affdad3d5.tar.zst SimpleParser-69ce370c9ebf92caea64ef1ba28a502affdad3d5.zip |
Switched OperatorNode to TokenType
* OperatorNode class now uses TokenType for internal operator storage and logic selection
* print-Method resolves the TokenType back into its character representation
Diffstat (limited to 'src/nodes.cc')
-rw-r--r-- | src/nodes.cc | 68 |
1 files changed, 42 insertions, 26 deletions
diff --git a/src/nodes.cc b/src/nodes.cc index fac74a0..bab7ff7 100644 --- a/src/nodes.cc +++ b/src/nodes.cc @@ -7,18 +7,15 @@ namespace SimpleParser { -OperandNode::OperandNode(double val): - value_(val) { } - -OperandNode::OperandNode(std::string identifier): - value_(42) { } +OperandNode::OperandNode(double value): + value_(value) { } double OperandNode::solve() { return this->value_; } NodeType OperandNode::getType() { - return NodeType::OPERAND_NODE; + return NodeType::OPERAND; } std::string OperandNode::print() { @@ -30,15 +27,26 @@ std::string OperandNode::print() { return convertStream.str(); } -OperatorNode::OperatorNode(char op): - function_(op) { } +OperatorNode::OperatorNode(TokenType token): + operator_(token) { } double OperatorNode::solve() { - switch ( this->function_ ) { - case '*': { + switch ( this->operator_ ) { + case TokenType::OPERATOR_MULTIPLY: { return this->leftChild->solve() * this->rightChild->solve(); } - case '/': { + case TokenType::OPERATOR_PLUS: { + return this->leftChild->solve() + this->rightChild->solve(); + } + case TokenType::OPERATOR_MINUS: { + return this->leftChild->solve() - this->rightChild->solve(); + } + case TokenType::OPERATOR_POWER: { + return std::pow( + this->leftChild->solve(), this->rightChild->solve() + ); + } + case TokenType::OPERATOR_DIVIDE: { double rightChild = this->rightChild->solve(); if ( rightChild != 0 ) { @@ -48,17 +56,6 @@ double OperatorNode::solve() { throw divide_exception(); } } - case '+': { - return this->leftChild->solve() + this->rightChild->solve(); - } - case '-': { - return this->leftChild->solve() - this->rightChild->solve(); - } - case '^': { - return std::pow( - this->leftChild->solve(), this->rightChild->solve() - ); - } default: { throw operator_exception(); } @@ -66,15 +63,34 @@ double OperatorNode::solve() { } NodeType OperatorNode::getType() { - return NodeType::OPERATOR_NODE; + return NodeType::OPERATOR; } std::string OperatorNode::print() { - return std::string(1, this->function_); + switch ( this->operator_ ) { + case TokenType::OPERATOR_PLUS: { + return std::string(1, '+'); + } + case TokenType::OPERATOR_MINUS: { + return std::string(1, '-'); + } + case TokenType::OPERATOR_MULTIPLY: { + return std::string(1, '*'); + } + case TokenType::OPERATOR_DIVIDE: { + return std::string(1, '/'); + } + case TokenType::OPERATOR_POWER: { + return std::string(1, '^'); + } + default: { + throw operator_exception(); + } + } } -char OperatorNode::getFunction() { - return this->function_; +TokenType OperatorNode::getToken() { + return this->operator_; } } |