Docsity

Judgments for Mini-Java - Compiler Design - Lecture Slides, Slides of Computer Science

Partial preview of the text

Download Judgments for Mini-Java - Compiler Design - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Lecture #15, March. 5, 2007

  • Judgments for mini-Java
  • Multiple type environments
  • Class Hierarchy
  • Setting up an ML typechecker for mini-Java
  • Subtyping
  • Rules for Java
  • Declarations as functions over

environments

Types for mini-Java

  • Mini-Java has two kinds of programs
    • Expressions
    • Statements
  • We need a judgment for each kind
    • Expressions Th, C, TE |- exp : type
    • Statements R, Th, C, TE |= stmt
  • Subtyping judgment
    • C |~ t 1 ≤ t 2
  • Equality judgment
    • t 1 = t 2

Class Hierarchy object

point numeric

int double

boolean

colorpoint

void

C |~ int ≤ object

C |~ colorpoint ≤ point

C |~ boolen ≤ object

C defines point

Type Checkers in ML

  • To build type-checkers in ML we need
    • A datatype to represent types
    • A datatype (or datatypes) to represent programs (whose type we are checking) - Expressions - Statements - Declarations
    • A means for computing equality and subtyping over types
    • A means for expressing type errors

Representing Programs 1 - Auxillaries

(* A slot of type (`x TC) is an option type. The ) ( parser places NONE there and the type-checker ) ( fills it in with (SOME x) when "x" is known *)

type 'x TC = 'x option;

(******** Representing Programs *******)

datatype Op (* infix operators ) = ADD | SUB | MUL | DIV ( Arithmetic ) | AND | OR ( logical ) | EQ | NE | LT | LE | GT | GE ( relational *)

datatype Constant (* Literal constants *) = Cint of int | Creal of string | Cbool of bool

Representing Programs 2 - expressions

datatype Exp = Literal of Constant (* 5, 6.3, true ) | Binop of Op * Exp * Exp ( x + 3 ) | Relop of Op * Exp * Exp ( x < 7.7 ) | Not of Exp (! x ) | ArrayElm of Exp * Exp * (Basic TC) ( x[3] ) | ArrayLen of Exp ( x.length() ) | Call of Exp * Id (Id TC) Exp list ( x.f(1,z) ) | NewArray of Basic * Exp ( new int[3] ) | NewObject of Id ( new point() ) ( Coerce is used only in type checking *) | Coerce of Exp

Representing programs 4 - declarations datatype VarDecl = VarDecl of Type * Id * Exp option;

datatype Formal = Formal of Type * Id;

datatype MetDecl = MetDecl of Type * Id *

Formal list * VarDecl list * Stmt list;

datatype ClassDec

= ClassDec of Id * Id * VarDecl list * MetDecl list;

datatype Program = Program of ClassDec list;

Type Equality

  • In mini-Java we need both type equality and sub-typing
  • Type equality for testing that the primitive operators are applied to appropriate expressions of the correct type. Type equality is over basic types.
  • Sub-typing for method calls and assignments, testing that actual arguments are subtypes of the formal arguments. Subtyping is over all types.

Type equality

fun typeeq (x,y) =

case (x,y) of

(BasicType x,BasicType y) =>

basiceq(x,y)

| (ArrayType x,ArrayType y) =>

basiceq(x,y)

| (ObjType x,ObjType y) => x=y

| (VoidType,VoidType) => true

| (,) => false

Subtyping fun subtype classH (x,y) =

case (x,y) of (x,ObjType “object”) => true | (BasicType Int,ObjType “numeric”) => true | (BasicType Real,ObjType “numeric”) => true | (ObjType x,ObjType y) => useTree classH (x,y) | (,) => typeeq(x,y);

Fun useTree class (x,y) =...

Type rules for mini-java

  • One rule for each piece of syntax
  • Use the different judgments accordingly

Th, C, TE |- exp : type Th, R, C, TE |= stmt C |~ t 1 ≤ t 2 t 1 = t 2 C defines x Th, C, TE |- Y.f : (s 1 , … ,sn) → t

Th, C, TE |- Y.x : t [ ]

Op <+> : (t 1 ,t 2 ) -> t 3 t is basic x ∉ TE and TE (x) = t

Expression has type

Statement is well formed t 1 is a subtype of t (^2)

Class Y has method f with domain and range

Class Y has variable x with type

Simple Expressions

  • Th, C, TE |- n : int n is integer constant like 5
  • Th, C, TE |- r : double r is real constant like 5.
  • Th, C, TE |- this : Th
  • Th, C, TE |- true : boolean
  • Th, C, TE |- false : boolean Docsity.com

Operators

Th, C, TE |- e 1 : t 1

Th, C, TE |- e 2 : t 2

Op <+> : (t 1 ,t 2 ) -> t 3


Th, C, TE |- e 1 <+> e 2 : t 3

Th, C, TE |- e 1 : boolean


Th, C, TE |-! e 1 : boolean Docsity.com

Arrays

Th, C, TE |- e 1 : t [ ]

Th, C, TE |- e 2 : int


Th, C, TE |- e 1 [ e 2 ] : t

Th, C, TE |- e : t [ ]


Th, C, TE |- e.length() : int