Friday, December 25, 2009

Reading Stacks From Bottom to Top

Take the example of a PRINT statement as follows:

    PRINT A;TAB(20);B,C

I'm anticipating that this will be encoded into the internal language symbolically as follows:

    A 20 TAB B COMMA C PRINTNL

Note that semicolons are not encoded, but assumed between expressions.  There will also be two types of PRINT statements, one with an implied new-line (PRINTNL) and one that ends with a semicolons or commas to stay on the current line (PRINT).

When this command is executed, the expression stack will look as follows after all the parts are executed up to the PRINTNL command:

    A <-- bottom
    TAB(20)
    B
    COMMA
    C <-- top

When the PRINTNL is read, the PRINTNL command code will be called.  To print the items in order (i.e. A first), the stack will need to be read from from the bottom to the top.  The expressions and functions could be written into the internal language backwards so they will be on the stack in the correct order during run time (to pull each from the top), but this will complicate the translator and the recreator, plus the expressions would be evaluated backwards, an idea I'm not comfortable with.  Therefore, it's easiest to keep them in the same order as entered, hence the need to read the stack from the bottom to the top.