Home
 

Array subscripts

Array subscripts

Array elements are data items. The grammar looks as follows (see also the section called parser.y in Appendix B).

DataItem:
    Identifier
        {$$ = new DataItemCls($1);}
    | DataItem LBRACKTK Expr RBRACKTK
        {$$ = new DataItemCls($1, $3);}
    ;


Code generation works as follows. For data items that are simple identifiers we put the address onto the stack. If we have a subscript we assume that the address we have put onto the stack is the base address; therefore we put the expression onto the stack (by emitting it) and add it to the base address. The stack now contains the address of the specified array element.