Before working on the design of the routines that will handle the matching of the data types and finding the correct code, I decided to make the changes to the table entries for the new operand data type (operand_datatype) array and associated code (assoc_code) array and make sure these compile. The number of arguments member (nargs) was also renamed to the number of operands (noperands). New access functions for these members were added to the Table class.
All the table entries for the operators and internal functions were updated with the new values. The number of operands (previously nargs) for all of the operators were previously set to zero, so these were changed to 1 for the unary or 2 for the binary operators. The data type for many operators were also incorrectly set to None, so these was changed to the appropriate data type.
New entries for all the associated codes were added to the table and their codes were added to the Code enumeration. An entry for the CDBL functions was missing and was added. An entry for a new function FRAC was also added (this function will return the fractional part of a floating point values).
The integer division operator (“\” or IntDiv) is intended to be used with double operands, which will be rounded and converted to integers internally before the division. The operand data types could have been set to the integer data type, but then the CvtInt would always be inserted for both operands. This operator only has one code and should not be used with integer operands as the regular division should be used, but this will be allowed (CvtDbl will be inserted, but only to be converted back to integers internally).
The Power operator will have three forms, the standard double/double (Power) and integer/integer (PowerInt), but there will also be a double/integer code (PowerMul). The PowerInt will use integer multiplication internally. The idea for the PowerMul code is for it to also use multiplication internally instead of calling the standard C pow() function for speed. However, the pow() function may already have these optimizations, so this may be unnecessary. Some experiments will be needed to determine which is more efficient. The goal is to allow the programmer to use an expression like A^2 instead of A*A and not be penalized by slow execution.
Saturday, April 24, 2010
Translator – Associated Codes
Operators and some internal functions allow different operand data types, each will have it's own code (e.g. Add, AddInt, and CatStr), but only one name in the source (e.g. “+”). The Parser will only find the first one in the table since the Parser is only responsible for breaking the source into tokens, not looking at data types and determining appropriate codes. It is the Translator's responsibility to examine and validate the operands of operators and internal functions and to set the token to the appropriate code.
To accomplish this, the first entry in the table (that the Parser finds, the default entry), which for many will be the code that handles the double data type, will contain the other codes associated to the main code. These codes will be put into an associated code array in the table entry.
After obtaining the data types of the operands from the done stack, the Translator will check to see if there is a match to the code by checking the required operand data types in the code's table entry. If there is no match, then each of the associated code table entries will be checked for a match.
In addition to an exact match, there will also be a convertible match. With this type of match, hidden conversion codes can be inserted to obtain the desired data types. The exact match will be preferred so that the correct code is used. For example, if the first exact match or convertible match is used, then for Integer + Integer, the Translator would use the default Add code (for doubles) and add two CvtInt instead of using the preferred AddInt code. Next, how a convertible match will be detected...
To accomplish this, the first entry in the table (that the Parser finds, the default entry), which for many will be the code that handles the double data type, will contain the other codes associated to the main code. These codes will be put into an associated code array in the table entry.
After obtaining the data types of the operands from the done stack, the Translator will check to see if there is a match to the code by checking the required operand data types in the code's table entry. If there is no match, then each of the associated code table entries will be checked for a match.
In addition to an exact match, there will also be a convertible match. With this type of match, hidden conversion codes can be inserted to obtain the desired data types. The exact match will be preferred so that the correct code is used. For example, if the first exact match or convertible match is used, then for Integer + Integer, the Translator would use the default Add code (for doubles) and add two CvtInt instead of using the preferred AddInt code. Next, how a convertible match will be detected...
Friday, April 23, 2010
Translator – Operand Data Types
Now it has been established that operators and internal functions can be handled the same way, except that the number of arguments (operands) is checked first for internal functions. For internal functions, the number of arguments check also determines which code to use (MID2 vs. MID3, INSTR2 vs. INSTR3, or ASC vs. ASC2).
Each code (operator or internal function) has a return data type, a fixed number of operands and a expected data type for each operand. The word operand will be used instead of argument from now on, which is more appropriate when applied to operators. Taking the plus operator as an example, which will have three codes (one main and two associated codes):
The table entries already contain the code and data type values and the number of arguments will be renamed to the number of operands. A new operand data type array needs to be added to the table entries. The size of this array will be set to three since there are currently no planned functions containing more than three arguments. Next, how associated codes will be handled...
Each code (operator or internal function) has a return data type, a fixed number of operands and a expected data type for each operand. The word operand will be used instead of argument from now on, which is more appropriate when applied to operators. Taking the plus operator as an example, which will have three codes (one main and two associated codes):
Add Double (2) Double DoubleThe code is listed first along with the return data type. The number of operands is in parentheses followed by the data type of each operand. If there is an add with a double and an integer operand, the Add code will be used with a CvtInt inserted after the integer operand.
AddInt Integer (2) Integer Integer
CatStr String (2) String String
The table entries already contain the code and data type values and the number of arguments will be renamed to the number of operands. A new operand data type array needs to be added to the table entries. The size of this array will be set to three since there are currently no planned functions containing more than three arguments. Next, how associated codes will be handled...
Thursday, April 22, 2010
Translator – Operators and Internal Functions
Unary operators have one operand. When the translation to RPN of unary operators is compared to the translation of functions with one argument, it can be seen that the translations are the same (one operand followed by the code):
-A → A NegBinary operators have two operands. When the translation to RPN of binary operators is compared to the translation of functions with two arguments, it can be seen that the translations are the same (two operands followed by the code):
ABS(A) → A Abs
A$+B$ → A$ B$ CatStrThere are no tertiary or more operators, but functions with three of more arguments have a similar format except there would be more operands before the code. So, once the source code for operators and internal functions are translated to RPN, there is no difference between them - operand(s) followed by a code. Therefore, for data type handling, both operators and functions can be handled the same way. Next, how data types will be handled...
LEFT$(A$,2) → A$ 2 Left
Wednesday, April 21, 2010
Translator – Data Types and Internal Functions
The internal functions have a fixed number of arguments, though some functions have multiple forms with different number of arguments (MID$, INSTR and ASC). Each argument has a specific data type expected, however, integer and double data types are interchangeable as the necessary conversion will be perform like for operators.
Most of the math functions (e.g. INT, SQR, LOG, COS, etc.) have one argument, which is expected to be a double. These functions return a double. Only one code is required for these functions. If the argument is an integer, then a hidden CvtDbl code will be inserted after the integer operand. Here are some RPN examples of math functions:
The conversion functions (CDBL and CINT) return the opposite date type as their operand. These functions work the same is the hidden CvtDbl and CvtInt codes. It would appear that these functions are unnecessary since the hidden conversion codes will be inserted as needed. However, they may be reasons where they are necessary like in function and subroutine calls.
The string functions deal with the string data type (implementation will be delayed along with the string data). Some string functions take an integer operand (CHR$ and SPACE$) or double operand (STR$) and produce a string, some take a string operand and produce an integer (ASC and LEN) or double (VAL), and some take both string and integer operands and produce a string (LEFT$, MID$, RIGHT$, and REPEAT$) or an integer (ASC and INSTR).
Next, the similarity between operators and internal functions...
Most of the math functions (e.g. INT, SQR, LOG, COS, etc.) have one argument, which is expected to be a double. These functions return a double. Only one code is required for these functions. If the argument is an integer, then a hidden CvtDbl code will be inserted after the integer operand. Here are some RPN examples of math functions:
A SqrA few of the math functions (ABS and SGN), will return the same type as there operand. Two codes are required for these functions: Abs, AbsInt, Sgn, and SgnInt. The reason for having two codes is so that these functions can be used in integer expressions without any wasteful conversions to and from double precision. This is not necessary for the other math function because they will be calculated in double precision internally.
B% CvtDbl Log
The conversion functions (CDBL and CINT) return the opposite date type as their operand. These functions work the same is the hidden CvtDbl and CvtInt codes. It would appear that these functions are unnecessary since the hidden conversion codes will be inserted as needed. However, they may be reasons where they are necessary like in function and subroutine calls.
The string functions deal with the string data type (implementation will be delayed along with the string data). Some string functions take an integer operand (CHR$ and SPACE$) or double operand (STR$) and produce a string, some take a string operand and produce an integer (ASC and LEN) or double (VAL), and some take both string and integer operands and produce a string (LEFT$, MID$, RIGHT$, and REPEAT$) or an integer (ASC and INSTR).
Next, the similarity between operators and internal functions...
Tuesday, April 20, 2010
Translator – Data Types and Operators
Three data types are currently planned: double precision (the default), integers and strings. Several operators accept all three as operands (plus, equality, relational and assignment). Consider the different possible operand combinations for the plus operator (the default data type is double, % is for integers and $ is for strings):
If the plus operator was implemented as a single routine, at run-time it would have to look at the operands and then decide what action to take, whether to add two doubles, whether to convert one operand or the other from integer to double and then add two doubles, whether to add two integers, or whether to concatenate two strings. All this decision making will slow execution.
These decisions will of course be made before run-time by the Translator. The Translator will know which operation will be needed at run-time, so it will add the appropriate code to the RPN list. Since there are five different possible combinations, that would mean five different add operator codes. If this same thing was implemented for every operator, then the number of codes and the number of execution routines would be excessive.
There are really only three operations, adding doubles, adding integers and concatenating strings. For the other two combinations, one or the other operand needs to be converted from integer to double. To accomplish this, there will be special hidden conversion codes (which have already been mentioned in several earlier posts). With these hidden codes, the above expressions would be converted to RPN as:
A + BAny combination between a number and a string is invalid and an error needs to be reported. However, doubles and integers may be mixed in an expression. The expression will remain integer for efficiency as long as there are integer operands. In a double expression, integers will be promoted (converted) to double.
A + J%
I% + B
I% + J%
S$ + T$
If the plus operator was implemented as a single routine, at run-time it would have to look at the operands and then decide what action to take, whether to add two doubles, whether to convert one operand or the other from integer to double and then add two doubles, whether to add two integers, or whether to concatenate two strings. All this decision making will slow execution.
These decisions will of course be made before run-time by the Translator. The Translator will know which operation will be needed at run-time, so it will add the appropriate code to the RPN list. Since there are five different possible combinations, that would mean five different add operator codes. If this same thing was implemented for every operator, then the number of codes and the number of execution routines would be excessive.
There are really only three operations, adding doubles, adding integers and concatenating strings. For the other two combinations, one or the other operand needs to be converted from integer to double. To accomplish this, there will be special hidden conversion codes (which have already been mentioned in several earlier posts). With these hidden codes, the above expressions would be converted to RPN as:
A B AddThe Add handles doubles, AddInt handles integers, and CatStr (concatenate) handles strings. The CvtDbl code converts it's integer operand to double. For now, the focus will be on doubles and integers; strings require more involved handling and will be added later. Next, data types and internal functions...
A J% CvtDbl Add
I% CvtDbl B Add
I% J% AddInt
S$ T$ CatStr
Sunday, April 18, 2010
Project – Processes and Insights
The transition to CVS is almost complete (two releases remaining to be put into the repository). While it would have been far simpler just to throw to whole project into CVS as is, I wanted the complete history for each release in the CVS repository as if CVS had been used from the beginning. So this would be a good time to give some insight into the development process of this project.
Most days there is only an hour or two to work of this project either on the code or write blog entries, more on the weekends, sometimes less depending on the demands of my full time paying job and my family. The is an attempt to post at least once a day on what is being worked on or on the design of upcoming components, but some days the code is being worked on and there is no time left to write and proofread a post. Sometimes several entries are written at a time (because there is too much for one post), but wait to post each entry to give some time to work on the code.
The design is ongoing, and is actually being worked on ahead of what is being worked on in the actual code. A lot of notes were made on the design almost six months prior to the first post last December, when the decision was made to actually write code and document the effort in a blog. Many of these notes (like on the Recreator design) have yet to be put to code. It is very helpful to explain the design and the reasons behind decisions in the posts. Many problems are identified during this process. The design notes for future components continue to be made (now in a bound notebook instead of loose pieces of paper).
The parallel development (code and future design) process is the reason why there are few major changes to what has been developed so far. This will be become even more evident when commands are implemented in the Translator. Thought needs to be given to how the command will be executed at run-time, the format of the command when it is encoded into the internal code, and what is needed to recreate the source code from the internal code for editing. Now back to the CVS transition and on to data type handling...
Most days there is only an hour or two to work of this project either on the code or write blog entries, more on the weekends, sometimes less depending on the demands of my full time paying job and my family. The is an attempt to post at least once a day on what is being worked on or on the design of upcoming components, but some days the code is being worked on and there is no time left to write and proofread a post. Sometimes several entries are written at a time (because there is too much for one post), but wait to post each entry to give some time to work on the code.
The design is ongoing, and is actually being worked on ahead of what is being worked on in the actual code. A lot of notes were made on the design almost six months prior to the first post last December, when the decision was made to actually write code and document the effort in a blog. Many of these notes (like on the Recreator design) have yet to be put to code. It is very helpful to explain the design and the reasons behind decisions in the posts. Many problems are identified during this process. The design notes for future components continue to be made (now in a bound notebook instead of loose pieces of paper).
The parallel development (code and future design) process is the reason why there are few major changes to what has been developed so far. This will be become even more evident when commands are implemented in the Translator. Thought needs to be given to how the command will be executed at run-time, the format of the command when it is encoded into the internal code, and what is needed to recreate the source code from the internal code for editing. Now back to the CVS transition and on to data type handling...
Saturday, April 17, 2010
Translator – Assignments (Release)
The test code was modified to output “<ref>” after tokens that have the reference flag set. For the third set of test inputs used for testing arrays and function, there are some differences because the reference flag is now set for array subscripts and function arguments. The unexpected comma error was also changed (replaced with separate unexpected comma errors). For the assignment statement test inputs (fifth set), a lot of different statements were needed to test the many situations that can occur. Many have already been discussed. As bugs were discovered, new test statements were added. There are 9 new possible assignment related errors, so there are statements for each of these.
Some more comma related bugs were discovered, including assigning an multiple dimension array element as in the statement “A(B,C)=D” added an assign list operator instead of an assign operator; and the statement “A(B+C,D=E)=F” generated an error. The bottom line was that the counter stack not empty check also needed to be added to the comma operator code. This resulted in new error “unexpected comma in parentheses” being added.
I'm changing the way the changes made to the code are dated. Previously, all the dates were changed to the date of the release – like all these changes were made the same day, which may not have been the case. The changes are becoming rather involved over many days and this method is not efficient. From this release on, the date will be when the changes are actually made. The time stamp of the file may be more recent because of going back and editing the change history at the top of the file.
I have also decided to put this project under software version control, specifically CVS – with the next release. I have working knowledge of CVS and I discovered that either MSYS came with it, or I installed an MSYS/CVS package. The current version numbers in the source files will be removed and replaced with the CVS revision ID tag. These versions were my attempt at version control anyway. Maintaining all the difference versions as zip files and directories is also not efficient.
The code now handles assignment statements and ibcp_0.1.8-src.zip has been uploaded at Sourceforge IBCP Project along with the binary for the program. The release notes now contain a Planned Roadmap to show upcoming development. Next the handling of data types...
Some more comma related bugs were discovered, including assigning an multiple dimension array element as in the statement “A(B,C)=D” added an assign list operator instead of an assign operator; and the statement “A(B+C,D=E)=F” generated an error. The bottom line was that the counter stack not empty check also needed to be added to the comma operator code. This resulted in new error “unexpected comma in parentheses” being added.
I'm changing the way the changes made to the code are dated. Previously, all the dates were changed to the date of the release – like all these changes were made the same day, which may not have been the case. The changes are becoming rather involved over many days and this method is not efficient. From this release on, the date will be when the changes are actually made. The time stamp of the file may be more recent because of going back and editing the change history at the top of the file.
I have also decided to put this project under software version control, specifically CVS – with the next release. I have working knowledge of CVS and I discovered that either MSYS came with it, or I installed an MSYS/CVS package. The current version numbers in the source files will be removed and replaced with the CVS revision ID tag. These versions were my attempt at version control anyway. Maintaining all the difference versions as zip files and directories is also not efficient.
The code now handles assignment statements and ibcp_0.1.8-src.zip has been uploaded at Sourceforge IBCP Project along with the binary for the program. The release notes now contain a Planned Roadmap to show upcoming development. Next the handling of data types...
Translator – Assignments and Parentheses
Simply changing the mode to Expression when an open parentheses occurs is insufficient. Statements like “A(B,C)=D” no longer worked correctly because the mode was not set to Command when the equal was processed. Also, statements like “A(B+C)=D” and especially “A(B=C)=D” need to have the mode temporarily set to Expression to process the operators in the subscripts correctly. Simply checking if the counter stack is not empty is sufficient for detecting this situation. The logic for open parentheses needs to be:
The counter stack is not empty check also needs to be made when processing operators before checking the mode. In other words, if the counter is not empty, then it is assumed that the Translator is within an expression. This will handle statements like “A(B+C)=D” and “A(B=C)=D” correctly. So this check is needed at both the equal operator section and the no special operator section. Looks like everything is working correctly, so almost ready to release...
Counter Stack Not Empty: No need to check mode; push open parentheses token on hold stack and push a 0 on the counter stack (which prevents commas).
Command: Return an “unexpected parentheses in command” error. Statements like “(A=B)” and “(A+B)” are not valid.
Equal: Set mode to Expression. This is the start of the expression after an equal (assignment). This will handle statements like “A=(B=3)” and “A=(B)=3” where the expression starts after the open parentheses.
Comma: Return an “unexpected parentheses in assignment list” error. Statements like “A,(B),C=4” are not valid.
Expression: Same as if counter stack is not empty (push token and 0).When processing a closing parentheses for an open parentheses (not an array or function), the reference flag of the last token added to the output list needs to be cleared. A pointer to this token is on top of the done stack. If this token is an operator like in the expression “(A+B)” then clearing the reference flag has no effect, but in statements like “A=(B)=3” or “A=Function((B),C)” the reference flag of B is cleared.
The counter stack is not empty check also needs to be made when processing operators before checking the mode. In other words, if the counter is not empty, then it is assumed that the Translator is within an expression. This will handle statements like “A(B+C)=D” and “A(B=C)=D” correctly. So this check is needed at both the equal operator section and the no special operator section. Looks like everything is working correctly, so almost ready to release...
Friday, April 16, 2010
Translator – Assignments (Testing)
While testing comma separated assignment statements, an “unexpected character” error for statements like “A,B,C=4” was occurring from the Parser. This occurred because the Parser was seeing the line as an immediate command and expected a number for B. The Parser was modified to not return errors for these since these can be valid immediate statements.
Before testing the previous Translator tests that consisted of only expressions (which are not normally valid by themselves and would now cause “unexpected operator” errors), the test code needed to be modified for a special expression test mode that would be set for the first four sets of test expressions only. The Translator start() function was modified to optionally initialize the mode to Expression instead of Command.
The reference flags also need to be cleared for the arguments of internal functions as values are needed during run-time. This does not apply to define or user functions as the arguments are planned to be passed by reference by default. Surrounding a variable with parentheses will override this and pass the variable by value. For array subscripts, values are needed during run-time. Remember that the Translator does not know the difference between arrays and user functions, therefore, the reference flag will be left set for array subscripts along with function arguments. The Encoder can clear the reference flags for array subscripts once an array is identified.
While testing, I realized that the mode needed to be changed to Expression when an open parentheses occurs. Statements like “A=(B=3)” and “A=(B)=3” are single assignments and the second equals are the equality operator. Testing continues...
Before testing the previous Translator tests that consisted of only expressions (which are not normally valid by themselves and would now cause “unexpected operator” errors), the test code needed to be modified for a special expression test mode that would be set for the first four sets of test expressions only. The Translator start() function was modified to optionally initialize the mode to Expression instead of Command.
The reference flags also need to be cleared for the arguments of internal functions as values are needed during run-time. This does not apply to define or user functions as the arguments are planned to be passed by reference by default. Surrounding a variable with parentheses will override this and pass the variable by value. For array subscripts, values are needed during run-time. Remember that the Translator does not know the difference between arrays and user functions, therefore, the reference flag will be left set for array subscripts along with function arguments. The Encoder can clear the reference flags for array subscripts once an array is identified.
While testing, I realized that the mode needed to be changed to Expression when an open parentheses occurs. Statements like “A=(B=3)” and “A=(B)=3” are single assignments and the second equals are the equality operator. Testing continues...
Thursday, April 15, 2010
Translator – Assignment Operator Implementation
The reference flag for the first operand of an assignment operator or the list of operands of an assignment list operator will be checked in the add_operator() function before these operators are added to the output list. These operator remain on the hold stack until the end of statement processing since the precedence of these operators is low.
When the expected reference flag is not set, a “cannot be assigned” error will occur. The error needs to point to the token that is not a reference, not to the assignment operator token. Therefore, the add_operator() function's argument was changed to be a reference to the token pointer so that the a different token can be returned upon an error.
For the assignment list operator, the error should point to the first token in the line that is not a reference. There could be more than one token in the list that is not a reference, but only the first one will be reported. Because the tokens are pulled from the done stack in reverse order, the last one pulled that is not a reference is the token that needs to be reported. There will be a bad token pointer that will be set for each non-reference token. Once the stack is empty, if there was a non-reference, the bad token pointer will be pointing to the first non-reference token in the statement.
Upon modifying the code to return the bad token, I realized there was a problem doing this. Previously the code that calls the add_token() function deleted the token with the error since it had not been added to the output list. The tokens in the output list are deleted by the clean_up() function called after an error occurs. Before the Translator returned a different token on an error, it deleted the token passed to the add_token() function. This is where the problem occurred; the caller would delete the error token coming back unconditionally. If it was a token in the output list, it would be deleted twice (once by the caller, once by the clean_up function). This was corrected in the caller by checking if the original token was passed back and only then deleting the token.
When the expected reference flag is not set, a “cannot be assigned” error will occur. The error needs to point to the token that is not a reference, not to the assignment operator token. Therefore, the add_operator() function's argument was changed to be a reference to the token pointer so that the a different token can be returned upon an error.
For the assignment list operator, the error should point to the first token in the line that is not a reference. There could be more than one token in the list that is not a reference, but only the first one will be reported. Because the tokens are pulled from the done stack in reverse order, the last one pulled that is not a reference is the token that needs to be reported. There will be a bad token pointer that will be set for each non-reference token. Once the stack is empty, if there was a non-reference, the bad token pointer will be pointing to the first non-reference token in the statement.
Upon modifying the code to return the bad token, I realized there was a problem doing this. Previously the code that calls the add_token() function deleted the token with the error since it had not been added to the output list. The tokens in the output list are deleted by the clean_up() function called after an error occurs. Before the Translator returned a different token on an error, it deleted the token passed to the add_token() function. This is where the problem occurred; the caller would delete the error token coming back unconditionally. If it was a token in the output list, it would be deleted twice (once by the caller, once by the clean_up function). This was corrected in the caller by checking if the original token was passed back and only then deleting the token.
Monday, April 12, 2010
Translator – Reference Flag Implementation
For non-parentheses tokens, only the NoParen and DefFuncN token types should have the reference flag set. The NoParen token type includes a possible variable or user function with no arguments. The reference flag should not be set for internal functions with no arguments (IntFuncN) or constants. At the moment it's unclear if the reference flag should be set for defined functions with no arguments (DefFuncN). Intuition says that the reference flag should be set since the DEF command line is similar to an assignment statement.
For parentheses tokens, only the Paren (an array or user function with arguments) and DefFuncP token types should have the reference flag set. For now the reference flag will be set for define functions with arguments (same reason as above). The reference flag should not be set for internal functions with arguments (IntFuncP). However, with that said (and this is jumping ahead a bit), some internal functions, namely string functions, could be references. Specifically the MID$ function, which could be used to assign a part of a string. This will be implemented later when data type handling and string handling is added.
Lastly, in the test code that outputs the tokens, a “<ref>” will be output after the token if the reference flag of a token is set. This check will be made for all token types just to make sure it is not getting set when it's not supposed to.
For parentheses tokens, only the Paren (an array or user function with arguments) and DefFuncP token types should have the reference flag set. For now the reference flag will be set for define functions with arguments (same reason as above). The reference flag should not be set for internal functions with arguments (IntFuncP). However, with that said (and this is jumping ahead a bit), some internal functions, namely string functions, could be references. Specifically the MID$ function, which could be used to assign a part of a string. This will be implemented later when data type handling and string handling is added.
Lastly, in the test code that outputs the tokens, a “<ref>” will be output after the token if the reference flag of a token is set. This check will be made for all token types just to make sure it is not getting set when it's not supposed to.
Sunday, April 11, 2010
Translator – Single Vs. Multiple Assignments
Some issues came up during the implementation of the assignment handling. A new Assign code is needed for assignment operator. The original plan was that this operator would handle both single and multiple assignments. At run-time, it would simply keeping popping references off of the evaluation stack and assign the value until the stack is empty. However, this requires a check if the stack is empty. The goal is to reduce the number of checks that is needed at run-time because the more checks made, the more time wasted doing these checks during program execution.
Since a single assignment will be the common case and multiple assignment the exception, the single assignment execution time would be penalized by the extra stack empty check To prevent this, two assignment codes are needed, Assign for single assignment and AssignList for multiple assignment.
To implement two assignment codes in the Translator, at the first equal, the token will be changed to Assign as previously planned. If the mode is Equal (indicating another equal in a multiple equal assignment), the Assign token on top of the hold stack will be changed to AssignList. If the mode is Comma, then the token will be changed to AssignList before pushing the token onto the hold stack. At the time the assignment token is added to the output list, the Assign operator will expect two operands (the first a reference) and the AssignList operator will expect multiple reference operands and the value to assign operand.
This brings up another issue. The Recreator will need to distinguish if the AssignList is from a multiple equal or comma separated multiple assignment so that the original code is recreated as it was entered. There could be two different AssignList codes for each (for a total of three assignment operators). Though not implemented yet, there will also need to be unique codes each assignment if the optional LET command word is entered (that's now 6 unique assignment codes). All these unique codes would not be required at run-time (only two are needed, single or multiple). There's actually a better way to prevent this multiplying of codes, but this will wait until the internal code is developed (and it will also eliminate the need for the dummy parentheses codes in the program, but not all the code needed to determine when it is needed).
Since a single assignment will be the common case and multiple assignment the exception, the single assignment execution time would be penalized by the extra stack empty check To prevent this, two assignment codes are needed, Assign for single assignment and AssignList for multiple assignment.
To implement two assignment codes in the Translator, at the first equal, the token will be changed to Assign as previously planned. If the mode is Equal (indicating another equal in a multiple equal assignment), the Assign token on top of the hold stack will be changed to AssignList. If the mode is Comma, then the token will be changed to AssignList before pushing the token onto the hold stack. At the time the assignment token is added to the output list, the Assign operator will expect two operands (the first a reference) and the AssignList operator will expect multiple reference operands and the value to assign operand.
This brings up another issue. The Recreator will need to distinguish if the AssignList is from a multiple equal or comma separated multiple assignment so that the original code is recreated as it was entered. There could be two different AssignList codes for each (for a total of three assignment operators). Though not implemented yet, there will also need to be unique codes each assignment if the optional LET command word is entered (that's now 6 unique assignment codes). All these unique codes would not be required at run-time (only two are needed, single or multiple). There's actually a better way to prevent this multiplying of codes, but this will wait until the internal code is developed (and it will also eliminate the need for the dummy parentheses codes in the program, but not all the code needed to determine when it is needed).
Translator – More Mode Processing
There is some mode mode processing needed. When a non-assignment operator is added to the output, the mode will determine how the operator token is processed:
Command: An operator is not expected so an “unexpected operator” error occurs.
Equal: Within an assignment (possibly multiple). The mode will be changed to Expression indicating the start of the expression to assign.
Comma: Within a comma separated list of a multiple assignment, an operator is not expected so an “expected comma or equal” error occurs.
Expression: Within an expression, operator is processed as previously implemented.At the end of the line, some checks needs to be made based on mode to see if any error has occurred:
Comma: Within a comma separated list of a multiple assignment, but there was no assignment operator, so an “incomplete assignment” error occurs.No errors are detected for the other modes at the current time. Now on to implementation of assignment statements...
Saturday, April 10, 2010
Translator – Comma Token Processing
Some comma token processing has already been implemented – commas separating subscripts of arrays or arguments of functions. Commas outside of arrays or functions caused an “unexpected comma” error. This processing needs to be extended to include comma separated multiple assignment statements. Again the current mode will determine how the Translator processes comma operator tokens:
Command: The beginning of a comma separated multiple assignment. The mode will be changed to Comma indicating a comma separated multiple assignment. The comma token is not needed, so it can be deleted. Eventually an assignment operator will be pushed on the hold stack.
Equal: Continuation of a multiple equal assignment statement, so a comma token at this point cause an “unexpected comma in assignment” error.
Comma: Continuation of a comma separated list of a multiple assignment. No further action is needed so the comma token can be deleted.
Expression: Within an expression, so proceeds with the previously implemented comma processing. When the counter stack is empty or the top counter has a zero value, an “unexpected comma in expression” error occurs (changed from “unexpected comma” to differentiate it from the “unexpected comma in in assignment” error).The comma token has the same low precedence as closing parentheses and assignment operators so most tokens will be removed from the hold stack. Comma tokens are not pushed to the hold stack so they won't be removed. Closing parentheses are also not pushed to the hold stack. And there will never be an assignment operator pushed to the stack before a comma token is processed.
Translator – Equal Token Processing
The Parser returns an equality operator token for an equal. The current mode will determine how the Translator processes equality operator tokens:
Command: The token will be changed to an assignment operator and the mode will be changed to Equal indicating a possible multiple equal assignment and to prevent a comma separated assignment. The assignment operator will be pushed onto the hold stack.
Equal: Continuation of a multiple equal assignment statement. There is already an assignment operator on the hold stack, so this token can be deleted. No further action is needed. During run-time, the single assignment operator will handle multiple variable references.
Comma: The end of a comma separated list of a multiple assignment. The token will be changed to an assignment operator and the mode will be changed to Expression indicating the start of the expression being assigned. The assignment operator will be pushed onto the hold stack.
Expression: Within an expression, equals are equality operators, so the token (already an equality operator) will be processed as a regular binary operator.The assignment operator will have a very low precedence, which will keep on the hold stack until the end of the statement. Its precedence can be the same as the closing parentheses. The assignment token will be first on the hold stack, so there won't be a closing parentheses token to remove. The closing parentheses only empties the hold stack to an opening parentheses or internal function token (with a lower precedence), so there won't see an assignment token.
Subscribe to:
Posts (Atom)