Saturday, December 27, 2014

Table – More Alternate Codes

Since it will be ideal to reduce the number of unique code type enumerators needed, several uses of the current code enumerators was eliminated.  Most of these were accomplished by assigning the codes needed as alternate codes to commands that need them in the Alternate Info initializer list and obtaining the codes using the alternate map:
AssignLet (alternate 0)
Input BeginInput (alternate 0)
Input Begin StringInput Prompt (alternate 0)
Input AssignInput (alternate 1)
Input AssignInput Prompt (alternate 1)
Print DoublePrint (alternate 0)
PrintSemicolon (alternate 0)
Variable ReferenceVariable (alternate 1)
Since the command code is available in the translate function needing them (LET, INPUT, or PRINT) , it can be used to find the alternate code needed.  The Print code is assigned as an alternate to the Semicolon code for recreation (see below).  For a PRINT statement ending with a semicolon, no PRINT command code is stored in the program code so the semicolon recreate needs to also recreate the PRINT keyword.

The Input code enumerator was used in the common INPUT translate routine to determine if the routine was called for the INPUT or INPUT PROMPT command.  This test was changed to checking if the second name of the command token code is empty, which is empty for INPUT and not empty for INPUT PROMPT.

The Constant String code enumerator was used in the token equality operator function to determine if token string comparison should be case sensitive (for REM, REM operator and string constants) or case insensitive (variables, arrays, etc.).  This test was changed to checking if the token type is a constant and the data type is a string.

The Print code enumerator was used in the print recreate function instead of using the name from the table entry.  This was done because the print semicolon recreate function called this function after appending a semicolon.  If the table entry was used, a semicolon would have been output for the command instead of PRINT.  The function was changed to create a temporary RPN item with a temporary token with the Print code, but not the code enumerator using the alternate code.  This set of statements is convoluted, but will be far simpler once the new table model is implemented.

Unrelated to removing the use of code enumerators, it was noticed that the print comma recreate and print semicolon recreate functions were using the strings for a comma and semicolon directly but technically should have been using the name from the table entry.  Both of these were changed to use the table entry name.

[branch table commit b6850c7c7f]

Table – New Code Type Enumeration

With the current table model, each entry contains a token type that identifies the type of token that is created for the table entry.  Each entry has a code enumerator, which is simply used as an index to the entry.  The code (index) is put into the program code.  The code enumeration was originally automatically generated from comments next to the table entries.  This avoided mismatches between the code enumerators and the table entries (though this was a poor design choice).  Only some entries were identified using these code enumerators.

For the new table model, each table entry will be a unique class with a unique instance.  Each table entry instance will be assigned a unique index by the base table entry constructor.  Some table entries will still need to be found be some means by the parser and translator.  This could be done directly by referencing the table entry instance, but this would require exposing the derived table entry classes.  The plan is only to expose the base table entry class definition.

To find these small number of table entries, an enumeration will still be used.  This enumeration will be similar to the token type enumeration currently given to each table entry.  Therefore a single Code Type enumeration will be defined and will also replace the token type enumeration.  Unlike the current Code enumeration, table entries could have the same Code Type enumerator (for example, all of the six variable codes will be assigned the Variable code type).  Only the first table entry assigned to a code type will be returned for a code type enumerator; the others will be assigned as alternate codes of the first.

Code types will generally not be assigned to table entries for commands, operators and functions (which will no longer be referred to as internal functions) except for a few cases (for example, the LET command and the equal operator).  New table flags will be assigned instead.  There is already a Command table flag, and there will be Operator and Function table flags.  Each of the codes with operands (variables, arrays, constants, defined functions, user functions, and subroutines) will have a code type.  There will not be separate enumerators for tokens with and without parentheses (more on this later).