1 16-035 More Ada Constructs 7 Oct. 2002. 2 16-035 Today’s Constructs Derived types Record types Array types Visibility and scope Subprogram arguments.

Slides:



Advertisements
Similar presentations
Slide: 1 Copyright © AdaCore Packages Presented by Quentin Ochem university.adacore.com.
Advertisements

Programming Languages and Paradigms
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
Exception Handling. Background In a perfect world, users would never enter data in the wrong form, files they choose to open would always exist, and code.
Subprogram Control - Data sharing Mechanisms to exchange data Arguments - data objects sent to a subprogram to be processed. Obtained through  parameters.
Chapter 7: User-Defined Functions II
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
An Introduction to Java Programming and Object- Oriented Application Development Chapter 8 Exceptions and Assertions.
Exception Handling Exception handling (EH) allows a programmer to provide code in the program to handle run-time errors or exceptional situations – this.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 13 In a language without exception handling: When an exception occurs, control goes to the operating.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
VBA Modules, Functions, Variables, and Constants
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Exceptions (Large parts of these copied from Ed Schonberg’s slides)
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Concurrency - 1 Exceptions General mechanism for handling abnormal conditions Predefined exceptions: constraint violations, I/O errors, communication errors,
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
CS 201 Functions Debzani Deb.
Java Review 2 – Errors, Exceptions, Debugging Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
EE694v-Verification-Lect5-1- Lecture 5 - Verification Tools Automation improves the efficiency and reliability of the verification process Some tools,
Even More Ada Constructs 9 Oct Today’s Constructs Generics Private types Child library units Tasking Pragmas Elaboration.
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
MT311 Java Application Programming and Programming Languages Li Tak Sing ( 李德成 )
1 Exception Handling Introduction to Exception Handling Exception Handling in PLs –Ada –C++ –Java Sebesta Chapter 14.
PZ11A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ11A - Exception handling Programming Language Design.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
General Programming Introduction to Computing Science and Programming I.
Names Variables Type Checking Strong Typing Type Compatibility 1.
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
Lecture Set 5 Control Structures Part D - Repetition with Loops.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
Basic Semantics Associating meaning with language entities.
Introduction to Exception Handling and Defensive Programming.
Slide: 1 Copyright © AdaCore Subprograms Presented by Quentin Ochem university.adacore.com.
1 Type Checking Type checking ensures that the operands and the operator are of compatible types Generalized to include subprograms and assignments Compatible.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chap 7- Control Structures : The WHILE Statement.
Implementing Subprograms What actions must take place when subprograms are called and when they terminate? –calling a subprogram has several associated.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Higher Computing Science 2016 Prelim Revision. Topics to revise Computational Constructs parameter passing (value and reference, formal and actual) sub-programs/routines,
Controlling Program Flow with Decision Structures.
3/12/2013Computer Engg, IIT(BHU)1 OpenMP-1. OpenMP is a portable, multiprocessing API for shared memory computers OpenMP is not a “language” Instead,
JavaScript and Ajax (Control Structures) Week 4 Web site:
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Exception Handling How to handle the runtime errors.
Programming Fundamentals Enumerations and Functions.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
structured statically typed imperative wide-spectrum object-oriented high-level computer programming language extended from Pascal and other languages.
1 Exception handling Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 11.1.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
PZ11A Programming Language design and Implementation -4th Edition
User-Written Functions
Chapter 7: User-Defined Functions II
Type Checking, and Scopes
CS 326 Programming Languages, Concepts and Implementation
Testing and Debugging.
Languages and Compilers (SProg og Oversættere)
Exception handling Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 11.1.
Exception handling Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 11.1.
Exception handling Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 11.1.
Exception handling Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 11.1.
Presentation transcript:

More Ada Constructs 7 Oct. 2002

Today’s Constructs Derived types Record types Array types Visibility and scope Subprogram arguments Exception handling Case statement

Derived Types Ada is a “strongly-typed” language —No automatic conversions —Different types can only interact using operations specifically defined for the types To even add more strength to types, Ada allows the user to create new types derived from other types. type Meters_t is new Float; type Yards_t is new Float; Distance_To_Paris : Meters_t; Distance_To_NY : Yards_t; Distance_To_Paris := Distance_To_NY ; -- Illegal! However, explicit conversions can be performed Distance_To_Paris := Meters_t (Distance_To_NY * ) ;

Record Types Comparable to “structures” in other languages —One object contains components of various types type Vehicle_State is record Position : Position_type; Velocity : Velocity_type; Linear_Acceleration : Linear_Acceleration_type; Inertial_Attitude : Attitude_type; Attitude_Rate : Attitude_Rate_type; Att_Accel : Att_Accel_type; -- Yes, you can abbreviate end record; Ada records also can have “discriminants”

Discriminated Records* type Matrix_t is array (Int_32_t range <>, Int_32_t range <>) of Single_Float_t; Want to have a Trace function for a square matrix —Would need to check for non-square —Would need to check ranges (someone could have created an object My_Array : Matrix_t (1.. 3, 0.. 2); ) Instead, create a discriminated record type: subtype Pos_32_t is Int_32_t range 1.. Int_32_t’last; type Square_Matrix_t (Order : Pos_32_t := 3) is record Matrix : Matrix_t (1.. Order, 1.. Order); end record; Now, the compiler will make sure the input is of the correct form Taken from “Programming in Ada95”, 2 nd Edition, John Barnes

