Wednesday, February 16, 2011

Translator – INPUT Command – New Problem

A few new ideas for the INPUT command have been developed that may simplify the execution of the INPUT command and work better than traditional implementation (more on this in a bit). Upon reviewing the previous posts on the INPUT command and reading the ANSI Standard document for the INPUT command again, a problem was discovered with the previous design. The problem can be shown with this INPUT statement:
INPUT I%,A(I%)
This is probably bad programming - if the value entered is outside the range of the array, an exception occurs. In any case, it is allowed. Both GW-Basic and QBASIC act as expected where the first value entered becomes the index of the element that is assigned the second value.  However, the design laid before won't act as expected. Consider the planned translation for this statement:
InputGet I%<ref> InputInt I% A(<ref> InputDbl Input
This will not work, consider the items pushed to the evaluation stack upon reaching the Input code at the end:
  1. Input information (prompt is specified, question flag, and location)
  2. I%<ref>
  3. Parsed integer value from input
  4. Pointer to the integer assign routine
  5. A(I%)<ref> (after I% pushed, popped and reference to A(I%) calculated)
  6. Parsed double value from input
  7. Pointer to the double assign routine
Notice in item 5 that the value of I% used to calculated the reference to A(I%) would not be the value of I% just parsed from the input because I% has not been assigned yet, which would occur after all of the input has been parsed and found to be valid. Making the implementation difficult are two rules that must be followed:
  1. Can't put references to the evaluation stack until previous values have been assigned.
  2. Can't assign any variables to the input values until the entire input has been parsed and validated.