Saturday, November 22, 2014

Translator Exceptions – Internal Functions

The process internal function function used to translate internal function tokens for the get operand function was modified to throw errors.  Some of the changes made in the failed attempt to add exceptions throughout the translator routines around the get operand call used for sub-string assignments ended up in the code during the last commit.  This did not affect the functionality (since the regression tests had passed, it wasn't noticed).

The expression error status private helper function was added to determine the error status depending on whether at the last operand of the internal function, if the internal function has multiple arguments and if the bad token is a unary operator.  This function is called in three locations.

Since all errors are thrown, there was no longer a need for a return value.  The unary operator local variable was no longer needed and was removed.  The status and expected data type local variable declarations were moved closer to where they are used.  The status variable can be removed once the get expression function is modified to throw errors.

[branch misc-cpp-stl commit 640255514d]

Translator Exceptions – Operands

The get operand function used to get an operand was modified to throw errors.  The command routines either returned an error or a Done status, so once they were modified to throw errors, there was no need to return anything.  In addition to errors, the get operand function returned a Good status representing successfully getting an operand or a Done status representing  no operand token (an operator or command).

The return value of the get operand function was changed to a boolean where true represents successfully getting an operand and false representing no operand.  When a reference operand is requested this function will not return a false status, it will just throw an error.  So for the process internal function, LET translate, and INPUT translate routines, it is not necessary to test the return value since these only request reference operands.

There were three locations in the get operand function where errors were returned that popped the token on top of the hold stack before returning.  This was intended to prevent memory leaks.  This wasn't necessary since the translator cleans up the stack when the translator goes out of scope, so these pop calls were removed.

One of the errors for define function identifiers with parentheses tokens added the length of the token to the column to point the error to the parentheses of the token.  The token class add length to column access function was used for this.  Instead of using this access function (the only caller), the token length is added to the column when the token error instance is created and thrown.  This access function was removed.

[branch misc-cpp-stl commit 640255514d]

Translator Exceptions – Commands

The routines that process commands were the next modified to throw exceptions.  This included the get commands routine used to get command statements separated by colons.  For now, if the two calls to the get token function return an error (not good status), the appropriate error is thrown.  The get token function will also be modified to throw errors soon.  Since get commands now throws errors, it no longer needs to return a status so the return type was removed (along with the local status variable).

The process command routine handles the processing of one command and was modified to throw exceptions.  It calls a specific translate function for a command or the LET translate function if the first token is not a command.  The LET, PRINT and INPUT translate functions were also modified to throw exceptions.  The status return type was also removed and if statement surrounding the call to process command in get commands was removed (which will cause thrown exceptions to be thrown up its caller - the translator function operator function).

In the translator function operator function, a status is no longer returned from the get commands function, so assignment of the local status variable was removed.  And thrown exceptions are passed up to the caller of the function operator function (which will catch the errors).  For a successful return, the local status variable is set to Done.  This is temporary until the get expression function is also modified to throw exceptions.

The LET, PRINT and INPUT translate functions were restructured a bit.  Local variable declarations were moved to where the variables are first used.  Some of the error checking if statements were rearranged to ease the handling of errors (when the lower functions are modified to throw exceptions).  These changes came from the failed total translator exception changes.  The checks for the end-of-statement were also moved to a more logical place in each of these routines.

[branch misc-cpp-stl commit 7217ce58c6]