Ada - Can't find library info for "check_positive.adb" - ada

I have to learn Ada so I can write an interpreter for it. But I cannot find many resources on learning the language. I get the above message when attempting to compile the following code: I save the file as check_positive.adb. What else am I supposed to do? I ran gnatls Check_Positive.adb after I ran gnatchop -w Check_Positive.adb. I am using GNAT Community v5.1.0.
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
procedure Check_Positive is
N : Integer;
begin
Put ("Enter an integer value: "); -- Put a String
Get (N); -- Read in an integer value
if N > 0 then
Put (N); -- Put an Integer
Put_Line (" is a positive number");
end if;
end Check_Positive;

gnatls and gnatchop will not compile your code, you should try gnatmake:
gnatmake check_positive.adb
be aware that GNAT expects lower-case filenames and one procedure/function/package spec/package body per file. If you organize your code that way, you won't need gnatchop.

Related

Ada : custom type in range 1..var?

I'm a very beginner of Ada code. I use GPS from AdaCore.
I would create a variable sized by the user.
I write this :
-- My ada program --
with Ada.Text_IO, Ada.Integer_Text_IO;
use Ada.Text_IO, Ada.Integer_Text_IO;
procedure main is
wanted : Integer := 10;
type custom is range 0..wanted;
...
But something went wrong in line 8 :
Builder results
C:\Users\**********\Desktop\ada project\src\main.adb
8:26 "wanted" is not static constant or named number (RM 4.9(5))
8:26 non-static expression used for integer type bound
I really don't understand what this mean... Can someone help me ?
Variable wanted is not a constant, it may change its value during program execution, therefore this variable is not allowed to be used as range constraint when declaring new types. You may however make it constant by using constant keyword (Wanted : constant Integer := 10;). It should resolve your problem.
As said by Timur, wanted has to be constant in its scope. This allows you some nice things such as declaring a type inside a procedure. Look at this, it might be of interest :)
-- My ada program --
with Ada.Text_IO, Ada.Integer_Text_IO;
use Ada.Text_IO, Ada.Integer_Text_IO;
procedure Main is
procedure Test (Wanted : Integer) is
type custom is new Integer range 0..wanted;
begin
Put_Line("First value " & Custom'Image (Custom'First)
& " Last value " & Custom'Image (Custom'Last));
end Test;
begin
Test (10);
Test (12);
end Main;
Output is
First value 0 Last value 10
First value 0 Last value 12
In this case, your type is different from one call to another but it works as wanted is constant within the procedure. The only thing is that the type defined has to be a new derived type of the type of your parameter.
I let you think about the possibilities :)

How do I use “separate" keyword

I am unable to figure out keyword separate in Ada and its depth concept. Please help me to understand by giving a small example?
Lets say I have a nested procedure
with ada.text_io; use ada.text_io;
procedure main is
procedure proc is
begin
put_line ("i am proc");
end proc;
begin
put_line ("main");
end main;
How to use separate keyword ?
You primarily use the separate keyword to achieve one of 2 effects.
OS specific actions. (Put 2 versions of the procedure / functions in different directories, and compile for 2 different targets)
Separation of a lengthy procedure from surrounding code.
Here is an example to show the syntax.
package_x.ads
package Package_X is
procedure Foo;
procedure Sep;
end Package_X;
package_x.adb
package body Package_X is
procedure Foo is
begin
null;
end Foo;
procedure Sep is separate;
end Package_X;
package_x-sep.adb
separate (Package_X) procedure Sep is
begin
null;
end Sep;
The separate keyword creates a unit of compilation, a subunit, that is compiled independently. The parameter of separate refers to the package in which the subunit is a sub unit of.
So if you had a package body X, then you remove procedure Y from it, you create a sub unit of X by creating a new file in which you place Y, and put "separate(X)" at the start of the file, to indicate that Y is really part of X.

How to print integers in ada83 environment

