Whirlwind Tour of Ada
Part 1
Brief History, Design Goals, and Applications
Hello World, Use, Text_IO, Compile and Run
Ada History
- In 1970s DOD used 400 different languages
- Language design competition for one language
- Versions: 1983, 1995, 2005, 2012, 202x (coming soon)
- For the curious - The Ada language was ...
Ada Design Goals
- Program reliability and maintenance
- Programs with long lifetimes
- Program efficiency
- Programming as a human activity
- Emphasize readability over writability
- We will see examples of how the language supports these goals
Four Versions
- Ada 83
- Rich type model
- Support for threads, generics
- Partial support for OO
- Ada 95:
- Complete support of OO (but not interfaces)
- Protected objects (only one task at a time can access)
- Gnat: free compiler included in GNU gcc
- Backward compatible
- Ada 05:
- Java-like interfaces (ie multiple specification inheritance)
- Object notation allowed (eg aStudent.getName)
- Backward compatible
- Ada 12:
- Preconditions, postconditions, type predicates and invariants
- Example - Pre/post conditions:
-- Returns the location of X in A
function Find(A: Array; X: Integer) return Integer
with Pre => (for some M in A'Range => A(M) = X),
Post => A(Find'Result) = X and
(for all M in A'First .. Find'Result-1 => A(M) /= X);
- Example - Dynamic Predicate:
subtype Even is Integer
with Dynamic_Predicate => Even mod 2 = 0;
- Quantified expressions (ie for all and for some)
- Backward compatible
Procedural (and Ada) Goals and Application Areas
- Procedural goal: support applications that require:
- Time performance
- Space performance
- Closeness to the machine
- Reliability
- Example application areas:
- Example Ada applications:
Hello World
-- Purpose: Prints a message!
-- File: hello1.adb
with ada.text_io; -- Context clause
procedure hello1 is
begin
ada.text_io.put_line("Hello ITEC 320 class!");
end hello1;
/////////////////////////////////////////////
// Java equivalent
class Hello{
public static void main(String[] args){
System.out.println("Hello from Java!");
}
}
Edit, Compile, and Run - environments
- Environments you can use:
- Adagide - simplest
- GPL - professional (comes with gnat from libre.adacore.com)
- Eclipse plugin: gnatbench (also available from libre)
- Or you can use the command line
- Edit:
vim foo.adb
(or another use editor, such as notepad)
- Compile:
gnatmake foo
- Run:
foo
- More information on Compile and run Hello World
- Installing Gnat and an Ada Environment
Comments
-
--
to end of line
- No multi-line comment
Context Clause
-
with Ada.Text_IO
is a context clause
- Specifies to compiler that package Ada.Text_IO is used in this procedure
- All packages referenced in a routine must be named in a context clause
Program Structure
- Let's repeat the program:
with ada.text_io; -- Context clause
procedure hello1 is
begin
ada.text_io.put_line("Hello ITEC 320 class!");
end hello1;
Procedure hello1
is the called the main routine
- Main routine name matches file name
- One top level (ie not nested) procedure per file
- Later we will see nested procedures
- Execution begins with hello1
- The name hello1 is a user defined identifier
- Rules for identifiers are below
Compare and Contrast with Java
- These are comparable:
-
procedure hello1 is begin ... end hello1;
-
public static void main(String[] args){...}
- Begin/end used instead of brackets
- A procedure is similar to a void method
- No class needed to encapsulate
hello1
Ada.Text_IO: put_line and other I/O routines
-
Ada.Text_IO
contains many routines for I/O of text
-
Ada.Text_IO.put_line(s)
:
- outputs its string parameter
s
and a newline
- only works for strings, not for characters and numeric values
-
Ada.Integer_text_io
for integers
-
Ada.Float_text_io
for floats
- Other routines from Ada.Text_IO:
-
put(s)
: outputs its character or string parameter s
, without a newline
-
new_line
: outputs a newline
-
new_line(n)
: outputs n
newlines
-
set_col(n)
: move output cursor to column n
-
end_of_file
: checks for end of file
-
get
: read a character or a fixed length string from the input
-
get_line
: read a string from the input
-
put
takes characters or strings
-
put_line
takes only strings
Rules for Identifiers
- Begin with letter
- Followed by any number of letters, digits, underscores
- Underscore must be followed by a letter or digit
- Case Insensitive:
- hello, Hello, and HELLO all identify the same item
- Letters include 32 bit characters
- Example: π:
Constant
:= 3.14159;
- Compare and Constrast with Java identifiers:
- Any number of Unicode character that are letters or digits, _, $
- Must not begin with digit
Compilation and Execution: Ada vs Java
- Compile and Run Java
-
javac Foo.java
- creates Foo.class
, which contains java byte code
-
java Foo
- executes java.exe on the hardware
- executing java.exe interprets the byte code in
Foo.class
- Compile and run Ada
-
gnatmake foo.adb
- creates foo.o, foo.ali
, and foo.exe
-
foo
- runs foo.exe directly on the hardware
- Notes:
-
gnatmake foo
also compiles foo.adb
- On unix, the executable is called
foo
, not foo.exe
Files Created by Gnatmake
- The following files are created by
gnatmake foo
:
-
foo.o
contains the object code (ie machine code) for
foo.adb
- Does not contain library routines (eg ada.text_io.put)
-
foo.ali
contains library information about the compilation
-
foo.exe
contains an executable program
- Contains all library routines (eg ada.text_io.put)
hello2.adb
- Another Hello World program:
with ada.text_io; use ada.text_io;
procedure hello2 is
begin
put_line("Hello ITEC 320 class!");
ada.text_io.put("Hello again!");
new_line;
put_line("Hello ITEC" & " 320 class!");
new_line(2);
put("See ");
put("you ");
put_line("later!");
end hello2;
Things to note:
Use Statement
- Use Statement:
- packages named in a use clause don't need fully qualified names
(unless needed for disambiguation)
- fully qualified names can still be used
- Comparison with Java:
- import is similar to use: allows access to class members without using fully
qualified names
- No equivalent to with statement: any class file that the compiler can
find can be accessed
- Use statement has nothing to do with anything beyond compilation
More on the With Statement
- With Statement: Required for every library referenced in
the program
- Packages can't be accessed unless a with statement exists
- Its only function is to specify to the compiler what libraries are being
used
- Why have a with statement?
- Provides a level of redundancy for the compiler
- Provides a simple way to find all packages used, without searching for
all library accesses
- Allows with and use as separate statements
- One package is automatically available: Standard
- Contains definitions of standard type, such as Boolean
- Similar to java.lang
- With statement has nothing to do with anything beyond compilation
- Similar to C
include
statement
- But no expansion
- No nesting