Tuesday, January 13, 2015

Translator – First/Last Operands Handling

After refactoring the token convert and changing its caller, the process done stack top function, I noticed this later function could be better implemented.  Specifically how the first and last operands of the tokens were being returned.  If the callers wanted these tokens returned, they would pass pointers to were to put the tokens, otherwise null pointers passed (also the default).  Using arguments as outputs is not the best design.  Changing this caused a cascade of additional refactoring.

The process done stack top function was changed to return the first and last operands as a standard pair of token pointers (now referred to appropriately as the first and second operands, the members of the pair).  If the caller doesn't need them, then it ignores the return value.  An Operands alias was added for this pair.  The local first and last variables were replaced with an operands pair, which is returned at the end of the function.  The comments to this function were removed as they were now outdated and restated what the function did.

The process final operand function was changed to use a first/second operands pair.  The operand index argument was used to determine if an operator token was unary.  The token itself can be used to determine this.  This argument was also passed to the process done stack top function, but it was always the last operand of the token, which can be obtained from the token.  This argument was removed.  The generic token2 argument was renamed more appropriately to first since it is the first operand of the token, and was made an r-value reference to force the caller to move its token to this function.  The comments to this function were also removed.

The process operator function, another caller of the process done stack top function, was also modified to use an operands pair (only the first operand returned is used).  The final caller, the LET translate function, does not use the returned operands and didn't need to be modified.

The first and last members of the internal Done Item structure (the values of the done stack member) were replaced with an operands pair.  The constructors were updated to initialize the first and second members of the operands pair and a new constructor was added with an operands pair argument.  The replace first last function (a poor name) was renamed to the more appropriate replace operands.

[branch table commit 240f967d4a]

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