Primary:
[Skip To Body]
[Front Door]
[Current]
[Glance]
[Honesty]
[Instructions]
[Links]
[Search]
[Syllabus]
Sets:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Project]
[Readings]
[Reference]
ECA:
[About]
[Grades]
[Quizzes]
[Submit Work]
[Change Password]
[Reset Password]
Miscellaneous:
[2001S]
[98F]
[SamR]
[Glimmer Labs]
Back to Translating Declarations and Expressions. On to Translating Loops.
Held Monday, November 11, 2002
Summary
Today we consider how to translate conditionals.
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.
Back to Translating Declarations and Expressions. On to Translating Loops.
Primary:
[Skip To Body]
[Front Door]
[Current]
[Glance]
[Honesty]
[Instructions]
[Links]
[Search]
[Syllabus]
Sets:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Project]
[Readings]
[Reference]
ECA:
[About]
[Grades]
[Quizzes]
[Submit Work]
[Change Password]
[Reset Password]
Miscellaneous:
[2001S]
[98F]
[SamR]
[Glimmer Labs]
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 Fri Dec 6 10:38:30 2002.
The source to the document was last modified on Wed Sep 4 10:08:37 2002.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS362/2002F/Outlines/outline.29.html
.
You may wish to
validate this document's HTML
;
;
Check with Bobby