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