Calculator
Extensible stack-based calculator primarily in library form
StackManipulator.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2013, Komodo Does Inc
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6 
7 - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8 - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
9 - Neither the name of the Komodo Does Inc nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10 
11 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
12 */
13 
14 #include <memory>
15 #include <string>
16 #include <vector>
17 
18 #include "Error.h"
19 #include "StackManipulator.h"
20 #include "Number.h"
21 #include "Stack.h"
22 
23 namespace Calculator {
24 
26  return Ptr(new StackManipulator(theOp));
27  }
28 
30  }
31 
32  StackManipulator::StackManipulator(Operation theOp)
33  :op(theOp)
34  {
35  }
36 
38  if(Operation::POP == op) {
39  StackIterator iter = stack.begin();
40  *iter; // Dereference even though we don't care to ensure that stack depth is auto checked
41  ++iter;
42 
43  if(!iter) {
44  return iter.getResult();
45  }
46 
47  stack.popAfter(iter);
48 
49  return Result({Error::Ok});
50  } else if(Operation::DUP == op) {
51  StackItem::Ptr item;
52  StackIterator iter = stack.begin();
54 
55  if(!iter) {
56  return iter.getResult();
57  }
58 
59  // Do NOT stack.popAfter(iter) -- we are keeping it and duplicating it
60  (*item)(stack, item);
61 
62  return Result({Error::Ok});
63  } else if(Operation::SWAP == op) {
64  StackItem::Ptr first;
65  StackItem::Ptr second;
66  StackIterator iter = stack.begin();
69  // stack.popAfter() deferred until after evaluation
70 
71  if(!iter) {
72  return iter.getResult();
73  }
74 
75  stack.popAfter(iter);
76 
77  (*first)(stack, first);
78  (*second)(stack, second);
79 
80  return Result({Error::Ok});
81  } else if(Operation::POP_ALL == op) {
82  stack.popAll();
83  return Result({Error::Ok});
84  } else if(Operation::RESET == op) {
85  stack.reset();
86  return Result({Error::Ok});
87  }
88 
89  // Internal error.... Not sure what to do.
90  return Result({"Internal Error: Missing StackManipulator."});
91  }
92 
94  }
95 
96  std::string StackManipulatorCreator::getHelp() const {
97  return std::string() +
98  "pop -- remove the top element\n" +
99  "dup -- add a copy of the top element\n" +
100  "swap -- exchange the top two values on the stack\n" +
101  "reset -- reset the stack including removing all elements of the and clearing all variables\n" +
102  "pop-all -- remove all elements of the stack";
103  }
104 
107  if(0 == str.compare("pop")) {
109  } else if(0 == str.compare("dup")) {
111  } else if(0 == str.compare("swap")) {
113  } else if(0 == str.compare("reset")) {
115  } else if(0 == str.compare("pop-all")) {
117  } else {
118  return StackOperator::Ptr();
119  }
120 
121  return StackManipulator::create(op);
122  }
123 
124 } // namespace Calculator
Collection of Result information for an operation.
Definition: Result.h:30
Stack of values to process from.
Definition: Stack.h:208
StackIterator begin()
Definition: Stack.cpp:221
void popAfter(StackIterator &iter)
Pop all items after iter on the Stack.
Definition: Stack.cpp:233
void popAll()
Pop all items from the stack.
Definition: Stack.cpp:217
void reset()
Reset the stack including pop all variables and reset the variables.
Definition: Stack.cpp:212
std::shared_ptr< StackItem > Ptr
Definition: StackItem.h:30
Iterator over a Stack from top to bottom.
Definition: Stack.h:35
@ NO_DEREFERENCE_NEXT
Do not dereference the next read.
const Result & getResult() const
Definition: Stack.cpp:150
virtual StackOperator::Ptr create(const std::string &str)
virtual std::string getHelp() const
Stack Modifier for Stack Operations.
static StackManipulator::Ptr create(Operation operation)
Create to perform operation.
virtual Result operator()(Stack &stack, StackOperator::Ptr ofThis)
Execute this operator on stack.
Operation
Operations supported.
@ SWAP
Swap the top two elements.
@ POP_ALL
Clear the entire stack.
@ RESET
Clear the entire stack and set of variables.
@ POP
Pop the top value off and forget it.
std::shared_ptr< StackOperator > Ptr
Definition: StackOperator.h:31
const std::string Ok
Operation was successful.
Definition: Error.h:23
Container of Calculator resources.