Expression Parts Implementation
#include "expression_parts.h" #include "expr_stack.h" // Part printer. std::ostream & operator<<(std::ostream &strm, const Expression_Part &p) { p.print(strm); return strm; } // Action for reading an integer. void Operand::action(expr_stack &s) { s.push(shared_from_this()); } // Action on reading ( void Left::action(expr_stack &s) { s.push(shared_from_this()); } // Action on reading an operator. void Operator::action(expr_stack &s) { // Perform pending operations of equal or greater precedence, // then push ourself onto the stack. s.reduce(m_precedence); s.push(shared_from_this()); } /* * When a right paren is input, use reduce to replace the contents of * the parenthetical with its value. Then remove the paren itself, and * discard it, along with the one read. Throws if something goes wrong. */ void Right::action(expr_stack &s) { // Perform all operations to the matching left paren. s.reduce(); // Remove the value, remove and discard the left paren, then // return the value. auto paren_value = s.pop(); if(!s.pop()->is_left()) throw bad_expression("Unbalanced right paren."); s.push(paren_value); }