UP | HOME

Classes and objects IV
Lecture 16

Table of Contents

Review

Questions about the last class?

Quiz

What is the difference between virtual and non-virtual functions?

Quiz Discussion

Semantic rules

cd      ::=  class c { d* }            // class declaration

cd      ::=  impl c { f*  }            // class implementation

t       ::= int                        // integer type
          | bool                       // boolean type
          | string                     // string type
          | (t+) -> t                  // function type
          | class c                    // class type

n       ::= x                          // simple name
          | n . x                      // qualified name

s       ::= init x ;
          | delete x ;


// denotational semantics

[[ class c { d* } ]]  ::= struct c { [[ d* ]](c) };
[[ impl c { f+ } ]]   ::= [[ f+ ]](c)
[[ init x ; ]]        ::= x = malloc(sizeof(* x)); [[ initFuncs(x) ]]
[[ delete x ; ]]      ::= free(x);

[[ d ]](c)  ::= type.getFieldType(d.ID, c) ;
[[ f ]](c)  ::= f.returnType.getDeclarationType(mangleDefName(c, f.IO)) ( struct c * this [[ arg* ]] ) { [[ d* ]] [[ st* ]] }
[[ arg ]]   ::= , scope.getSymbol(arg.ID).getReferenceType(arg.ID)

Initializing function pointers

initFuncs(x) {
  type = scope.getTypeAnyScope(x)
  className = type.name
  classScope = scope.getScopeAny(className)
  for each symbol in classScope
    if classScope.symbols.get(symbol).key() == Type.Kind.FUNCTION
      emit( x -> symbol = mangleDefName(className, symbol) ; )
  scope.getScopeAny().symbols.keySet()
}

COOL is another illustration of the semantics of an object-oriented language.

Implementing the semantic rules

Project

(3.5 weeks)

Implement the ClassC extension to SimpleC, i.e., the type-checker extension and the code generation extension.

  • Skeleton
    • Includes complete type checker and code generator
    • ClassTypechecker.java
    • ClassCodeGen.java

Getting the skeleton

Be sure to unzip the tarfile in the root of your existing repo. It depends on having access to the original Grammar.g4.

# from the parent of your project folder
wget http://www.cs.ucf.edu/~gazzillo/teaching/cop5621spring23/lectures/src/classc.tar
cd project-NAME
tar -xvf ../classc.tar ./
git add src/classc
git commit -m "Added classc skeleton"

References to keep on hand

  • Keep open the following while implementing the type checker
    1. The ClassC grammar extension (Grammar.g4)
    2. The type rules
    3. The semantics

Author: Paul Gazzillo

Created: 2023-04-13 Thu 14:59