Saturday, November 16, 2013

Recreator – PRINT Statements

There are several codes that make up a PRINT statement including print item (double, integer or string), comma, print function (TAB and SPC), semicolon (only at the end of the statement) and the print command.  A recreate function was implemented for each of these codes.  Because the PRINT statement is composed of several codes, the separator member variable of the recreator instance is used between the processing of these codes to keep track of separators between the print items.

As the codes of the PRINT statement are processed, the resulting PRINT statement is built up by adding to the string on top of the holding stack.  Generally, the string for the current item is popped from the stack, a separator is added to the string on top of the stack, which contains previous items and the string of the current item is added to the string of the previous items that is on top of the stack.  At the end of the statement, the PRINT keyword is added to the output string along with the built up string of the print items and separators that is popped from the stack.

Implementation

The print item recreate function contains a local string variable.  If the separator is set from a previous print code, the string is set to it.  If this separator is not a space, then a space is added after the separator.  The separator is a space if the last print code was a comma (see below).  The string on top of the stack is popped and added to the string.  If the stack is now empty, then the string is pushed to the stack, otherwise the string is added to the string on top of the stack.  The separator is set to a semicolon for the next item if there is one.

Spaces are normally added after a comma like a semicolon, but spaces are not added between multiple commas.  The print comma recreate function contains a local string variable.  If the holding stack is not empty, the string on top of the stack is popped into the local string.  A comma is added to the string (which is empty if the stack was empty, like when there is a comma directly after the PRINT keyword).  The local string is pushed to the stack.  The separator is set to a space.  A space is only added after the last consecutive comma.

The print function recreate function first calls the internal function recreate function (since print functions are translated the same as other internal functions), which will process the print function and its operand and leave the result on top of the stack.  The print item recreate function is called to process the print function like any other item.

The semicolon code is only found at the end of a PRINT statement and replaces the print command code.  The print semicolon recreate function pops the string from the holding stack, adds a semicolon, and pushes it back to the stack.  The print recreate function is called to complete the PRINT statement.

The print recreate function adds the PRINT keyword to the output string.  If the holding stack is not empty, a space is added to the output string, and the string is popped from the stack and added to the output string.

A new separator is set access function was added to the recreator to a specific character, which is used by the print item recreate function.  Pointers to the new print recreate functions were added to the table entries of the various codes.  The expected recreated outputs for translator test #6 (PRINT statements) were updated and now recreated correctly.

[commit 1d3a05f610]

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