I want to print integers in Ada 83. At present I am just using 'with Text_IO' and 'use Text_IO'. I don't want to print using the Integer'Image option. I want to use Integer_Text_IO in ada83. Please help me out with the syntax.
I am using below code:
with Text_IO;
use Text_IO;
i: INTEGER :=1;
package Int_IO is new Integer_IO(INTEGER);
use Int_IO; put(i);
I am getting 'expect signed integer type in instantiation of "Num" ' error.
The example below, which compiles, should help.
But please, when posting a question on StackOverflow (or anywhere on the Net, really) show us the code you’ve actually tried. The sample you’ve provided doesn’t come close to compiling (it fails at line 3 with compilation unit expected), and that makes it very hard for us to work out how to help you.
You’ll get expect signed integer type in instantiation of “Num” if you try to instantiate Text_IO with the wrong sort of type (for example, Float).
with Text_IO;
procedure Integer_IO_Demo is
package Int_IO is new Text_IO.Integer_IO (Integer);
begin
for J in 5 .. 10 loop
Int_IO.Put (J);
Text_IO.New_Line;
end loop;
end Integer_IO_Demo;

Programming in Ada error in executing files

I just started programming in Ada and I have a problem in executing some files .adb.
with Ada.Text_IO, Ada.Integer_Text_IO;
use Ada.Text_IO, Ada.Integer_Text_IO;
procedure Trapeze(A:integer;B: Integer;C :Integer;D: Integer) is
procedure Traceligne(C:Character;X:Integer) is
begin
for I in 1 .. X loop
Put("C");
end loop;
end Traceligne;
H:Integer:=C;
Alpha:Integer:=D;
Decd:integer:=A;
Decg:integer:=B;
begin
for I in 1 .. H loop
Traceligne(' ',Decd);
Traceligne('*',Alpha);
Traceligne(' ',Decg);
Decd:=Decd-Integer(Decd*I/H);
Decg:=Decg-Integer(Decg*I/H);
Alpha:=Alpha+Integer(Decd*I/H)+Integer(Decg*I/H);
end loop;
get(x);
Trapeze(1,2,3,4)
end Trapeze;
i have this code error:
line 1: with: command not found.
I don't know the reason of this message .
PS: The question is simple for some programs my computer recognize the with Ada.Text_IOcommand and for some others it gives me the error mentioned before I don't know why.
In fact I found the real problem its in the compilation of the file it compiles but stops in gcc and doesn't continue to the gnatbind and gnatlink I hope you will have some suggestions.
The reason for the command not found message is that you're trying to execute the trapeze.adb file rather than an executable program built from it (trapeze or, on Windows, trapeze.exe).
The reason why GNAT hasn't built an executable program is that, in GNAT, a main program must be a parameterless procedure; presumably some of your test programs were parameterless, but this one isn't.
If you were hoping to be able to supply the parameters from the command line, as
$ ./trapeze 1 2 3 4
then you need to use Ada.Command_Line to retrieve them.

Ada Modeless Subprogram Parameters

If a parameter in Ada is left modeless, what happens?
what is the difference between
procedure my_func ( param_1 : in param_type )
and
procedure my_func ( param_1 : param_type )
I am new to ada and have been writing most of my procedures as the latter. The program compiles and runs as expected.
There is no difference - if no parameter mode is given, the compiler assumes "in".
See http://www.ada-auth.org/standards/12rm/html/RM-6-1.html line beginning 18/3.
-- Martin
As suggested by Martin, the default mode is 'in' if not provided.
I would like to add that if possible, you can sometimes experiment with things about which you are doubtful.
like look at the below simple code, I have not given any mode to the arguement 'no_1'.
And as seen I am assigning value of 'no_2' to it.
with Ada.Text_IO; use Ada.Text_IO;
procedure just_testing is
procedure get_value (no_1 : Integer);
procedure get_value (no_1 : Integer) is
no_2 : Integer := 2;
begin
no_1 := no_2;
end get_value;
begin
Put("auto mode");
end just_testing;
And when I compile this code, look what we get as error.
>gnatmake just_testing.adb
gcc -c just_testing.adb
just_testing.adb:10:09: assignment to "in" mode parameter not allowed
gnatmake: "just_testing.adb" compilation error
So, compiler makes it clear that the default mode is 'in', as we can not assign any value to argument with mode in.

Resources