Friday, June 28, 2013

Minor Translator Improvements

Before embarking in the new translator routines, some minor improvements were made to the hold and done stacks.  The same improvements could also have been made to the count and command stacks, but (spoiler alert) these stacks won't be needed for the new translator.

For the hold stack, there were several places where the top item on the stack is popped.  The pop() function of QStack returns the item popped.  But for these uses, the top item is not needed so a somewhat convoluted method of resizing the stack less one item was used for efficiency.  It would be much better for code readability to use a simple function.  A similar method was used for pushing items, where the stack was resized (up by one) first and then the members of the item filled in.

To make the code easy to work with and increase readability, a new HoldStack class was implemented, based on the QStack class, which contains a new drop() function, for popping the top item without returning it, and a push() function with specific arguments for the token and first token pointers (with the first token pointer defaulting to null since it is not always set for a push operation).  Both functions still use the resize method, but these are now hidden in the class definition.

Similarly, a new DoneStack class was implemented except only a push() function was implemented since there were no uses of an empty pop.  This push() function takes arguments for the RPN item pointer along with the first and last token pointers (both defaulting to null since these are not always set for push operations).

A minor change was also made to the table initialization routine that checks the maximum number of operands and associated codes.  This routine now checks to make sure the fixed constants for these values are set exactly to the actual values found in the table instead of just checking if the constant at least as big as found in the table.  The original thought was that this value would never decrease, which was not the case for the last change that decreased the maximum number of associated codes.  (See post on May 20, 2010 for details of these constants.)

[commit cdfc70b43f] [commit 9951f9b9da] [commit 0f0fb97142]

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.)