A + BAt the A operand, the string "A" will be pushed to the holding stack. Same for the B operand with the "B" string being pushed. The plus operator will pop the second operand, the "B" string, from the stack. The string for the operator will be appended with surrounding spaces to the operand now on top of the stack, specifically the " + " string gets append to the "A" string leaving "A + " on top of the stack. The previously popped second operand is then appended to the top of the stack leaving the "A + B" string on top of the stack, which may be an operand for the next operator.
A B +
For unary operators, the string for the operator is created. A separator space is appended only for a word operator like NOT, otherwise no space is needed (for example the "-" unary operator). The string of the single operand on top of the stack is popped and appended to the operator string. The resulting string is pushed back to the stack.
For simple expressions, only support for operands (variables and constants) and operators are needed. For string constants some processing is needed: all double quotes in the string of the constant need to be changed to two doubles quotes, and the entire string needs to be surrounded by a pair of double quotes.
The table entry for each code will have a pointer to a recreate function. Since operands already have their string in the token of the item, this string just needs to be pushed to the string holding stack (with the exception of string constants). Since this is trivial, a null pointer will indicate that the string of the token should be pushed to the string holding stack. For string constants, there will be a constant string recreate function. Since all binary operators work the same, there only needs to be a single binary operator recreate function. Likewise for the unary operators.
The recreate process will loop through each item in the RPN list. If there is no recreate function, the string of the token is pushed onto the string holding stack. Otherwise the recreate function is called to process the token. At the end of the RPN list, the resulting output string is returned. For expression mode, nothing gets appended to the output list, so the tester class will need to pop the result string of the expression from the string holding stack.
No comments:
Post a Comment
All comments and feedback welcomed, whether positive or negative.
(Anonymous comments are allowed, but comments with URL links or unrelated comments will be removed.)