Cse 321 Programming Languages and Compilers Lecture 15
- Slides: 39
Cse 321, Programming Languages and Compilers 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 9/16/2020 1
Cse 321, Programming Languages and Compilers • HW 10, Assigned last week, due Wed, – excellent warmup for project 3 • Project 3 assigned today – See webpage for details 9/16/2020 2
Cse 321, Programming Languages and Compilers 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 9/16/2020 3
Cse 321, Programming Languages and Compilers Environments • TE - Type environments – TE(x) = t – The variable x has type t in the environment TE • R - Return mode – R = { void , explicit t , none} – void means the method has type return type void and no expression is expected after a return statement – Explicit t means the method has return type t and the argument of a return must present be a subtype of t • C - Class Hierarchy – The set of classes is arranged into a hierarchy – Class colorpoint extends point {. . . } – C defines x » There is a class definition for x • Th – the type of the self object “this” 9/16/2020 4
Cse 321, Programming Languages and Compilers Class Hierarchy object point colorpoint numeric int double boolean void C |~ int ≤ object C |~ colorpoint ≤ point C |~ boolen ≤ object C defines point 9/16/2020 5
Cse 321, Programming Languages and Compilers 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 9/16/2020 6
Cse 321, Programming Languages and Compilers Representing Types type Id = string; (** Representing types for mini-Java **) datatype Basic = Bool | Int | Real; datatype Type = Basic. Type of Basic | Array. Type of Basic | Obj. Type of Id | Void. Type; 9/16/2020 7
Cse 321, Programming Languages and Compilers 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 = Cint of int | Creal of string | Cbool of bool 9/16/2020 *) *) (* Literal constants *) 8
Cse 321, Programming Languages and Compilers Representing Programs 2 - expressions datatype Exp = Literal of Constant (* | Binop of Op * Exp (* | Relop of Op * Exp (* | Not of Exp (* | Array. Elm of Exp * (Basic TC) (* | Array. Len of Exp (* | Call of Exp * Id *(Id TC)* Exp list (* | New. Array of Basic * Exp (* | New. Object of Id (* (* Coerce is used only in type checking | Coerce of Exp 9/16/2020 5, 6. 3, true x + 3 x < 7. 7 ! x x[3] x. length() x. f(1, z) new int[3] new point() *) *) *) 9
Cse 321, Programming Languages and Compilers Representing Programs 3 - statements datatype Stmt = Block of Stmt list (* {x: 5; print(2)} | Assign of Exp option * Id * Exp option * Exp (* p. x[2]=5 p. x=5 | Call. Stmt of Exp * Id * (Id TC)* Exp list (* x. f(1, z) | If of Exp * Stmt (* if (p<2) x=5 else x=6 | While of Exp * Stmt (* while (p) s | Print. E of Exp (* System. out. println(x) | Print. T of string (* System. out. println("zbc") | Return of Exp option; (* return (x+3) 9/16/2020 10 *) *)
Cse 321, Programming Languages and Compilers Representing programs 4 - declarations datatype Var. Decl = Var. Decl of Type * Id * Exp option; datatype Formal = Formal of Type * Id; datatype Met. Decl = Met. Decl of Type * Id * Formal list * Var. Decl list * Stmt list; datatype Class. Dec = Class. Dec of Id * Var. Decl list * Met. Decl list; datatype Program = Program of Class. Dec list; 9/16/2020 11
Cse 321, Programming Languages and Compilers 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. 9/16/2020 12
Cse 321, Programming Languages and Compilers Type Equality over basic types fun typeeq (x, y) = case (x, y) of (Real, Real) => true | (Int, Int) => true | (Bool, Bool) => true | (_, _) => false • Pair wise pattern match over the basic types 9/16/2020 13
Cse 321, Programming Languages and Compilers Type equality fun typeeq (x, y) = case (x, y) of (Basic. Type x, Basic. Type y) => basiceq(x, y) | (Array. Type x, Array. Type y) => basiceq(x, y) | (Obj. Type x, Obj. Type y) => x=y | (Void. Type, Void. Type) => true | (_, _) => false 9/16/2020 14
Cse 321, Programming Languages and Compilers Subtyping fun subtype class. H (x, y) = case (x, y) of (x, Obj. Type “object”) => true | (Basic. Type Int, Obj. Type “numeric”) => true | (Basic. Type Real, Obj. Type “numeric”) => true | (Obj. Type x, Obj. Type y) => use. Tree class. H (x, y) | (_, _) => typeeq(x, y); Fun use. Tree class (x, y) =. . . 9/16/2020 15
Cse 321, Programming Languages and Compilers Expressing Errors • In ML we are lucky to have a rich exception mechanism. • Design a set of exceptions to report errors. • You might want to define helper functions – fun showt t =. . . – fun report exp computed expected =. . . 9/16/2020 16
Cse 321, Programming Languages and Compilers Type rules for mini-java • • One rule for each piece of syntax Use the different judgments accordingly Expression has type Th, C, TE |- exp : type Th, R, C, TE |= stmt Statement is well formed C |~ t 1 ≤ t 2 t 1 = t 2 t 1 is a subtype of t 2 C defines x Th, C, TE |- Y. f : (s 1, … , sn) → t Th, C, TE |- Y. x : t [ ] Class Y has method f with domain and range Class Y has variable x with type Op <+> : (t 1, t 2) -> t 3 t is basic x TE 9/16/2020 and TE (x) = t 17
Cse 321, Programming Languages and Compilers Simple Expressions • Th, C, TE |- n : int n is integer constant like 5 • Th, C, TE |- r : double r is real constant like 5. 3 • Th, C, TE |- this : Th • Th, C, TE |- true : boolean • Th, C, TE |- false : boolean 9/16/2020 18
Cse 321, Programming Languages and Compilers Variables TE (x) = t --------------R, Th, C, TE |= x : t Th, C, TE |- e 1 : Y C defines Y Th, C, TE |- Y. x : t -----------------R, Th, C, TE |= e 1. x : t x TE C defines Th Th, C, TE |- Th. x : t ---------------R, Th, C, TE |= x : t 9/16/2020 19
Cse 321, Programming Languages and Compilers 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 9/16/2020 20
Cse 321, Programming Languages and Compilers 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 9/16/2020 21
Cse 321, Programming Languages and Compilers Method call Expressions • Note that f(x) is really shorthand for this. f(x) C defines Th Th, C, TE |- Th. f : (s 1, … , sn) → t Th, C, TE |- ei : ti C |~ ti ≤ si --------------Th, C, TE |- f(e 1, … , en) : t Th, C, TE |- exp : X C defines X Th, C, TE |- X. f : (s 1, … , sn) → t Th, C, TE |- ei : ti C |~ ti ≤ si --------------Th, C, TE |- exp. f(e 1, … , en) : t 9/16/2020 22
Cse 321, Programming Languages and Compilers New arrays and objects Th, C, TE |- e : t t is basic --------------Th, C, TE |- new t [e] : t [ ] C defines x --------------Th, C, TE |- new x ( ) : x 9/16/2020 23
Cse 321, Programming Languages and Compilers Judgments over Statements • We use a different notation for typing statements • The judgment for statements does not have a return type. • It also uses a different “turnstile” symbol. • R, Th, C, TE |= e 1. x [ e 2] = e 3 9/16/2020 24
Cse 321, Programming Languages and Compilers Static Implicit Assignment TE (x) = t [ ] TE |- e 2 : int Th, C, TE |- e 3 : t --------------R, Th, C, TE |= x [ e 2] = e 3 Static, because the variable is in the type environment. This means the variable is a either a parameter or local variable to the current method. TE (x) = t Th, C, TE |- e 2 : t --------------R, Th, C, TE |= x = e 2 9/16/2020 25
Cse 321, Programming Languages and Compilers Explicit Hierarchical Assignment Statements Th, C, TE |- e 1 : Y C defines Y Th, C, TE |- Y. x : t [ ] Th, C, TE |- e 2 : int Th, C, TE |- e 3 : t --------------R, Th, C, TE |= e 1. x [ e 2] = e 3 Explicit because the object for containg the variable is explicit in the code 9/16/2020 Th, C, TE |- e 1 : Y C defines Y Th, C, TE |- Y. x : t Th, C, TE |- e 2 : t --------------R, Th, C, TE |= e 1. x = e 2 26
Cse 321, Programming Languages and Compilers Hierarchical Implicit Assignment x TE C defines Th Th, C, TE |- Th. x : t [ ] Th, C, TE |- e 2 : int Th, C, TE |- e 3 : t --------------R, Th, C, TE |= x [ e 2] = e 3 These rules hold only if the variable is not in the type environment x TE C defines Th Th, C, TE |- Th. x : t Th, C, TE |- e 2 : t --------------R, Th, C, TE |= x = e 2 Implicit because the object containg the variable is not in the code. 9/16/2020 27
Cse 321, Programming Languages and Compilers Note 3 forms of assignment static TE (x) = t Th, C, TE |- e 2 : t --------------R, Th, C, TE |= x = e 2 Implicit hierarchical x TE C defines Th Th, C, TE |- Th. x : t Th, C, TE |- e 2 : t --------------R, Th, C, TE |= x = e 2 9/16/2020 Explicit hierarchical Th, C, TE |- e 1 : Y C defines Y Th, C, TE |- Y. x : t Th, C, TE |- e 2 : t --------------R, Th, C, TE |= e 1. x = e 2 28
Cse 321, Programming Languages and Compilers If statement R, Th, C, TE |= s 1 Th, C, TE |- e 1 : boolean --------------------R, Th, C, TE |= if ( e 1 ) s 1 R, Th, C, TE |= s 2 Th, C, TE |- e 1 : boolean --------------------R, Th, C, TE |= if ( e 1 ) s 1 else S 2 9/16/2020 29
Cse 321, Programming Languages and Compilers While and Print statement R, Th, C, TE |= s 1 Th, C, TE |- e 1 : boolean --------------------R, Th, C, TE |= while ( e 1 ) s 1 t is basic Th, C, TE |- x : t ----------------------------R, Th, C, TE |= System. out. println ( x ) R, Th, C, TE |= System. out. println ( “literal string” ) 9/16/2020 30
Cse 321, Programming Languages and Compilers Return statement Void, Th, C, TE |= return C |~ t ≤ s Th, C, TE |- e : s -------------------------Explicit t, Th, C, TE |= return e 9/16/2020 31
Cse 321, Programming Languages and Compilers Method call statements Th, C, TE |- ei : ti C defines Th Th, C, TE |- Th. f : (s 1, … , sn) → Void C |~ ti ≤ si --------------R, Th, C, TE |= f(e 1, … , en) Th, C, TE |- ei : ti C defines X Th, C, TE |- X. f : (s 1, … , sn) → Void C |~ ti ≤ si Th, C, TE |- exp : X --------------R, C, TE |= exp. f(e 1, … , en) 9/16/2020 32
Cse 321, Programming Languages and Compilers Sequences of statements R, Th, C, TE |= si --------------R, Th, C, TE |= { s 1, … , sn } 9/16/2020 33
Cse 321, Programming Languages and Compilers Declarations • Declarations are type-environment to typeenvironment functions. R, Th, C, TE |= decl → R, Th, C, TE There are four kinds of declarations – Var-declarations – Method-declarations » Are mutually recursive – Class-declarations – Main method declaration » There is only one of these in any program 9/16/2020 34
Cse 321, Programming Languages and Compilers One Var declaration R, Th, C, TE |= t x ; → R, Th, C, TE+(x: t) Th, C, TE |- e : t ----------------------------------R, Th, C, TE |= t x = e ; → R, Th, C, TE+(x: t) 9/16/2020 35
Cse 321, Programming Languages and Compilers Many Var declarations R, Th, C, TE |= d 1 → R 1, Th 1, C 1, TE 1 |= d 2 → R 2, Th 2, C 2, TE 2 |= d 3 → R 3, Th 3, C 3, TE 3 ------------------------------------------- R, Th, C, TE |= {d 1; d 2; d 3; …} → R 3, Th 3, C 3, TE 3 9/16/2020 36
Cse 321, Programming Languages and Compilers Mutually recursive Method Declaration TEA = TE + (f 1, method Th (t 11, … , tn 1) →t 1) + … + (fk, method Th (t 1 k, … , tnk) →tk) TEA |= (t 1 k x 1 k, … tnk xnk) → TEBk |= (v 1; … ; vi ) → TECk tk, Th, C, TECk |= {s 1; … ; sj } -----------------------------------R, Th, C, TE |= { public t 1 f 1 (t 1 x 1, … tn xn) { v 1; … ; vi ; s 1; … ; sj } ; …; public tk fk (t 1 x 1, … tn xn) { v 1; … ; vi ; s 1; … ; sj } } → R, Th, C, TEA 9/16/2020 37
Cse 321, Programming Languages and Compilers Class Declarations • Class declarations extend the Class heirarchy R, Th, C+(y/x), TE |= { v 1; … ; vi ; m 1; … ; mj } → R 2, Th 2, C 2 TE 2 -------------------------------R, Th, C, TE |= Class x extends y { v 1; … ; vi ; m 1; … ; mj } → R 2, Th 2, C 2 TE 2 9/16/2020 38
Cse 321, Programming Languages and Compilers New Boiler plate for Parser Program. Types. sml structure Program. Types = struct (* Put type declarations here that you *) (* want to appear in both the parser *) (* and lexer. You can open this structure *) (* else where inside your application as well (* I’ve put the datatypes for the syntax here to start *) end; Mini. cm group is Program. Types. sml Mini. lex Mini. grm Driver. sml Type. Check. sml $/basis. cm $/smlnj-lib. cm $/ml-yacc-lib. cm 9/16/2020 39
- Cs 421
- Cs 421 uiuc
- Cse 340 principles of programming languages
- Vineeth kashyap
- Cse 321
- Pros and cons of compilers and interpreters
- Finding and understanding bugs in c compilers
- Symbol table implementation in lex and yacc
- Disadvantages of compiler
- Real-time systems and programming languages
- Advantages and disadvantages of programming languages
- Real-time systems and programming languages
- C++ binarymove
- Cross compilers
- Crafting a compiler
- Machine dependent compiler features
- Explain compiler construction tools
- 01:640:244 lecture notes - lecture 15: plat, idah, farad
- Multithreading program in java
- Cxc it
- Introduction to programming languages
- Plc
- Joey paquet
- Comparative programming languages
- Alternative programming languages
- Strongly typed vs weakly typed
- Transmission programming languages
- Types of programming languages
- Xenia programming languages
- Mainstream programming languages
- Programing languages
- Programming languages
- Programming languages
- Programming languages
- Tiny programming language
- Brief history of programming languages
- Lisp_q
- Low level language
- If programming languages were cars
- Reasons for studying concepts of programming languages