diff options
author | Adrian Kummerländer | 2013-10-20 00:10:54 +0200 |
---|---|---|
committer | Adrian Kummerländer | 2013-10-20 00:10:54 +0200 |
commit | cf2aa4c9d70fc8ed658c213b2c46bb48ee10e6f7 (patch) | |
tree | 02eda2354acb6462977cf9c6babcbd62b446d0e2 /src/nodes.cc | |
parent | 2b5ed18ae11439897a64708a434e5d7a6edac91e (diff) | |
download | SimpleParser-cf2aa4c9d70fc8ed658c213b2c46bb48ee10e6f7.tar SimpleParser-cf2aa4c9d70fc8ed658c213b2c46bb48ee10e6f7.tar.gz SimpleParser-cf2aa4c9d70fc8ed658c213b2c46bb48ee10e6f7.tar.bz2 SimpleParser-cf2aa4c9d70fc8ed658c213b2c46bb48ee10e6f7.tar.lz SimpleParser-cf2aa4c9d70fc8ed658c213b2c46bb48ee10e6f7.tar.xz SimpleParser-cf2aa4c9d70fc8ed658c213b2c46bb48ee10e6f7.tar.zst SimpleParser-cf2aa4c9d70fc8ed658c213b2c46bb48ee10e6f7.zip |
Implemented constant identifier functionality
* SimpleParser optionally acceps a pointer to an ConstantMap containing string keys mapping to values
* Constants are handled in their own ConstantNode class derived from the standard Node class
* Operator precedence is now determined separated from the TokenType using a new PrecedenceLevel enum
** Conversion between tokens and their PrecedenceLevel is possible using the new utility function getPrecedence
* Added additional test cases for constant identifier resolutions
Diffstat (limited to 'src/nodes.cc')
-rw-r--r-- | src/nodes.cc | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/src/nodes.cc b/src/nodes.cc index 1ab1502..9ef0e6a 100644 --- a/src/nodes.cc +++ b/src/nodes.cc @@ -1,5 +1,6 @@ #include "nodes.h" #include "utils.h" +#include "tree.h" #include "exceptions.h" #include <cmath> @@ -94,11 +95,22 @@ TokenType OperatorNode::getToken() { return this->operator_; } -ConstantNode::ConstantNode(std::string identifier): - identifier_(identifier) { } +ConstantNode::ConstantNode(std::string identifier, + const ConstantMap* constants): + identifier_(identifier), + constants_(constants) { } double ConstantNode::solve() { - + if ( this->constants_ != nullptr ) { + try { + return this->constants_->at(this->identifier_); + } + catch ( std::out_of_range &e ) { + throw identifier_exception(); + } + } else { + throw identifier_exception(); + } } NodeType ConstantNode::getType() { |