To support simple parenthetical expressions, the get expression routine was modified to look for an open parentheses token just before checking for a unary operator. The open parentheses token is pushed to the hold stack so that when the expression is processed by recursively calling get expression, only operators from inside the expression will be popped from the hold stack (open parentheses has a very low precedence). After processing the closing parentheses, get expression continues on by getting and processing a binary operator of end-of-expression token.
The recursive get expression will terminate upon a closing parentheses token. In the table, the end expression flag had to be added to the closing parentheses code to cause this termination. Upon return, the terminating token is checked for a closing parentheses, otherwise an "expected operator or closing parentheses" error is returned. The open parentheses token is popped from the hold stack and the open and closing parentheses tokens are assigned to the first and last tokens of the item on top of the done stack (used for error reporting; details of this design start with posts on January 16, 2011).
The closing parentheses is saved as a pending parentheses token for later checking. This token is marked as being used as a last operand and as the pending parentheses (to prevent it from being prematurely deleted). The precedence of the last token added to the output list (which is on top of the done stack) is saved for when the pending parentheses token is later checked. If this token is an operator, the precedence is obtained from the table, otherwise it is set to the highest precedence (parentheses around not operators are never necessary).
A new check pending parentheses routine was added starting with the existing do pending parentheses routine (renamed more appropriately instead of adding a '2', but will still eventually replace the old routine). These routines check if parentheses entered are not necessary, in which case, a flag is set in the translated so that these will be reproduced. Necessary parentheses are implied by the translated RPN format. Parentheses necessity is determined by the precedence of the operators (see posts starting on March 21, 2010 for details).
The new check pending parentheses routine is called from two locations in the new process operator routine, one when an operator is popped from the hold stack before its final operand is processed and one for a new operator before it is checked for the end-of-expression or its first operand is processed. The check pending parentheses routine determines if parentheses are not necessary by checking if the precedence of last token added is greater than the current operator or of equal precedence if th operator was just popped from the hold stack. There is a new popped argument to determine from which (the old routine used the current translator state).
Expression test #2 (parentheses tests) now passes with the new translator routines, but one change was needed for the unexpected closing parentheses test that reported the "expected operator or end-of-expression" error (internally named NoOpenParen). When the wording of this error was changed, it should been been removed and the "expected operator or end-of-statement" error used instead. This error needs to be replaced by the caller as appropriate for the command in which it is detected (see post from Tuesday).
[commit d3d57f2235]
Wednesday, July 3, 2013
Subscribe to:
Post Comments (Atom)
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.)