Calculator
Extensible stack-based calculator primarily in library form
VariableManipulator.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 "VariableManipulator.h"
20 #include "Variable.h"
21 #include "VariableSet.h"
22 #include "Number.h"
23 #include "Stack.h"
24 
25 namespace Calculator {
26 
28  return Ptr(new VariableManipulator(op));
29  }
30 
32  }
33 
35  :op(operation)
36  {
37  }
38 
40  if(Operation::WRITE == op) {
41  Variable::Ptr variable;
42  StackItem::Ptr value;
43  StackIterator iter = stack.begin();
46  // stack.popAfter() deferred until after variable evaluation
47 
48  if(!iter) {
49  return iter.getResult();
50  }
51 
52  stack.popAfter(iter);
53 
54  stack.getVariables().set(variable->getName(), value);
55 
56  return Result({Error::Ok});
57  } else if(Operation::READ == op) {
58  Variable::Ptr variable;
59  StackIterator iter = stack.begin();
60  iter >> StackIterator::Hint::NO_DEREFERENCE_NEXT >> variable;
61  // stack.popAfter() deferred until after variable evaluation
62 
63  if(!variable) {
64  // Already detected non variable, just return errors
65  return iter.getResult();
66  }
67 
68  StackItem::Ptr result = stack.getVariables().get(variable->getName());
69  if(!result) {
70  // Error not detected by iter, add it
72  }
73  if(!iter) {
74  return iter.getResult();
75  }
76 
77  stack.popAfter(iter);
78 
79  (*result)(stack, result);
80  return Result({Error::Ok});
81  }
82 
83  // Internal error.... Not sure what to do.
84  return Result({"Internal Error: Missing VariableManipulator."});
85  }
86 
88  }
89 
91  return std::string() +
92  "! -- write to the variable top element the value beneath it\n" +
93  "@ -- replace the top variable with its value";
94  }
95 
98  if(0 == str.compare("!")) {
100  } else if(0 == str.compare("@")) {
102  } else {
103  return StackOperator::Ptr();
104  }
105 
106  return VariableManipulator::create(op);
107  }
108 
109 } // 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
VariableSet & getVariables()
Definition: Stack.cpp:208
void popAfter(StackIterator &iter)
Pop all items after iter on the Stack.
Definition: Stack.cpp:233
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
void addError(unsigned int position, const std::string &message)
Add error message at position.
Definition: Stack.cpp:154
std::shared_ptr< StackOperator > Ptr
Definition: StackOperator.h:31
std::shared_ptr< Variable > Ptr
Definition: Variable.h:31
virtual StackOperator::Ptr create(const std::string &str)
virtual Result operator()(Stack &stack, StackOperator::Ptr ofThis)
Execute this operator on stack.
VariableManipulator(Operation op)
Create to perform operation.
@ READ
Replace the top variable with its value.
@ WRITE
Write the value in next-to-top to variable in top.
static VariableManipulator::Ptr create(Operation op)
Create to perform operation.
std::shared_ptr< VariableManipulator > Ptr
void set(const std::string &name, std::shared_ptr< StackItem > value)
Set the value of name, overriding existing values.
Definition: VariableSet.cpp:49
std::shared_ptr< StackItem > get(const std::string &name) const
Get the value of name, if any.
Definition: VariableSet.cpp:40
const std::string Ok
Operation was successful.
Definition: Error.h:23
const std::string VariableNotSet
Operation required a variable.
Definition: Error.h:29
Container of Calculator resources.