1 / 97

TL: Basic Types and Declarations

TL: Basic Types and Declarations. Programming Fundamentals 12 Feliks Klu ź niak. TL: Basic Types and Declarations. Programming Fundamentals 12 Feliks Klu ź niak. The concept of type

ashlyn
Télécharger la présentation

TL: Basic Types and Declarations

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. TL: Basic Types and Declarations Programming Fundamentals 12 Feliks Kluźniak

  2. TL: Basic Types and Declarations Programming Fundamentals 12 Feliks Kluźniak

  3. The concept of type A higher-level programming language is designed for relative ease of use, not for controlling the details of execution of a particular machine. TL: Basic Types and Declarations

  4. The concept of type A higher-level programming language is designed for relative ease of use, not for controlling the details of execution of a particular machine. In such a language allowing programs to modify themselves would be madness: that would effectively destroy the ability to reason about such programs. TL: Basic Types and Declarations

  5. The concept of type A higher-level programming language is designed for relative ease of use, not for controlling the details of execution of a particular machine. In such a language allowing programs to modify themselves would be madness: that would effectively destroy the ability to reason about such programs. And reason about programs we must, for otherwise we would be forced to apply only the operational point of view, which is – as we shall see – pitifully inadequate. TL: Basic Types and Declarations

  6. The concept of type Another hindrance in our ability to reason about programs would be treating variables as just memory locations. TL: Basic Types and Declarations

  7. The concept of type Another hindrance in our ability to reason about programs would be treating variables as just memory locations. If a and b are just machine words, then we don’t know whether the expression a * b makes sense. TL: Basic Types and Declarations

  8. The concept of type Another hindrance in our ability to reason about programs would be treating variables as just memory locations. If a and b are just machine words, then we don’t know whether the expression a * b makes sense. Consider, for example: a := 7; b := ‘A’; c := a * b What is the meaning of the product of an integer and a character? TL: Basic Types and Declarations

  9. The concept of type Another hindrance in our ability to reason about programs would be treating variables as just memory locations. If a and b are just machine words, then we don’t know whether the expression a * b makes sense. Consider, for example: a := 7; b := ‘A’; c := a * b What is the meaning of the product of an integer and a character? If we are doing something clever with the integer encoding of that character, we should have means of saying so explicitly. TL: Basic Types and Declarations

  10. The concept of type Another hindrance in our ability to reason about programs would be treating variables as just memory locations. If a and b are just machine words, then we don’t know whether the expression a * b makes sense. Consider, for example: if a < b then a := ‘A’ else a := 7 fi; c := a * b sometimes this makes sense, sometimes not TL: Basic Types and Declarations

  11. The concept of type Another hindrance in our ability to reason about programs would be treating variables as just memory locations. And so we treat each variable as associated with a type. A type is a set of values of a particular form (e.g., integers, characters), associated with a set of operations (i.e., things we can do with these values). TL: Basic Types and Declarations

  12. The concept of type Another hindrance in our ability to reason about programs would be treating variables as just memory locations. And so we treat each variable as associated with a type. A type is a set of values of a particular form (e.g., integers, characters), associated with a set of operations (i.e., things we can do with these values). For example, the type “length” and the type “velocity” might consist of the same set of values (real numbers), but be associated with different sets of operations (multiplication of velocities does not make sense). TL: Basic Types and Declarations

  13. The concept of type Another hindrance in our ability to reason about programs would be treating variables as just memory locations. And so we treat each variable as associated with a type. A type is a set of values of a particular form (e.g., integers, characters), associated with a set of operations (i.e., things we can do with these values). If a programming language is constructed in such a fashion that the compiler can determine the type of each expression, and detect whether it is well-typed (i.e., whether it “makes sense”), then we say that the language is (statically) strongly typed. TL: Basic Types and Declarations

  14. The concept of type Another hindrance in our ability to reason about programs would be treating variables as just memory locations. And so we treat each variable as associated with a type. A type is a set of values of a particular form (e.g., integers, characters), associated with a set of operations (i.e., things we can do with these values). If a programming language is constructed in such a fashion that the compiler can determine the type of each expression, and detect whether it is well-typed (i.e., whether it “makes sense”), then we say that the language is (statically) strongly typed. A compiler translates a program into machine language. Its function is similar to that of an assembler, but it is usually much more complicated. TL: Basic Types and Declarations

  15. The concept of type Another hindrance in our ability to reason about programs would be treating variables as just memory locations. And so we treat each variable as associated with a type. A type is a set of values of a particular form (e.g., integers, characters), associated with a set of operations (i.e., things we can do with these values). If a programming language is constructed in such a fashion that the compiler can determine the type of each expression, and detect whether it is well-typed (i.e., whether it “makes sense”), then we say that the language is (statically) strongly typed. Statically strongly typed languages have many advantages, and we will limit our attention to such languages from now on. TL: Basic Types and Declarations

  16. The concept of type Another hindrance in our ability to reason about programs would be treating variables as just memory locations. And so we treat each variable as associated with a type. A type is a set of values of a particular form (e.g., integers, characters), associated with a set of operations (i.e., things we can do with these values). If a programming language is constructed in such a fashion that the compiler can determine the type of each expression, and detect whether it is well-typed (i.e., whether it “makes sense”), then we say that the language is (statically) strongly typed. Variables in such languages usually have to be declaredbefore they are used. TL: Basic Types and Declarations

  17. Declaration of variables in TL Variables are declared like this: varvname : TName where vname is the name of the declared variable; TName is the name of a type. TL: Basic Types and Declarations

  18. Declaration of variables in TL Variables are declared like this: varvname : TName where vname is the name of the declared variable; TName is the name of a type. Example: var age : int TL: Basic Types and Declarations

  19. Declaration of variables in TL Variables are declared like this: varvname : TName where vname is the name of the declared variable; TName is the name of a type. Such a declaration means that the variable can only hold values of that particular type TL: Basic Types and Declarations

  20. Declaration of variables in TL Variables are declared like this: varvname : TName where vname is the name of the declared variable; TName is the name of a type. Such a declaration means that the variable can only hold values of that particular type, and be used in expressions in which values of that type would “make sense”. TL: Basic Types and Declarations

  21. Declaration of variables in TL Variables are declared like this: varvname : TName where vname is the name of the declared variable; TName is the name of a type. Such a declaration means that the variable can only hold values of that particular type, and be used in expressions in which values of that type would “make sense”. NOTE: In general, the declaration does not make the variable hold any value: it is uninitialized until, e.g., it appears on the LHS of an assignment. TL: Basic Types and Declarations

  22. Declaration of variables in TL Variables are declared like this: varvname : TName where vname is the name of the declared variable; TName is the name of a type. Such a declaration means that the variable can only hold values of that particular type, and be used in expressions in which values of that type would “make sense”. In TL the declaration of a variable must precede its first use. TL: Basic Types and Declarations

  23. Declaration of variables in TL Variables are declared like this: varvname : TName where vname is the name of the declared variable; TName is the name of a type. Such a declaration means that the variable can only hold values of that particular type, and be used in expressions in which values of that type would “make sense”. In TL the declaration of a variable must precede its first use. There are also some visibility rules, which we will discuss later. TL: Basic Types and Declarations

  24. The basic types of TL: • int: the type of integers (in the supported range); • char : the type of characters (ASCII); • bool : the type of boolean values. TL: Basic Types and Declarations

  25. The basic types of TL: • int: the type of integers (in the supported range); • char : the type of characters (ASCII); • bool : the type of boolean values. • Each of these types has the property that a single value of the type fits in one machine word. TL: Basic Types and Declarations

  26. The basic types of TL: • int: the type of integers (in the supported range); • char : the type of characters (ASCII); • bool : the type of boolean values. • Each of these types has the property that a single value of the type fits in one machine word. • In TL there is only one other class of types with this property: pointer types. (A pointer is essentially an address: we will talk about these later.) TL: Basic Types and Declarations

  27. The basic types of TL: • int: the type of integers (in the supported range); • char : the type of characters (ASCII); • bool : the type of boolean values. • Each of these types has the property that a single value of the type fits in one machine word. • Values and variables of each of these types can be compared with = and != . TL: Basic Types and Declarations

  28. The basic types of TL: • int: the type of integers (in the supported range); • char : the type of characters (ASCII); • bool : the type of boolean values. • Each of these types has the property that a single value of the type fits in one machine word. • Values and variables of each of these types can be compared with = and != . • A variable of each of these types can be assigned with an expression of the same type ( v := E ). TL: Basic Types and Declarations

  29. The basic type int: Values: -2147483648, … , -1, 0, 1, … ,2147483647 (on a 32 bit machine) TL: Basic Types and Declarations

  30. The basic type int: Values: -2147483648, … , -1, 0, 1, … ,2147483647 Literals: 0, 1, … ,2147483647 ( but it is possible to write expressions that denote negative integers, e.g., – 2147483647 – 1 ) TL: Basic Types and Declarations

  31. The basic type int: Values: -2147483648, … , -1, 0, 1, … ,2147483647 Literals: 0, 1, … ,2147483647 NOTE: We can also write integer literals in hexadecimal (for 32 bits): #AF3 #12345678 (quite different from 12345678) TL: Basic Types and Declarations

  32. The basic type int: Values: -2147483648, … , -1, 0, 1, … ,2147483647 Literals: 0, 1, … ,2147483647 Predefined constants: min_int, max_int TL: Basic Types and Declarations

  33. The basic type int: Values: -2147483648, … , -1, 0, 1, … ,2147483647 Literals: 0, 1, … ,2147483647 Predefined constants: min_int, max_int Operators: + , – , * , div , mod (– : unary and binary) TL: Basic Types and Declarations

  34. The basic type int: Values: -2147483648, … , -1, 0, 1, … ,2147483647 Literals: 0, 1, … ,2147483647 Predefined constants: min_int, max_int Operators: + , – , * , div , mod (– : unary and binary) (e.g., – v + w mod 10 , the result is of type int ) TL: Basic Types and Declarations

  35. The basic type int: Values: -2147483648, … , -1, 0, 1, … ,2147483647 Literals: 0, 1, … ,2147483647 Predefined constants: min_int, max_int Operators: + , – , * , div , mod (– : unary and binary) (e.g., – v + w mod 10 , the result is of type int ) NOTE: unary – has the highest precedence, TL: Basic Types and Declarations

  36. The basic type int: Values: -2147483648, … , -1, 0, 1, … ,2147483647 Literals: 0, 1, … ,2147483647 Predefined constants: min_int, max_int Operators: + , – , * , div , mod (– : unary and binary) (e.g., – v + w mod 10 , the result is of type int ) NOTE: unary – has the highest precedence, then * , div, mod, multiplicative operators TL: Basic Types and Declarations

  37. The basic type int: Values: -2147483648, … , -1, 0, 1, … ,2147483647 Literals: 0, 1, … ,2147483647 Predefined constants: min_int, max_int Operators: + , – , * , div , mod (– : unary and binary) (e.g., – v + w mod 10 , the result is of type int ) NOTE: unary – has the highest precedence, then * , div, mod,multiplicative operators then + and binary – . additive operators TL: Basic Types and Declarations

  38. The basic type int: Values: -2147483648, … , -1, 0, 1, … ,2147483647 Literals: 0, 1, … ,2147483647 Predefined constants: min_int, max_int Operators: + , – , * , div , mod (– : unary and binary) (e.g., – v + w mod 10, the result is of type int ) NOTE: unary – has the highest precedence, then * , div, mod, then + and binary – . So the above is ( – v ) + ( w mod 10 ) . TL: Basic Types and Declarations

  39. The basic type int: Values: -2147483648, … , -1, 0, 1, … ,2147483647 Literals: 0, 1, … ,2147483647 Predefined constants: min_int, max_int Operators: + , – , * , div , mod (– : unary and binary) (e.g., – v + w mod 10 , the result is of type int ) NOTE: unary – has the highest precedence, then * , div, mod, then + and binary – . So the above is ( – v ) + ( w mod 10 ) . We can change this by using parentheses: – ((v + w) mod 10) .

  40. The basic type int: Values: -2147483648, … , -1, 0, 1, … ,2147483647 Literals: 0, 1, … ,2147483647 Predefined constants: min_int, max_int Operators: + , – , * , div , mod (– : unary and binary) a div b * b + a mod b = a 7 div - 2 = - 3, 7 mod - 2 = 1 -7 div 2 = - 3, - 7 mod 2 = - 1 TL: Basic Types and Declarations

  41. The basic type int: Values: -2147483648, … , -1, 0, 1, … ,2147483647 Literals: 0, 1, … ,2147483647 Predefined constants: min_int, max_int Operators: + , – , * , div , mod , (– : unary and binary) bit_not, bit_and, bit_or, bit_xor. bit_not has the precedence of unary – . The other “bitwise” operators have the precedence of multiplicative operators (i.e., * , div , mod).

  42. The basic type int: Values: -2147483648, … , -1, 0, 1, … ,2147483647 Literals: 0, 1, … ,2147483647 Predefined constants: min_int, max_int Operators: + , – , * , div , mod , … (– : unary and binary) Additional comparison operators: <, =<, >=, > . (e.g., v + d < max_int div 10 : value of type bool !). TL: Basic Types and Declarations

  43. The basic type int: Values: -2147483648, … , -1, 0, 1, … ,2147483647 Literals: 0, 1, … ,2147483647 Predefined constants: min_int, max_int . Operators: + , – , * , div , mod , … (– : unary and binary) Additional comparison operators: <, =<, >=, > . (e.g., v + d < max_int div 10 : value of type bool !). NOTE: The priority of comparison operators is lower than that of the arithmetic operators, so the above is (v + d) < (max_int div 10) . TL: Basic Types and Declarations

  44. The basic type char: Values: characters represented in 8 bits (ASCII encoding) TL: Basic Types and Declarations

  45. The basic type char: Values: characters represented in 8 bits (ASCII encoding) Literals: ‘ ‘ , ‘a’ , ‘A’ , ‘0’ , ‘\n’ , ‘\’‘ , ‘\\’ , etc. TL: Basic Types and Declarations

  46. The basic type char: Values: characters represented in 8 bits (ASCII encoding) Literals: ‘ ‘ , ‘a’ , ‘A’ , ‘0’ , ‘\n’ , ‘\’‘ , ‘\\’ , etc. the space character TL: Basic Types and Declarations

  47. The basic type char: Values: characters represented in 8 bits (ASCII encoding) Literals: ‘ ‘ , ‘a’ , ‘A’ , ‘0’ , ‘\n’ , ‘\’‘ , ‘\\’ , etc. the character zero, quite different from the integer 0 TL: Basic Types and Declarations

  48. The basic type char: Values: characters represented in 8 bits (ASCII encoding) Literals: ‘ ‘ , ‘a’ , ‘A’ , ‘0’ , ‘\n’ , ‘\’‘ , ‘\\’ , etc. the newline TL: Basic Types and Declarations

  49. The basic type char: Values: characters represented in 8 bits (ASCII encoding) Literals: ‘ ‘ , ‘a’ , ‘A’ , ‘0’ , ‘\n’ , ‘\’‘ , ‘\\’ , etc. the quote TL: Basic Types and Declarations

  50. The basic type char: Values: characters represented in 8 bits (ASCII encoding) Literals: ‘ ‘ , ‘a’ , ‘A’ , ‘0’ , ‘\n’ , ‘\’‘ , ‘\\’ , etc. the backslash TL: Basic Types and Declarations

More Related