Java Reference
In-Depth Information
for(Ii'=Expression.iterator();i'.hasNext();){
TypeIdentifier=i'.next();
Statement
}
The variable i' is compiler generated in such a way as not to conict with any other variables.
Otherwise, Expression must have an array type T[]. In this case, our enhanced for- statement
may be expressed as
T[]a'=Expression;
for(inti'=0;i'<a'.length;i'++){
TypeIdentifier=a'[i'];
Statement
}
The variables a' and i' are compiler generated in such a way as not to conflict with any
other variables. This can be compiled similarly to the classic for- statement.
Exercise 5.9. Study the Java Language Specification [Gosling et al., 2005] to determine
what it would take to implement the continue-statement in your compiler. Then add the
continue-statement to j--, adding them to your compiler and testing it thoroughly.
Exercise 5.10. Study the Java Language Specification [Gosling et al., 2005] to determine
what it would take to implement the break-statement in your compiler. Then add the
break-statement to j--, adding it to your compiler and testing it thoroughly.
Exercise 5.11. Add conditional expressions to j--, adding them to your compiler and
testing them thoroughly. Conditional expressions are compiled in a manner identical to
if-else statements. The only difference is that in this case, both the consequent and the
alternative are expressions. For example, consider the assignment
z=x>y?x-y:y-x;
The javac compiler produces the following code for this:
43:iload_1
44:iload_2
45:if_icmple 54
48:iload_1
49:iload_2
50:isub
51:goto57
54:iload_2
55:iload_1
56:isub
57:istore_3
As for the if-then statement, we compile the Boolean test expression using the 3-argument
version of codegen() .
Exercise 5.12. Add the conditional or operator || to j--, adding it to your compiler and
testing it thoroughly. Make sure to avoid unnecessary branches to branches. The conditional
|| , like the conditional && , is short-circuited. That is, in
e 1 || e 2
if e 1 evaluates to true , then e 2 is not evaluated and the whole expression is true . For ex-
ample, javac compiles
 
Search WWH ::




Custom Search