45 std::ostringstream stream{};
67 if (not (stream >> type))
70 return std::make_shared<node_void>(stream);
72 return std::make_shared<node_number>(stream);
73 if (type ==
"identifier")
74 return std::make_shared<node_identifier>(stream);
75 if (type ==
"function")
76 return std::make_shared<node_function>(stream);
78 return std::make_shared<node_function_call>(stream);
80 return std::make_shared<node_negate>(stream);
82 return std::make_shared<node_add>(stream);
83 if (type ==
"subtract")
84 return std::make_shared<node_subtract>(stream);
85 if (type ==
"multiply")
86 return std::make_shared<node_multiply>(stream);
88 return std::make_shared<node_divide>(stream);
90 throw calc_error{
"unknown node type: " + type};
104 stream << std::setw(indent) <<
"" <<
"void\n";
116 return std::string{};
142 stream << std::setw(indent) <<
"" <<
value() <<
'\n';
167 if (not (stream >> name_))
168 throw calc_error(
"malformed library file, cannot read identifier");
180 stream << std::setw(indent) <<
"" <<
"identifier " <<
name() <<
'\n';
198 stream <<
"identifier " <<
name() <<
'\n';
203 :
node_impl{}, parameters_{std::move(parameters)}, definition_{definition}
210 if (not (stream >> size))
211 throw calc_error{
"malformed library file, cannot read function"};
212 parameters_.reserve(size);
213 while (size-- != 0) {
214 std::string parameter{};
215 if (not (stream >> parameter))
216 throw calc_error{
"malformed library file, cannot read function parameter"};
217 parameters_.emplace_back(std::move(parameter));
219 definition_ =
node{stream};
237 char const* sep =
"";
238 for (
auto const&
id : identifiers) {
248 stream << std::setw(indent) <<
"" <<
"fun\n";
269 stream <<
"function " <<
parameters().size() <<
' ';
271 stream << parameter <<
' ';
277 :
node_impl{}, name_{std::move(name)}, arguments_{std::move(arguments)}
284 if (not (stream >> name_))
285 throw calc_error{
"malformed library file, cannot read function call name"};
287 if (not (stream >> size))
288 throw calc_error{
"malformed library file, cannot read function call"};
289 arguments_.reserve(size);
290 while (size-- != 0) {
291 arguments_.emplace_back(stream);
310 stream << std::setw(indent) <<
"" <<
name() <<
"(\n";
311 std::size_t index{0};
313 stream << std::setw(indent+1) <<
"" <<
"arg " << index <<
": ";
314 arg.print(stream, indent + 2);
317 stream << std::setw(indent) <<
"" <<
")\n";
326 if (parameters.size() !=
arguments().size())
332 identifier_list::const_iterator parm{parameters.begin()};
334 locals.insert(std::make_pair(*parm, arg));
338 return function.evaluate();
345 stream <<
"call " <<
name() <<
' ' <<
arguments().size() <<
' ';
372 :
node_impl{}, left_{left}, right_{right}
376 :
node_impl{}, left_{stream}, right_{stream}
415 stream << std::setw(indent) <<
"" <<
"-\n";
445 stream << std::setw(indent) <<
"" <<
"+\n";
476 stream << std::setw(indent) <<
"" <<
"-\n";
490 stream <<
"subtract ";
507 stream << std::setw(indent) <<
"" <<
"*\n";
521 stream <<
"multiply ";
538 stream << std::setw(indent) <<
"" <<
"/\n";
node get_function(std::string const &name)
virtual void save_node(std::ostream &stream) const override
void save(std::ostream &stream) const
virtual void save_node(std::ostream &stream) const override
virtual std::string evaluate_string() const override
virtual std::string evaluate_string() const
virtual void print_node(std::ostream &stream, int indent) const override
number evaluate_operand() const
node_list const & arguments() const
virtual void print_node(std::ostream &stream, int indent) const override
virtual void print_node(std::ostream &stream, int indent) const override
virtual void save_node(std::ostream &stream) const override
node_function_call(std::string name, node_list arguments)
virtual number evaluate_node() const override
node_binary(node left, node right)
std::string const & name() const
virtual void print_node(std::ostream &stream, int indent) const override
virtual std::string evaluate_string() const override
node_identifier(std::string name)
virtual void save_node(std::ostream &stream) const override
virtual void print_node(std::ostream &stream, int indent) const override
number evaluate_right() const
void print(std::ostream &stream, int indent) const
virtual number evaluate_node() const override
virtual number evaluate_node() const override
number evaluate_left() const
std::vector< std::string > identifier_list
A sequence of identifiers (e.g., parameter names).
static std::shared_ptr< node_impl > read_node(std::istream &stream)
virtual void print_node(std::ostream &stream, int indent) const override
virtual void save_node(std::ostream &stream) const override
node_function(identifier_list parameters, node definition)
virtual number evaluate_node() const override
virtual number evaluate_node() const override
node_multiply(node left, node right)
virtual identifier_list const & evaluate_parameters() const override
virtual void print_node(std::ostream &stream, int indent) const override
node_number(number value)
void save(std::ostream &stream) const
void print(std::ostream &stream, int indent=0) const
virtual void save_node(std::ostream &stream) const override
void print_identifier_list(std::ostream &stream, identifier_list const &identifiers)
virtual number evaluate_node() const override
virtual void print_node(std::ostream &stream, int indent) const override
virtual number evaluate_node() const override
virtual void print_node(std::ostream &stream, int indent) const =0
virtual number evaluate_node() const =0
identifier_list parameters_
node_subtract(node left, node right)
virtual void save_node(std::ostream &stream) const =0
virtual void print_node(std::ostream &stream, int indent) const override
virtual identifier_list const & evaluate_parameters() const
virtual void save_node(std::ostream &stream) const override
identifier_list const & get_parameters() const
identifier_list const & get_parameters() const
std::vector< node > node_list
A sequence of nodes.
virtual number evaluate_node() const override
std::string to_string() const
void save(std::ostream &stream) const
virtual number evaluate_node() const override
virtual void save_node(std::ostream &stream) const override
node_negate(node operand)
virtual void print_node(std::ostream &stream, int indent) const override
virtual void save_node(std::ostream &stream) const override
node_divide(node left, node right)
node_add(node left, node right)
virtual void save_node(std::ostream &stream) const override
identifier_list const & parameters() const
node get_variable(std::string const &name)
std::map< std::string, node > symbol_table
virtual number evaluate_node() const override