Wednesday, August 14, 2013

New Translator – PRINT Statements

The implementation of the PRINT translate routine using the new translator routines was mostly straight forward, but some time was spent getting the right errors reported at the correct tokens (the previous post dealt with some of these issues in the translator routines).  Three local variables were needed to hold the pointer to the last semicolon token (last one is used instead of the command token if at the end of the statement), a separator flag (indicates when a semicolon is allowed) and a print function flag (only used for error reporting).  The routine then enters a loop until the end of the statement.

The loop begins by calling the get expression routine for a None data type to allow for print functions or an empty expression.  If a non numeric parser error is returned, then the appropriate error is returned.  This and other errors cause the loop exit.  If the done stack is not empty and the data type of the top item is None, then the print function flag is set to true.  Otherwise a new print code token is created and the process final operand routine is called to get the correct associated code for the top item and the print function flag is cleared.  The top item (either a print function or the new print code token) is dropped from the done stack (both have already been added to the RPN output list).  The separator flag is set, the last semicolon token is deleted and the pointer is set to null.

If the next token (the terminating token from the get expression routine) is a comma, then if the last semicolon token pointer is set, then a comma is not allowed and the appropriate error is set and the loop exits.  Otherwise, the comma token is appended to the RPN output list.  The last semicolon token is deleted and the pointer is set to null.  The separator flag is cleared (no semicolon is allowed).  The loop continues with the next expression.

If the next token is a semicolon, then if the separator flag is not set, then a second semicolon is not allowed and the appropriate error is set depending if the last semicolon token pointer is set and the loop exits.  The last semicolon token is deleted and the pointer is set to the current semicolon token.  The loop continues with the next expression.

For any other terminating token, the loop exits.  Otherwise, the separator flag is cleared for the next expression and the loop continues.  If the loop exited with an error, the last semicolon token and command token pointers are deleted and the error is returned.  If the last semicolon token pointer is set, the command token is deleted and is set to the last semicolon token.  The command token is appended to the RPN output list.

If the terminating token is not an end-of-statement token, then the appropriate error is returned depending on the print function flag.  Otherwise a done status is returned.  The PRINT translate function was implemented in the new basic/print.cpp source file, which was added the CMake build file, and was added to the table for the PRINT command.

[commit 3fc94d4b18]