Friday, June 4, 2010

Translator – End of Statement Processing

The end-of-line processing currently performs the check if an expression was ended correctly, that is, checks if the hold stack contains only the initial null token (meaning all operator have been processed). The comma and semicolon should empty all operators from the hold stack, except lower precedence opening parentheses, functions, and array token. Except for the null token which acts as a buffer, if any of these are still on the stack, there is a missing closing parentheses. Finally, any pending parentheses needs to be processed.

This end of expression code needs to called by the comma and semicolon token handlers, so it will be moved into its own function (duplicating code is a bad programming practice). This function will also be called by future token handlers. In other words, it needs to be called at the end of a statement. The end-of-line is also the end of a statement, which currently contains this code.

However, the end of expression should not be called by the end-of-line token handler, which should instead be calling the command handler for the command that is currently being processed (on top of the command stack). The command handler will then do the end of expression call if required (some commands don't have expressions). The end-of-line token handler will pop the command from on top of the command stack and call the command's command handler.

Currently, when an assignment operator is appended to the output, the pointer to the output item is pushed to the done stack. There is no reason to push this to the done stack because it is not needed there (it is just popped at the end of the statement). The reason for pushing onto the done stack is so that the output item can be referenced by another token (for example, operands of functions for checking data types).

Remember the LET command was popped from the command stack (if there) by the assignment operator. The assignment operator, should instead be pushed onto the command stack since it is a command. It will be more clear why once the compound commands are implemented (like IF-THEN-ELSE). The assignment, being a command, will not get popped from the command stack (like all commands). The assignment command doesn't need a command handler, so it will have a NULL command handler function pointer.