Home
 

Multiple Assignments

Multiple Assignments

Multiple assignments are defined as follows (see the section called parser.y in Appendix B).

AssignmentStmt:
    AssignmentSeq Expr
    {$$ = new AssignmentStmtCls($1,$2,textline);}
    ;

AssignmentSeq:
    AssignmentSeq DataItem ASGTK
        {$$ = PAssignmentSeqCls($1)->append($2);}
    | DataItem ASGTK
        {$$ = new AssignmentSeqCls($1);}
    ;


An assignment consists of a sequence of data items (see Chapter 2) and an expression.

Code generation works as follows. First we put all data items onto the stack. Then we put the expression onto the stack. After that, we create as many assignment operations as there are data items. Finally, we pop of the expression from the stack. The code for this follows below (see also the section called emit.C in Appendix B).

int AssignmentStmtCls :: emit() {
   if (debugFlag) cout << "AssignmentStmtCls::emit()" << endl;
   StatementCls::emit();

   assign_seq->emit();
   expr->emit();
  
   for (int i=0; i<PAssignmentSeqCls(assign_seq)->getLength(); i++)
      cout << ":=" << endl;
  
   cout << "pop" << endl;
  
   return 0;
}