Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CSE240 Lecture 05

CSE240 Lecture 05

Introduction to Programming Languages
Data, Data types and Type Checking
(201701)

B546a9b97d993392e4b22b74b99b91fe?s=128

Javier Gonzalez
PRO

January 05, 2017
Tweet

Transcript

  1. jgs CSE 240 Introduction to Programming Languages Lecture 05: Data,

    Data types and Type Checking Dr. Javier Gonzalez-Sanchez javiergs@asu.edu javiergs.engineering.asu.edu | javiergs.com PERALTA 230U Office Hours: By appointment
  2. Javier Gonzalez-Sanchez | CSE240 | Fall2021 | 2 jgs Announcement

    § Assignment 01 is due on September 09 at 10:29 am
  3. jgs Previously …

  4. Javier Gonzalez-Sanchez | CSE240 | Fall2021 | 4 jgs C

    and C++ § Install Microsoft Visual Studio or XCode Plan B § CLion https://www.jetbrains.com/clion/ § Online Compiler for C/C++ https://www.onlinegdb.com/online_c_compiler § Dev-C++ 5 http://www.bloodshed.net/devcpp.html § Eclipse, etc.
  5. Javier Gonzalez-Sanchez | CSE240 | Fall2021 | 5 jgs Program

    Processing Program Processing Interpretation LISP Prolog Compilation C C++ Two Step Translation with Intermediate Code Compilation + Interpretation Java Compilation + Compilation C++ (.Net Solution) C# (.Net Solution)
  6. Javier Gonzalez-Sanchez | CSE240 | Fall2021 | 6 jgs Program

    Processing: Intermediate Code (Java) Intermediate code make the language and compiler machine-independent. This concept was implemented in 1970’s to simplify the compiler design. It is used in Java. bytecode Java program javac Java virtual machine Interpreter on a specific machine Simulator or Hardware source program intermediate code Virtual machine compiler
  7. Javier Gonzalez-Sanchez | CSE240 | Fall2021 | 7 jgs Program

    Processing: Intermediate Code (.Net) Microsoft uses an intermediate code for its Common Language Runtime (CLR) environment (.Net Runtime Environment), which is independent of languages Prolog F# X F# compiler Prolog compiler X compiler JIT AND CLI C# Languages MS IL Just-In-Time compiler Intermediate Language Common Language Infrastructure Compilers C# compiler C++ C++ compiler C C compiler Simulator or Hardware
  8. Javier Gonzalez-Sanchez | CSE240 | Fall2021 | 8 jgs Program

    Processing: Intermediate Code Advantages: • A single compiler for all machines • A small virtual machine (interpreter) that even fits in a web browser Disadvantages: § Slow execution and memory space § Separate compilation phase and may lose debugging info
  9. Javier Gonzalez-Sanchez | CSE240 | Fall2021 | 9 jgs Program

    Processing Program Processing Interpretation LISP Prolog Compilation C C++ Two Step Translation with Intermediate Code Compilation + Interpretation Java Compilation + Compilation C++ (.Net Solution) C# (.Net Solution)
  10. Javier Gonzalez-Sanchez | CSE240 | Fall2021 | 10 jgs Test

    Yourselves § Pros and Cons Interpreter § Pros and Cons Compiler § Pros and Cons Two-Steps Processing using Intermediate Code
  11. jgs Data and Data types

  12. Javier Gonzalez-Sanchez | CSE240 | Fall2021 | 12 jgs Data

    | Memory management
  13. Javier Gonzalez-Sanchez | CSE240 | Fall2021 | 13 jgs Data

    Types § A set of primary values allowed and operations on these values. § Data types define size and format – used to declare variables. // Java int (4) (-231 to 231 ) ; -2,147,483,648 to 2,147,483,647 float (4) 3.4e−038 to 3.4e+038
  14. Javier Gonzalez-Sanchez | CSE240 | Fall2021 | 14 jgs Data

    Types § A set of primary values allowed and operations on these values. § Data types define size and format – used to declare variables. 00000000000000000000000000000011 int i = 3; 01000000010000000000000000000000 float j = 3.0f; sign exponent fraction
  15. Javier Gonzalez-Sanchez | CSE240 | Fall2021 | 15 jgs Type

    Checking (semantic) § Ensuring that the types of operands of an operator are legal or equivalent to the legal type. "Hello" + 5; 5 > 7 + “Hello” char x = 60 + 5; float a = 2.0; double b = 2.0; int b = 'a';
  16. Javier Gonzalez-Sanchez | CSE240 | Fall2021 | 16 jgs Data

    Types Equivalence (Complex Types) § Structural equivalence Two types are equivalent if they have the same set of values and operations. § Name equivalence Two types are equivalent if they have the same name
  17. Javier Gonzalez-Sanchez | CSE240 | Fall2021 | 17 jgs Data

    Types Equivalence (Complex Types) class Foo { int data[100]; int count; } class Bar { int data[100]; int count; } Foo x, y; Bar r, s; // name // equivalence // correct x = y; r = s; // incorrect x = r; // structural // equivalence // correct x = y; r = s; // correct x = r;
  18. Javier Gonzalez-Sanchez | CSE240 | Fall2021 | 18 jgs Type

    Conversion § Coercion –implicitly convert Type_A to Type_B. § Casting: –explicitly convert Type_A to Type_B. § Type error.
  19. Javier Gonzalez-Sanchez | CSE240 | Fall2021 | 19 jgs Type

    Conversion int x = 5; // Coercion: Implicit type conversion (int to float) float f = 3.14f + x; // Casting: Explicit type conversion (float to int) int i = (int) f + x;
  20. Javier Gonzalez-Sanchez | CSE240 | Fall2021 | 20 jgs Strong

    Type Checking A strongly typed language is one in which: § each name in a program has a single type associated with it; § the type is known at compilation time; § type errors are always reported. Strong Weak C C++ Java
  21. Javier Gonzalez-Sanchez | CSE240 | Fall2021 | 21 jgs Strong

    Type Checking A strongly typed language is one in which: § each name in a program has a single type associated with it; § the type is known at compilation time; § type errors are always reported. Is strong type checking good? ☐ Yes ☐ No ☐ Maybe
  22. Javier Gonzalez-Sanchez | CSE240 | Fall2021 | 22 jgs Strong

    Type Checking A strongly typed language is one in which: § each name in a program has a single type associated with it; § the type is known at compilation time; § type errors are always reported. Is strong type checking good? Trade flexibility for reliability! Strong type increase reliability, but it loses the flexibility. float x = 3.0f; // Java float x = 3.0; // C (defvar x 3.0) // LISP
  23. Javier Gonzalez-Sanchez | CSE240 | Fall2021 | 23 jgs Questions

  24. jgs Coming next Programming with C and the Structural Paradigm

  25. jgs CSE 240 Introduction to Programming Languages Javier Gonzalez-Sanchez, Ph.D.

    javiergs@asu.edu Fall 2021 Copyright. These slides can only be used as study material for the class CSE240 at Arizona State University. They cannot be distributed or used for another purpose.