Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Strongly typed languages are ones in which every expression can be assigned an unambiguous type
Weakly typed languages could have run-time errors due to type incompatibility
Base Types
Numbers
integer C specifies length in relative terms, short and long Java specifies specific lengths: byte, short, int and long have 8, 16, 32 and 64 bits, respectively floating point numbers Many languages have two sizes, 32 and 64 bit
Can use IEEE representation standards
Characters
Single letter, used to be 8 bit ASCII standard, now can also be a 16 bit Unicode
Booleans
Two values: true and false C uses the 0 and 1 subrange of integers
Strings
Some languages have strings as a base type with catenation operators 2
Variant records or Unions a component may be one of a choice of types union ( double r; int i; }
Representation can save space for the largest element and may have a tag to indicate which type the value is Take care not to have run-time (value) errors
Other Types
Enumerated types the programmer can create a type name for a specific set of constant values enum WeekDay {Sunday, Monday, Saturday}
Representation as for a small set
Type declarations or type definitions allow the programmer to assign new type names to a type expression
These names should also be stored in the symbol table Will have scope and other attributes Definitions may be recursive in some languages If so, size of object will be unknown
6
Representing Types
Types can be represented as expressions, or quite often as trees:
array(9)
function
type
arglist
arg1 type
Type Equivalence
Name equivalence in this kind of equivalence, two types must have the same name
Assumes that the programmer will introduce new names exactly when they want types to be different if you say t1 = int and t2 = int, t1 and t2 are different
Structural equivalence two objects are interchangeable if their types have the same fields with equivalent types. int x[10][10] and int y[10][10] x and y have equivalent types, the 10 by 10 arrays
More complex situations arise in structural equivalence of other compound types, e.g. may have mutually recursive type definitions
Array reference
Suppose we have exp1 -> exp2[exp3], then an adhoc SDT: if (isArrayType(exp2.type)) and typeEqual(exp3.type, integer) then exp1.type = exp2.type.basetype // get the basetype child else type-error(exp1)
Function calls have similar rules to check the signature of the function name the parameters
10
Overloading Functions
Can declare the same function (or method) name with different numbers and types of parameters int max(int x,y) double max(double x,y)
Java and C++ allow such overloaded declarations
Need to augment the symbol table functionality to allow for the name to have multiple signatures
The lookup procedure is always give a name to look up we can add a typelist argument and the lookup can tell us if there is a function declared with that signature Or the lookup procedure is given the name to look up and in the case of a method, it can return sets of allowable signatures
12
Conversion from one type to another is said to be implicit it if is done automatically by the compiler, and is also called coercion Conversion is said to be explicit if the programmer must write the conversion
Called casts in C and Java languages
13
Conversions between primitive types in Java: (there is also widening and narrowing for references, i.e. objects) Widening Narrowing (usually with a cast) double float long int short byte
14
16
Polymorphic Typing
A language is polymorphic is language constructs can have more than one type procedure swap(anytype x,y) where anytype is considered to be a type variable Polymorphic functions have type patterns or type schemes, instead of actual type expressions The type checker must check that the types of the actual parameters fit the pattern
Technically, the type checker must find a substitution of actual types for type variables that satisfies the type equivalence between the formal type pattern and the actual type expression In complex cases with recursion, may need to do unification to solve the substitution problem Most notably in the language ML
References
Keith Cooper and Linda Torczon, Engineering a Compiler, Elsevier, 2004.. Kenneth C. Louden, Compiler Construction: Principles and Practices, PWS Publishing, 1997. Aho, Lam, Sethi, and Ullman, Compilers: Principles, Techniques, and Tools. Addison-Wesley, 2006. (The purple dragon book) Charles Fischer and Richard LeBlanc, Jr., Crafting a Compiler with C, Benjamin Cummings, 1991.
18