80 likes | 266 Views
Lecture # 21. Chapter 6 Uptill 6.4. Type System. A type system is a collection of rules for assigning type expressions to the various parts of the program. A Type Checker implements a Type System. Type checking of Expressions. E char { E.type =char} Eint { E.type = int }
E N D
Lecture # 21 Chapter 6 Uptill 6.4
Type System • A type system is a collection of rules for assigning type expressions to the various parts of the program. • A Type Checker implements a Type System
Type checking of Expressions • E char {E.type=char} • Eint {E.type=int} • Eid {E.type= lookup (id.entry)} • E E1 mod E2 { E.type= if E1.type= integer and E2.type=integer then integer else error } • E E1[E2] {E.type= if E2.type= integer and E1.type= lookup(E1.type) then E1.type else error }
Type Checking of Statements • S id :=E { S.type := ifid.type = E.typethenvoidelsetype_error } • S ifEthenS { S.type := ifE.type = booleanthenS1.typeelsetype_error } • S whileEdoS1{ S.type := ifE.type = booleanthenS1.type elsetype_error } • S S1;S2 { S.type := ifS1.type = void and S2.type = void then void elsetype_error }
Testing the Type Equivalence of Expressions • Public boolsequiv (s,t) { if s and t are the same basic type return true else if s=array(s1,s2) and t= array(t1,t2) then return sequiv(s1,t1) and sequiv(s2,t2) else if s=s1*s2 and t=t1* t2 then return sequiv(s1,t1) and sequiv(s2,t2) else return false }
Encoding of Type Expressions • The expressions can be encoded in bits to check their type equivalence • Type Constructor Encoding pointer 01 array 10 freturns 11
Basic Types Encoding boolean 0000 char 0001 integer 0010 real 0011
Example Type Encoding • Type Expression • Char 000000 0001 • Freturns(char) 000011 0001 • Pointer(Freturns(char)) 000111 0001 • Array(Pointer(Freturns(char))) 100111 0001