From 754cc721222ccb01acc4d93ffd8f88f172a0cdd0 Mon Sep 17 00:00:00 2001 From: Adrian Kummerländer Date: Sat, 19 Oct 2013 19:35:16 +0200 Subject: POC: alphabetic constants * New priority for alphabetic characters * Overloaded OperandNode constructor * Currently not usable, only basic proof of concept --- src/nodes.cc | 3 +++ src/nodes.h | 1 + src/tree.cc | 32 ++++++++++++++++++++------ src/tree.h | 1 + src/utils.cc | 75 +++++++++++++++++++++++++++++++++++++----------------------- 5 files changed, 76 insertions(+), 36 deletions(-) (limited to 'src') diff --git a/src/nodes.cc b/src/nodes.cc index 390eac7..fac74a0 100644 --- a/src/nodes.cc +++ b/src/nodes.cc @@ -10,6 +10,9 @@ namespace SimpleParser { OperandNode::OperandNode(double val): value_(val) { } +OperandNode::OperandNode(std::string identifier): + value_(42) { } + double OperandNode::solve() { return this->value_; } diff --git a/src/nodes.h b/src/nodes.h index 3a99474..a59f4a5 100644 --- a/src/nodes.h +++ b/src/nodes.h @@ -39,6 +39,7 @@ class OperatorNode: public Node { class OperandNode: public Node { public: explicit OperandNode(double); + explicit OperandNode(std::string); virtual double solve(); virtual NodeType getType(); diff --git a/src/tree.cc b/src/tree.cc index 1dcfca6..81de460 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) @@ -119,7 +131,7 @@ Node* Tree::buildTree(std::string term) { std::string& currTerm = (*termIter); priority = getPriority(currTerm[0]); - if ( priority != -1 && (*termIter).size() == 1 ) { + if ( priority != -1 && priority != 100 && (*termIter).size() == 1 ) { if ( !operatorStack.empty() ) { OperatorNode* lastNode( static_cast(topNodeFrom(operatorStack)) @@ -152,13 +164,19 @@ Node* Tree::buildTree(std::string term) { tmpLexer = lexer(*termIter); if ( tmpLexer.size() == 1 ) { - double value; - std::istringstream convertStream(tmpLexer[0]); - convertStream >> value; + if ( getPriority(tmpLexer[0][0]) == -1 ) { + double value; + std::istringstream convertStream(tmpLexer[0]); + convertStream >> value; - operandStack.push( - this->addOperand(nullptr,value) - ); + operandStack.push( + this->addOperand(nullptr,value) + ); + } else if ( getPriority(tmpLexer[0][0]) == 100 ) { + operandStack.push( + this->addOperand(nullptr,tmpLexer[0]) + ); + } } else if ( tmpLexer.size() > 1 ) { operandStack.push( buildTree(*termIter) diff --git a/src/tree.h b/src/tree.h index 5beec87..f20f765 100644 --- a/src/tree.h +++ b/src/tree.h @@ -18,6 +18,7 @@ class Tree { private: Node* addOperand(Node**, double); + Node* addOperand(Node**, std::string); Node* addOperator(Node**, char); Node* buildTree(std::string); diff --git a/src/utils.cc b/src/utils.cc index 7457ae2..8914166 100644 --- a/src/utils.cc +++ b/src/utils.cc @@ -1,36 +1,42 @@ #include "utils.h" +#include "tree.h" #include "exceptions.h" -#include "tree.h" +#include namespace SimpleParser { int8_t getPriority(char tmp) { - switch ( tmp ) { - case '-': - return 10; - case '+': - return 10; - case '/': - return 20; - case '*': - return 20; - case '^': - return 30; - case '(': - return 90; - case ')': - return 90; - case ',': - return -1; - default: - return -1; + if ( std::isalpha(tmp) ) { + return 100; + } else { + switch ( tmp ) { + case '-': + return 10; + case '+': + return 10; + case '/': + return 20; + case '*': + return 20; + case '^': + return 30; + case '(': + return 90; + case ')': + return 90; + case ',': + return -1; + default: + return -1; + } } } std::vector lexer(std::string term) { std::string tmp; - std::string tmpNum; + std::string tmpNumber; + std::string tmpIdentifier; std::vector output; int8_t priority = 0; @@ -42,17 +48,26 @@ std::vector lexer(std::string term) { termIter++ ) { priority = getPriority(*termIter); - if ( priority == -1 || ( termIter == term.begin() && - priority == 10 ) ) { + if ( priority == -1 || priority == 100 || ( termIter == term.begin() && + priority == 10 ) ) { if ( level > 0 ) { tmp += *termIter; } else { - tmpNum += *termIter; + if ( priority == -1 ) { + tmpNumber += *termIter; + } else if ( priority == 100 ) { + tmpIdentifier += *termIter; + } } } else { - if ( lastPriority == -1 && level == 0 ) { - output.push_back(tmpNum); - tmpNum.clear(); + if ( level == 0 ) { + if ( lastPriority == -1 ) { + output.push_back(tmpNumber); + tmpNumber.clear(); + } else if ( lastPriority == 100 ) { + output.push_back(tmpIdentifier); + tmpIdentifier.clear(); + } } switch ( *termIter ) { @@ -96,12 +111,14 @@ std::vector lexer(std::string term) { } if ( lastPriority == -1 ) { - output.push_back(tmpNum); + output.push_back(tmpNumber); + } else if ( lastPriority == 100 ) { + output.push_back(tmpIdentifier); } else if ( lastPriority != 90 ) { throw operator_exception(); } - if (level != 0) { + if ( level != 0 ) { throw parenthese_exception(); } -- cgit v1.2.3