Whirlwind Tour of Ada
Part 1
History, Design Goals, and Applications
Hello World, Use, Text_IO, Compile and Run
Ada History
Versions
- Ada 83
- Original version
- Robust type model
- Support for threads, generics (ie Java generic types)
- Partial support for OO
- Ada 95:
- Complete support of OO (but not interfaces)
- Expanded support of threads
- Support for multilanguage programming, ...
- Gnat: free compiler part of GNU gcc
- Backward/upward compatible
- Ada 05:
- Java-like interfaces (ie multiple specification inheritance)
- Object notation allowed
- Improved support for real-time and high integrity systems
- Improved standard packages
- Backward/upward compatible
- Ada 12:
- Conditional expressions and case expressions
- Quantified expressions (ie for all and for some
- In-out parameters for functions
- Iterators
- Preconditions, postconcidtions, type invariants
- Improved support for multiprocessors
- Bounded containers
Design Goals
- Program reliability and maintenance
- Program efficiency
- Programming as a human activity
- Emphasize readability over writability
- Programming in the large
- We will see examples of how the language supports these goals
Support for Design Goals
- Readability favored over writability (eg begin .. end vs {})
- Error prone notations avoided (eg = and :=)
- Strong typing (stronger than Java)
- Keep language small and consistent
- Support for large systems (ie packages)
- Support for abstract data types
- Support for multiple threads
- Validation suite: set of programs that test compiler
- Standardized before implementation
Application Areas
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
-
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
- Later we'll see 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 string from the input
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: Ada.Standard
- Contains definitions of standard type, such as Boolean
- Similar to java.lang
- With statement has nothing to do with anything beyond compilation
More on Text_IO
-
put vs put_line: like print and
println in java
-
put_line only available for string arguments
-
put:
- available for string and character arguments
- available (in other packages) for numeric arguments
- new_line: output one newline
- new_line(n): output n newlines