[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]
-
[Honesty]
[Instructions]
[Links]
[Search]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Project]
[Readings]
[Reference]
Misc:
[2001S]
[2002F]
[SamR]
Held: Wednesday, 21 April 2004
Summary: Today we consider how to translate conditionals.
Related Pages:
Overview:
and
and or
.
and
is false, do
you need to evaluate the second?
or
is true, do
you need to evaluate the second?
and
and or
, your language supports
short-circuit evaluation.
and
and or
, your language supports
strict evaluation.
code-for-expression Jump-if-Zero location-for-expression-result label-for-falsepart Jump label-for-truepart label-for-truepart code-for-truepart Jump label-for-end label-for-falsepart code-for-falsepart Jump label-for-end label-for-end
code-for-short-circuit-expression Jump label-for-truepart label-for-truepart code-for-truepart Jump label-for-end label-for-falsepart code-for-falsepart Jump label-for-end label-for-end
/* Translate a less-than expression. */ translate(LessThan(Exp left, Exp right), truelabel, falselabel) CodeLoc l = translate(left); CodeLoc r = translate(right); return JoinCode(l.code, r.code, new Instruction(JumpLT l.loc r.loc truelabel), new Instruction(Jump falselabel)); /* Translate a not expression. */ translate(Not(subexp), truelabel, falselabel) return translate(not(subexp), falselabel, truelabel); /* Translate an or expression. */ translate(Or(bexp1,bexp2), truelabel, falselabel) // The new label will fall between the code for bexp1 // and the code to bexp2. Label newlabel = new Label(); // If bexp1 is true, jump to true label. Otherwise, // do the code for bexp2. Code code1 = translate(bexp1, truelable, newlabel); // The result now depends only on bexp2. Code code2 = translate(bexp2, truelabel, falselabel); return JoinCode(code1, newlabel, code2); /* Translate an and expression. */ translate(And(bexp1,bexp2), truelabel, falselabel) // The new label will fall between the code for bexp1 // and the code to bexp2. Label newlabel = new Label(); // If bexp1 is false, jump to false label. Otherwise, // do the code for bexp2. Code code1 = translate(bexp1, newlabel, falselabel); // The result now depends only on bexp2. Code code2 = translate(bexp2, truelabel, falselabel); return JoinCode(code1, newlabel, code2);
if
s.
[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]
-
[Honesty]
[Instructions]
[Links]
[Search]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Project]
[Readings]
[Reference]
Misc:
[2001S]
[2002F]
[SamR]
Disclaimer:
I usually create these pages on the fly
, which means that I rarely
proofread them and they may contain bad grammar and incorrect details.
It also means that I tend to update them regularly (see the history for
more details). Feel free to contact me with any suggestions for changes.
This document was generated by
Siteweaver on Wed May 5 11:47:14 2004.
The source to the document was last modified on Tue Jan 20 23:06:46 2004.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS362/2004S/Outlines/outline.34.html
.
You may wish to
validate this document's HTML
;
;
Check with Bobby