Back to Translating Expressions, Continued. On to Translating Loops.
Held Friday, April 6, 2001
Summary
Today we consider how to translate conditionals.
Notes
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.
Monday, 22 January 2001
Friday, 6 April 2001
Back to Translating Expressions, Continued. On to Translating Loops.
[Current]
[Discussions]
[Glance]
[Honesty]
[Instructions]
[Links]
[Search]
[Syllabus]
Primary
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Project]
[Quizzes]
[Readings]
[Reference]
Sets
[Blackboard]
[98F]
Links
Disclaimer: I usually create these pages on the fly. This means that they are rarely proofread and may contain bad grammar and incorrect details. It also means that I may update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.
This page was generated by Siteweaver on Mon Apr 30 10:52:06 2001.
This page may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS362/2001S/outline.27.html
.
You may validate
this page's HTML.
The source was last modified Fri Apr 6 10:45:01 2001.