From cf2aa4c9d70fc8ed658c213b2c46bb48ee10e6f7 Mon Sep 17 00:00:00 2001 From: Adrian Kummerländer Date: Sun, 20 Oct 2013 00:10:54 +0200 Subject: 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 --- src/nodes.cc | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'src/nodes.cc') 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 @@ -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() { -- cgit v1.2.3