16.4. Assignments

As long as we're this close, we might as well create the code to deal with an assignment statement. This code needs only to remember the name of the target variable where we are to store the result of an expression, call Expression, then store the number. The procedure is shown next:

{ Parse and Translate an Assignment Statement }
procedure Assignment;
var Name: string;
        Name := GetName;

The assignment calls for yet another code generation routine:

{ Store the Primary Register to a Variable }
procedure StoreVariable(Name: string);
        EmitLn('LEA ' + Name + '(PC),A0');
        EmitLn('MOVE D0,(A0)');

Now, change the call in Main to call Assignment, and you should see a full assignment statement being processed correctly. Pretty neat, eh? And painless, too.

In the past, we've always tried to show BNF relations to define the syntax we're developing. I haven't done that here, and it's high time I did. Here's the BNF:

<factor>      ::= <variable> | <constant> | '(' <expression> ')'
<signed_term> ::= [<addop>] <term>
<term>        ::= <factor> (<mulop> <factor>)*
<expression>  ::= <signed_term> (<addop> <term>)*
<assignment>  ::= <variable> '=' <expression>