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(char op, node operand)
00039 : pimpl_(new node_negate(operand))
00040 {}
00041
00042 node::node(node left, char op, node right)
00043 : pimpl_(make_binary_operator(left, op, right))
00044 {}
00045
00046 node::node(identifier_list const& parameters, node definition)
00047 : pimpl_(new node_function(parameters, definition))
00048 {}
00049
00050 node::node(std::string const& name, node_list const& arguments)
00051 : pimpl_(new node_function_call(name, arguments))
00052 {}
00053
00054 node::~node()
00055 {
00056 pimpl_->del_ref();
00057 }
00058
00059 node& node::operator=(node const& n)
00060 {
00061 n.pimpl_->add_ref();
00062 pimpl_->del_ref();
00063 pimpl_ = n.pimpl_;
00064
00065 return *this;
00066 }
00067
00068 void node::print(std::ostream& stream, int indent)
00069 const
00070 {
00071 pimpl_->print(stream, indent);
00072 }
00073
00074 double node::evaluate()
00075 const
00076 {
00077 return pimpl_->evaluate();
00078 }
00079
00080 std::string node::to_string()
00081 const
00082 {
00083 return pimpl_->to_string();
00084 }
00085
00086 identifier_list const& node::get_parameters()
00087 const
00088 {
00089 return pimpl_->get_parameters();
00090 }