Project #3 Notes, Fall 2006 (updated 5 Nov 2006)

This page is an accumulation of (we hope) helpful notes, hints, etc., that might be useful to you in doing Project #3.

  1. Changes in project specifications
  2. Answers to some questions


Changes in project specifications

  1. We didn't say what to do with

          a[i] += 3
    

    That is, there is no rewrite corresponding to the conversion of

          a[i] = 3  => __setitem__ (a, i, 3)
    

    SO: assume (don't bother to check) that all augmented assignments have the form

          <simple variable or selection> = <expr>
    

    That is, no indexing or slices on the left-hand side of an augmented assignment (tuples on the left-hand side are already illegal).

    Given the node

       (aug_assign (id ... "__...__" ) LHS RHS)
    
    just decorate the first child (the id node) as if this were
       (call (id ... "__...__") [ LHS RHS ])
    
  2. We also didn't say what to do with constructors. SO: Let's not worry about them. Handle the parameterless case only:
            A () 
    

    and render it as an ordinary call in which the "function" is the type A and the type of A() is A (i.e., (type (id .... "A"))). Likewise, don't do anything special with __init__ for this project (treat as an ordinary def).

  3. I've played fast and loose with the scope of members within a class definition. So to keep you from going insane over one more detail, we'll do another little punt.

    The intent was that in a definition like this:

        class A (Object):
            x = 3
    	y = x + 2		# OK
    	def g (self):
    	   return x*2		# ILLEGAL: should be self.x for the
    				#   instance variable, or A.x for the
    				#   class variable.
    
    with the way I've set up the suggested skeleton, this can be a bit awkward to get right.

    SO: we won't test uses of unqualified members of classes (e.g., as in x + 2 or x*2 above). You have to handle selection of members (e.g., self.x) and definitions of members (e.g., x = 3), but not uses of the simple identifiers. Operationally, this means that you do the addSimpleDecls pass after having done gatherMembers, and that during addSimpleDecls on the class A, you don't have to add anything to the symbol table from the statements in A's bodies (just when you process the body of a method). It's not terribly difficult to do it right, and you're welcome to get it right if you want, but don't expect cases such as the commented lines above to be tested.

  4. The type of `E0 and E1' and of `E0 or E1' ought to be the least upper bound of the operands' types (say T0 and T1), as indicated in the lecture segment on typing the conditional (if-then-else) expression. However, that answer is going to cause a lot of grief (consider function types), so I'll test only a simpler alternative: if T0 is a subtype of T1, the type of either the "and" or "or" operation is T1; if T1 is a subtype of T0, then it is T0; and otherwise it is Any.

Answers to some questions

Q: Is "x = 3" a declaration (such that x is declared as of type Any)?

A: Yes, except in situations where x is already declared. For example, only one x is declared in

    def f ():
	x = 3
	x = x+y     # Same x
or in
    x = 3
    def f ():
	global x
	x = 42      # NOT a new x.
or in
    def f (x):
	x = 42      # Same as parameter x.

Q: By my understanding i:T does not get populated into the symbol table—it is there for the type checking phase of the semantic analyzer.

A: Well, you can use the symbol table as you like, but in my suggested outline, you are correct that one does not need to put this i into the symbol table: it will have been decorated with the appropriate decl elsewhere and you simply have to update the static type on that decl when you do the typing pass.

Q: In the prelude there were these two lines

   None: Void
   def None = Void.__None__ ()
now would this be declaring the symbol "None" twice and thus cause an error when populating the symbol table?

A: No. Type declarations do not define symbols. For example, the program consisting solely of

    Foo: Int
is illegal, since Foo is not defined.

[CS164 Homework]    [CS164 Home Page]

Page was last modified on Thu Nov 9 19:23:57 2006.
Address comments and questions to cs164@eecs.berkeley.edu