Variant Records The discriminant can also be used to create different records depending on the value of the discriminant type Error_t is (None, RAM_Scrub, Exception); type One_Hz_Telem_Data_t (Error_Kind : Error_t := None) is record case Error_Kind is when None => null; when RAM_Scrub => Bad_Location : Address_t; when Exception => Exception_data : Exception_Info_t; end case; end record;

Discriminant Can Change Dynamically or Can be Constant M : Square_Matrix_t := (3, (1.. 3 => (1.. 3 => 0.0))); N : Square_Matrix_t (Order => 4) := (4, (1.. 4 => (1.. 4 => 1.0)));. M := N; -- Legal N := M; -- Illegal (Order can not change for N)

Array Types Array types are easy —Multidimensional arrays are allowed »Matrix_t seen previously —Arrays of any other types are allowed, including arrays »Different syntax A (1,2) for 2-dim array A (1) (2) for array of arrays Very good idea to always have a well-constrained subtype for array types (or discriminated records with arrays) M : Square_Matrix_t; -- How much memory will this take?

Visibility and Scope Good News : Not every name used in multiple files must be global —In fact, only “Library-level” names are globally defined »Library-level names are packages and subprograms that are declared at the outermost level of a compilation unit Almost all global names are packages One notable exception: function Unchecked_Conversion Bad News : Lots of typing to tell the compiler what you want —Ada.Text_IO.Integer_IO.Put Not-so-bad News : “use” statement; “renames” statement with Ada.Text_IO;... use Ada.Text_IO.Integer_IO;-- Makes all declarations in package -- locally visible

Scope Example* 1.procedure Level_1 is 2. Count, Index : INTEGER := 1; 3. procedure Level_2 is 4. Index, Count : INTEGER := 2; 5. procedure Level_3 is 6. Count : INTEGER := 3; 7. begin 8. Count := -- Count from line 6 9. Level_1.Count; -- Count from line end Level_3; 11. procedure Level_3_Prime is 12. Data, Index, Count : INTEGER := 3; 13. Outer_Index : INTEGER renames Level_1.Level_2.Index; 14. begin 15. Count := Outer_Index -- Count from line Level_1.Level_2.Count; -- Count from line end Level_3_Prime; 18. begin 19. null; 20. end Level_2; end Level_1; 23.*

The “Use” Clause Makes names visible without qualification —Extremely useful to make readable code —Biggest problem is enumeration literals package Drawing_Package is type Colors_t is (Red, Blue, Yellow);... with Drawing_Package; procedure Use_It is C : Drawing_Package.Colors_t := Drawing_Package.Red; -- Tedious —Somewhat bad in that it makes it more difficult to find things Majority opinion – Use sparingly, so maintainers can easily find things Minority opinion – Tools can help find things; Ada has enough necessary tedium; don’t impose more

Subprogram Arguments Procedures can have arguments of 3 kinds —“in” means treat as a constant inside procedure —“out” means it must be written before it is read (similar to an uninitialized variable) »The value is given to the caller when the procedure exits —“in out” can be read and written »The value is copied in at the start, and copied out at the end —Note that copying or referencing is meant for example; the actual parameter passing mechanism is defined differently for different kinds of data, and in some cases is left up to the compiler

Exception Handling Exceptions are handled at the innermost scope where there is a relevant exception handler, out to the task level —For the moment, assume only 1 task in the program X := F(G(H(I(J(X))))); If an exception occurs in function J, if J has an exception handler for the specific exception, it will be handled locally —None of the outer functions will know the exception occurred If J does not have an exception handler for that exception, it is propagated to the next level

Exception Propagation with Ada.Text_IO; use Ada.Text_IO; procedure Exception_Test is type My_Int is range 1.. 5; Y : My_Int; function Level_1 (X : My_Int) return My_Int is Y : My_Int; begin Y := X + 10; -- raises Constraint_Error Does not matter. Never executes these statements return X; -- Does not execute this exception when Program_Error => return My_Int’Last; -- Attributes are useful when End_Error => return My_Int’First; end Level_1; begin Y := Level_1 (1); put (“Made it”); exception when Constraint_Error => Put (“Constraint_Error in Y”); -- Y has no value end Exception_Test;

Output Constraint_Error in Y Program exited normally

What Happens if no Exception Handler? raised CONSTRAINT_ERROR : exception_test.adb:8 Exception propagated to outermost level of current task Task (if no exception handler) ends Up to runtime system what to do next —For GNAT, see above for single task case —For multi-task case, tasks just disappear »GVD allows breakpoint on any exception »AdaGIDE has option to print traceback Shows the call stack when run inside AdaGIDE

Case Statement X : My_Positive := 1;... case (X) is when 1 =>...-- Any sequence of statements (if X = 1) when 2 => Any sequence of statements (if X = 2) when => Any sequence of statements (if X = 3, 4, or 5) when others => Any sequence of statements (X = anything else) end case Note, no need for “break” statement; after relevant sequence of statements, jump to “end case”