Calculator
Step 4
|
#include <node.hpp>
Public Member Functions | |
node () | |
node (double number) | |
node (std::string identifier) | |
node (char op, node operand) | |
node (node left, char op, node right) | |
node (identifier_list parameters, node definition) | |
node (std::string name, node_list const &arguments) | |
node (node const &)=default | |
node (node &&)=default | |
node & | operator= (node const &)=default |
node & | operator= (node &&)=default |
~node () | |
void | print (std::ostream &stream, int indent=0) const |
double | evaluate () const |
std::string | to_string () const |
identifier_list const & | get_parameters () const |
Static Private Member Functions | |
static std::shared_ptr< node_impl > | make_binary_operator (node, char, node) |
Factory function to make the binary operator nodes. More... | |
Private Attributes | |
std::shared_ptr< node_impl > | pimpl_ |
Wrapper class for all parse-tree nodes. The actual parse tree nodes derive from node_impl. This class uses the pimpl idiom to make it easier to work with node objects.
node::node | ( | double | number | ) |
node::node | ( | std::string | identifier | ) |
node::node | ( | char | op, |
node | operand | ||
) |
Construct a binary operator node.
left | The left-hand parse-tree |
op | The operator character, e.g., '*' |
right | The right-hand operand |
Definition at line 39 of file node.cpp.
References make_binary_operator().
node::node | ( | identifier_list | parameters, |
node | definition | ||
) |
node::node | ( | std::string | name, |
node_list const & | arguments | ||
) |
|
default |
|
default |
double node::evaluate | ( | ) | const |
Evaluate a parse tree. Evaluate and tree and return the result.
Definition at line 57 of file node.cpp.
References pimpl_.
Referenced by BOOST_AUTO_TEST_CASE(), node_binary::evaluate_left(), node_identifier::evaluate_node(), node_function::evaluate_node(), node_unary::evaluate_operand(), node_binary::evaluate_right(), and parser::get_statement().
identifier_list const & node::get_parameters | ( | ) | const |
Return a list of parameter names. Most node return a reference to a global, static, const, empty list. Only function nodes return a list of parameters.
Definition at line 69 of file node.cpp.
References pimpl_.
Referenced by node_function_call::evaluate_node().
|
staticprivate |
void node::print | ( | std::ostream & | stream, |
int | indent = 0 |
||
) | const |
Print a tree. For debugging, print a visual representation of the parse tree. Each level increments the indent
to make the tree structure evident.
stream | The output stream |
indent | The indentation. |
Definition at line 51 of file node.cpp.
References pimpl_.
Referenced by node_function::print_node(), node_negate::print_node(), node_add::print_node(), node_subtract::print_node(), node_multiply::print_node(), and node_divide::print_node().
std::string node::to_string | ( | ) | const |
Return a string representation. For an identifier node, return the identifier. For all other nodes, return a string representation of the evaluated value.
Definition at line 63 of file node.cpp.
References pimpl_.
Referenced by BOOST_AUTO_TEST_CASE().
|
private |
Definition at line 92 of file node.hpp.
Referenced by evaluate(), get_parameters(), print(), and to_string().