Thursday, June 3, 2010

Translator – Commas with PRINT

Currently a comma is not valid within an expression if the comma is not inside an array or function.  The comma has a low precedence, below all operators, but above end-of-line, close parentheses and commands, so it will empty all other operators from the hold stack. No lower precedence tokens will be on the hold stack except for an open parentheses, identifier with parentheses (function or array), function (internal or defined), or an assignment operator.

The comma token handler will be updated to check for a PRINT command on top of the command stack. Any other command will still cause an “unexpected comma in expression” error to occur. The hold stack needs to be checked to make sure it is empty (has the initial Null token on top), otherwise a “missing closing parentheses” error occurs. This check is the same that is performed for in semicolon token handler and also in the end-of-line token handler, so this code will be put into its own function for all to call. An assignment won't be on the hold stack if there is a PRINT on the command stack.

For a PRINT command, the handler will process any expression on top of the done stack. The expression on the done stack will be handled by the find code function as previously described. The flag for the PRINT command item on top of the done stack needs to be set in case this is the last comma in the PRINT statement, to tell the PRINT command handler not to append the Print code to the output (to keep the cursor on the same line).

If the done stack is empty, then there was no expression before the comma (allowed), which means to skip to the next column. The comma token will be appended to the output. For now, the PRINT command is the only reason a comma token is put into the translated output, therefore the execution routine called during run-time for the comma will be to advance to the next column. There is no need for a PrintComma code unless there will be another reason to put a comma in the program.