You are to implement the run time environment for a STACK machine. A valid STACK program is a sequence of instructions, which are read one at a time (from a file) and then evaluated by the STACK machine. Your assignment is to build this machine.
The machine operates on a single run-time stack, which is empty when execution begins. For each input instruction the machine performs the following operations.
1. An instruction is read from the input stream.
2. The instruction is evaluated as described below.
With the exception of the push instruction, none of the instructions takes a parameter.
The push instruction pushes operands on the stack. The input operands are of the following types: integers, reals, quoted strings, booleans (true or false).
The operators you should implement are listed below.
push pushes StackItem object (integers are pushed as Integer objects, reals are pushed as Double objects, quoted literal strings are pushed as String objects, true and false are pushed as Boolean objects)
pop discard top stack item
exch exchange top two stack items (pop top two stack items; push first operand then push second operand)
dup pop top stack item; push two copies on the stack
clear discard all items on stack
count count the number of items on the stack; push resulting integer value
add pop the top two items from the stack; push their sum on the stack (the items must be of the same type, the only allowed types are Integer or Double or Strings, addition of 2 strings is equivalent to concatenation, in case of type mismatch generate error)
sub pop the top two items from the stack; push the difference between the first and the second operands (the items must be of the same type, integer or float)
mul pop the top two items from the stack; push their product on the stack (the items must be of the same type, integer or double)
div pop the top two items from the stack; push the quotient of the first and the second operands (the items must be of the same type, integer or float)
int pop the top stack item, which should be a floating point number, and push the same value, converted to an integer
float pop the top stack item, which should be an integer, and push the same value, converted to a floating point number
equal pop the top two items; push true if the first operand equals the second operand, push false otherwise (operands of different type are not equal)
greater pop the top two items; push true if the first operand is greater than the second operand, push false otherwise
lessthan pop the top two items; push true if the first operand is less than the second operand, push false otherwise
not pop the top item; push its negation (applicable to integers, floats and booleans)
and pop top two items; push their conjunction (applicable to booleans)
or pop top two items; push their disjunction (applicable to booleans)
if pop top item; if true do nothing, but if false pop next item from stack
ifelse pop the top three items from the stack; if the first operand is true push the second operand, if false push the third operand
show print the top item on the stack
Make sure to print a message showing push and pop actions.
If there is an error during input or evaluation (e.g. an attempt to push an unquoted string, too few items on the stack, or operands of the wrong type for an operator), then an appropriate error message should be printed, the contents of the stack should be printed, and execution should terminate.
The comparison operators (equal, greater, lessthan) can be applied to all operand types (but the boolean values true and false can only be tested for equality).
For operators that consume more than one operand, the first one popped is the first, the second one popped is the second and so on.
our instructor gave us this help function but I have no idea how I can make use of it in my code :
template <class T>
bool fromString(T& t, const string& s)
return !(iss >> t).fail();
StackItem *convertToStackItem(string s)
if ( (s == "true") || (s == "false") )
return new Boolean(s);
else if (fromString(f, s)) // try to convert to double