00001 #include <stdexcept> 00002 00003 #include "node.hpp" 00004 #include "node_impl.hpp" 00005 00006 node_impl* node::make_binary_operator(node left, char op, node right) 00007 { 00008 if (op == '+') 00009 return new node_add(left, right); 00010 else if (op == '-') 00011 return new node_subtract(left, right); 00012 else if (op == '*') 00013 return new node_multiply(left, right); 00014 else if (op == '/') 00015 return new node_divide(left, right); 00016 else 00017 throw std::logic_error("fatal error in make_binary_opreator: unknown operator: " + op); 00018 } 00019 00020 node::node() 00021 : pimpl_(new node_void()) 00022 {} 00023 00024 node::node(node const& n) 00025 : pimpl_(n.pimpl_) 00026 { 00027 pimpl_->add_ref(); 00028 } 00029 00030 node::node(double number) 00031 : pimpl_(new node_number(number)) 00032 {} 00033 00034 node::node(std::string const& identifier) 00035 : pimpl_(new node_identifier(identifier)) 00036 {} 00037 00038 node::node(node identifier, node number) 00039 : pimpl_(new node_assign(identifier, number)) 00040 {} 00041 00042 node::node(char op, node operand) 00043 : pimpl_(new node_negate(operand)) 00044 {} 00045 00046 node::node(node left, char op, node right) 00047 : pimpl_(make_binary_operator(left, op, right)) 00048 {} 00049 00050 node::~node() 00051 { 00052 pimpl_->del_ref(); 00053 } 00054 00055 node& node::operator=(node const& n) 00056 { 00057 n.pimpl_->add_ref(); 00058 pimpl_->del_ref(); 00059 pimpl_ = n.pimpl_; 00060 00061 return *this; 00062 } 00063 00064 void node::print(std::ostream& stream, int indent) 00065 const 00066 { 00067 pimpl_->print(stream, indent); 00068 } 00069 00070 double node::evaluate() 00071 const 00072 { 00073 return pimpl_->evaluate(); 00074 } 00075 00076 std::string node::to_string() 00077 const 00078 { 00079 return pimpl_->to_string(); 00080 }