Ada
is a language that supports logic implementations of many different programming paradigms, including OOP
Object Oriented Programming and some elements of FP
Functional Programming. However, the core design of Ada
is to be an Imperative
Programming language central to C
or Pascal
.
There is one important difference between Ada
and most other programming languages, which is that statements and expressions are very clearly distinguished in Ada
. If we try to use an expression where there should be a statement, we will definitely get an error message when compiling the code.
This principle complements one of the language design criteria mentioned earlier: That is, expressions are used to convey values, not to produce any other side effects. This will help screen out some programming errors in some situations, for example when we mistakenly use equivalence comparison where there should be a statement that assigns a value to a variable. there.
1 2 3 4 5 6 7 8 | <span class="token keyword">const</span> fromMySoul <span class="token operator">=</span> <span class="token boolean">true</span> <span class="token keyword">var</span> shouldDoIt <span class="token operator">=</span> <span class="token boolean">false</span> <span class="token keyword">if</span> <span class="token punctuation">(</span> shoudDoIt <span class="token operator">=</span> fromMySoul <span class="token punctuation">)</span> console <span class="token punctuation">.</span> <span class="token function">log</span> <span class="token punctuation">(</span> <span class="token string">"Ok. We'll do it."</span> <span class="token punctuation">)</span> <span class="token keyword">else</span> console <span class="token punctuation">.</span> <span class="token function">log</span> <span class="token punctuation">(</span> <span class="token string">"Nah. We're not gonna do it."</span> <span class="token punctuation">)</span> |
It is an example of forking logic in JavaScript
, where we intend that: If the shoulDoIt
also agrees with the subconscious fromMySoul
, then the intention will be executed. In this case when we misrepresent by the above value assignment, obviously the result will always be "Ok. We'll do it."
. And that’s something Ada
won’t let happen.
Hello, Ada!
Before we talk about installing and preparing the environment for Ada
, let’s take a quick look at the Hello World
program just like when we start learning any other programming language.
1 2 3 4 5 6 7 8 9 | with Ada.Text_IO; procedure Greet is -- khu vực khai báo các biến begin -- in "Hello, Ada !" ra cửa sổ dòng lệnh Ada.Text_IO.Put_Line ("Hello, Ada !"); end Greet; |
There are a few points worth noting in this program:
- We have a language that uses
indentation style
syntax with statements that are considered to be in the same block if they have the same indentation at the beginning, instead ofblock style
with curly braces{}
likeC
orJavaScript
. - A
sub-program
inAda
can be aprocedure
or afunction
. And aprocedure
like in the example code above will never return a value at the called location. - The
with
keyword is used to refer to externalmodule
. This element is equivalent toimport
in many other languages, and#include
inC
orC++
. We will learn more about thewith
operation later. - Unlike languages like
C
orC++
, the main program inAda
can be named arbitrarily, as in the example aboveGreet
, instead ofMain
. comment
are prefaced with the symbol--
and extend to the end of that line. We will not havemulti-line comment
syntax and if necessary we will have to create multiple single line comments.
In terms of expressive meaning, procedure
are equivalent to sub-program
in other languages if they return meaningless values. For example void
in C
and C++
, or undefined
in JavaScript
, etc. We will go through the operation of defining function function
in Ada
in the following section. For now, we will modify the Hello World
program a bit.
1 2 3 4 5 6 7 8 9 | with Ada.Text_IO; use Ada.Text_IO; procedure Greet is -- khu vực khai báo các biến begin -- in "Hello, Ada !" ra cửa sổ dòng lệnh Put_Line ("Hello, Ada !"); end Greet; |
This time we use the additional use
feature that comes with with
. The syntax to use is use package-name
. And in the place of calling the Put_Line
procedure, we were able to reduce the package
reference name up front.
Environment Settings
Although used mainly in the fields of high-tech science and enterprise
applications, Ada
‘s application development toolkit offers a free community edition
with a custom compiler. Full-featured translation equivalent to the commercial pro edition
for businesses.
You can download GNAT Community
for Windows here:
- GNAT Community – integrated toolkit with IDE code editor for
Ada
andC
- GTA-Ada – plug-in that supports creating
project
with GNOME.org GUI libraries for cross-platform application development needs. Write code only once and compilepackage
for Windows, Linux, BSD, Mac, etc..
If you are using a certain version of Linux, you can find in the default repository
library the gnat-12
package (the latest version is Ada 2012) and the gprbuild
project management tool (GNAT Project Build). In it, gnat-12
is the core of the development environment and we will learn how to immediately use gprbuild
to set up the project
and manage the code folders.
Currently I am using Ubuntu 22.10 and so I can guarantee the install command below will work fine. Other typical Linux distributions such as Fedora and OpenSUSE also list the above package
in the library lookup pages.
1 2 3 | sudo apt update && sudo apt upgrade sudo apt install gnat-12 gprbuild |
In particular about usage, in the tutorial series here, I will use gprbuild
using the command line window and edit the project
declaration file manually. You can also save the built-in IDE editor until you want to create an actual application project
. And so here I need to pay attention to the installation operation on Windows.
All operations with the installer are Next
, Next
, and Next
with the options already set by default. However, the GNAT Community
installer for Windows will not automatically declare environment variables for operations on the CMD
command line. Therefore, after running the installer, we will need to open the installation directory of GNAT
and navigate to the bin
directory. Then copy/paste
the full path of the C:GNAT2021bin
directory into the Path
environment variable.
Environment Variables > System Variables > Path > Edit > New
Or open a CMD
command line window with Admin
rights:
1 2 | setx path "%PATH%;C:GNAT2021bin" |
Then in the command line window, we can type the command to check the installed version of GNAT
:
1 2 3 | gnatmake --version gprbuild --version |
Ok.. everything is ready, let’s start learning the details of the Ada
language and the gprbuild
management tool.