Computer Science 164 - Spring 2000 - Aiken - Midterm 2

Computer Science 164 - Spring 2000 - Aiken - Midterm 2 - CS...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS 164, Midterm #2, Spring 2000 Midterm II CS164, Spring 2000 April 11, 2000 Problem #1:: Typing and Code Generation (15 points) Assume that we extend the Cool language with a new looping construct "do e1 until e2". The intended execution of this construct is that the expression e1 (the loop body) is evaluated by e2 (the terminal condition). If the value of e2 is false then the execution of the loop body starts again with e1. Otherwise the execution of the loop terminates and the value of the expression is the value of e1 in the last iteration * Give an appropriate Cool typing rule for do e1 until e2. To remind yourself of the notation we show below the cool typing rule for if. O, M, C |- e1 : Bool O, M, C |- e2 : t2 O, M, C |- e2 : T3 O, M, C |- if e1 then e2 else e3 fi : T2 |_| T3 * Give Cool operational semantics rules for du e1 until e2. To remind yourself of the notation, we show below one of the two operational semantics rules for if: so, S1, E |- e1 : Bool (true), S2 so, S2, E |- e2 : v2, S3 so, S1, E |- if e1 then e2 else e2 fi : v2, S3 * Write the code generation function for do e1 until e2 that generates code for a stack machine with an accumulator. You should only use the following stack-machine instructions: - "push" - pushes the value of the accumulator on the stack - "goto L" - jumps to label L - "iftrue L" tests the value of the accumulator and jumps to label L if the value is true. - "pop" - copies the value on top of the stack into the accumulator and pops the stack - "L:" - a label Remember that after the evaluation of an expression the result must be in the accumulator and the stack height must be as before the evaluation. stackcgen (do e1 until e2) Problem #2:: Type Checking (20 points) file:///C|/Documents%20and%20Settings/Jason%20Raft. ..%20Spring%202000%20-%20Aiken%20-%20Midterm%202.htm (1 of 5)1/27/2007 5:32:59 PM
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS 164, Midterm #2, Spring 2000 The typing rules of Cool contain many details whose presence and correctness is important to achieving a couple of goals: 1. We should not be able to type check programs whose operational behavior is undefined, such as programs that try to invoke a method on an object for which the method is not defined. 2. We should be able to type check all the obviously valid programs Here is the Cool typing rule for the let construct: O, M, C |- e1 : T1 T <= T0 O[T0/x], M, C |- e2 : T2 O, M, C |- let x : T0 <- e1 in e2 | T2 We propose below a number of changes to the typing rule for "let". Some of these proposed changes
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 5

Computer Science 164 - Spring 2000 - Aiken - Midterm 2 - CS...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online