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(int x) 00037 : pimpl_(new node_number(number(x))) 00038 {} 00039 00040 node::node(long x) 00041 : pimpl_(new node_number(number(x))) 00042 {} 00043 00044 node::node(double x) 00045 : pimpl_(new node_number(number(x))) 00046 {} 00047 00048 node::node(number num) 00049 : pimpl_(new node_number(num)) 00050 {} 00051 00052 node::node(std::string const& identifier) 00053 : pimpl_(new node_identifier(identifier)) 00054 {} 00055 00056 node::node(char op, node operand) 00057 : pimpl_(new node_negate(operand)) 00058 {} 00059 00060 node::node(node left, char op, node right) 00061 : pimpl_(make_binary_operator(left, op, right)) 00062 {} 00063 00064 node::node(identifier_list const& parameters, node definition) 00065 : pimpl_(new node_function(parameters, definition)) 00066 {} 00067 00068 node::node(std::string const& name, node_list const& arguments) 00069 : pimpl_(new node_function_call(name, arguments)) 00070 {} 00071 00072 node::~node() 00073 { 00074 pimpl_->del_ref(); 00075 } 00076 00077 node& node::operator=(node const& n) 00078 { 00079 n.pimpl_->add_ref(); 00080 pimpl_->del_ref(); 00081 pimpl_ = n.pimpl_; 00082 00083 return *this; 00084 } 00085 00086 void node::print(std::ostream& stream, int indent) 00087 const 00088 { 00089 pimpl_->print(stream, indent); 00090 } 00091 00092 number node::evaluate() 00093 const 00094 { 00095 return pimpl_->evaluate(); 00096 } 00097 00098 std::string node::to_string() 00099 const 00100 { 00101 return pimpl_->to_string(); 00102 } 00103 00104 identifier_list const& node::get_parameters() 00105 const 00106 { 00107 return pimpl_->get_parameters(); 00108 } 00109 00110 void node::save(std::ostream& stream) 00111 const 00112 { 00113 pimpl_->save(stream); 00114 }