aboutsummaryrefslogtreecommitdiff
path: root/src/tree.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/tree.cc')
-rw-r--r--src/tree.cc48
1 files changed, 39 insertions, 9 deletions
diff --git a/src/tree.cc b/src/tree.cc
index 0f5c2d8..2dfb786 100644
--- a/src/tree.cc
+++ b/src/tree.cc
@@ -84,6 +84,18 @@ Node* Tree::addOperand(Node** place, double value) {
return this->node_collection_.back().get();
}
+Node* Tree::addOperand(Node** place, std::string value) {
+ this->node_collection_.emplace_back(
+ new OperandNode(value)
+ );
+
+ if ( place != nullptr ) {
+ *place = this->node_collection_.back().get();
+ }
+
+ return this->node_collection_.back().get();
+}
+
Node* Tree::addOperator(Node** place, char oper) {
this->node_collection_.emplace_back(
new OperatorNode(oper)
@@ -117,7 +129,9 @@ Node* Tree::buildTree(std::string term) {
const std::string& currTerm = (*termIter);
const TokenType token = getTokenType(currTerm[0]);
- if ( token != TokenType::VALUE_NUMBER && (*termIter).size() == 1 ) {
+ if ( token != TokenType::VALUE_NUMBER &&
+ token != TokenType::VALUE_IDENTIFIER &&
+ (*termIter).size() == 1 ) {
if ( !operatorStack.empty() ) {
OperatorNode* lastNode(
static_cast<OperatorNode*>(topNodeFrom(operatorStack))
@@ -150,14 +164,30 @@ Node* Tree::buildTree(std::string term) {
tmpLexer = lexer(*termIter);
if ( tmpLexer.size() == 1 ) {
- double value;
- std::istringstream convertStream(tmpLexer[0]);
- convertStream >> value;
-
- operandStack.push(
- this->addOperand(nullptr,value)
- );
- } else if ( tmpLexer.size() > 1 ) {
+ switch ( getTokenType(tmpLexer[0][0]) ) {
+ case TokenType::VALUE_NUMBER: {
+ double value;
+ std::istringstream convertStream(tmpLexer[0]);
+ convertStream >> value;
+
+ operandStack.push(
+ this->addOperand(nullptr,value)
+ );
+
+ break;
+ }
+ case TokenType::VALUE_IDENTIFIER: {
+ operandStack.push(
+ this->addOperand(nullptr,tmpLexer[0])
+ );
+
+ break;
+ }
+ default: {
+ throw operator_exception();
+ }
+ }
+ } else {
operandStack.push(
buildTree(*termIter)
);