Programming in Ada error in executing files - runtime-error

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.

Related

Ada - Discrete_Random example

Page 53 of "Programming in Ada 2012" by John Barnes shares an incomplete fragment of code that I cannot get to work.
I've come up with this as a full program expanding the code from the book...
with Ada.Numerics; use Ada.Numerics;
with Ada.Text_IO; use Ada.Text_IO;
procedure Main is
type Coin is (Heads, Tails);
package Random_Coin is new Discrete_Random(Coin);
use Random_Coin;
G : Generator;
C : Coin;
begin
for i in 1 .. 20 loop
C := Random(G);
Put (C'Image);
end loop;
end Main;
The "GPS" IDE I'm using complains of the following errors:
Line 6: "Discrete_Random" is undefined
Line 7: "Random_Coin" is undefined
Line 9: "Generator" is undefined
Line 14: "Random" is undefined
The IDE does give me the "intellisense" (to use a term from Visual Studio) that indicates that Discrete_Random is actually visible and available given the "with" and "use" statements I've added.
Can someone walk me through the stupid mistakes I'm making please?
The problem is that, unlike Ada.Numerics.Pi, in which Pi is a component of Ada.Numerics, Discrete_Random is a child of Ada.Numerics.
Once you’ve said use Ada.Numerics, you could write just Pi in your program, but you have to actually with Ada.Numerics.Discrete_Random in order for it to be available.
In fact, you don’t need to with or use Ada.Numerics, this works just fine:
with Ada.Numerics.Discrete_Random;
with Ada.Text_IO; use Ada.Text_IO;
procedure Main is
type Coin is (Heads, Tails);
package Random_Coin is new Ada.Numerics.Discrete_Random(Coin);

How to stop console window from closing immediately | GNAT - GPS

I have Ada program that runs and compile perfectly using GNAT - GPS. When I run its exe file and provide user input then instead of saying "Press any key to continue", the exe closes immediately.
I have searched for it online alot but i only found info related to c/c++/visual studio console window using system('pause'); OR Console.Readline().
Is there any way around for this in Ada lanaguage?
Apart from using Get_Line or Get, you can also use Get_Immediate from the Ada.Text_IO package. The difference is that Get_Line and Get will continue to read user input until <Enter> has been hit, while Get_Immediate will block only until a single key has been pressed when standard input is connected to an interactive device (e.g. a keyboard).
Here's an example:
with Ada.Text_IO; use Ada.Text_IO;
procedure Main is
begin
-- Do some interesting stuff here...
declare
User_Response : Character;
begin
Put_Line ("Press any key to continue...");
Get_Immediate (User_Response);
end;
end Main;
NOTES
You should run the program in an interactive terminal (Bash, PowerShell, etc.) to actually see the effect of Get_Immediate. When you run the program from within GPS, then you still have to press enter to actually exit the program.
This might be too much detail, but I think that Get still waits for <Enter> to be pressed because it uses fgetc from the C standard library (libc) under the hood (see here and here). The function fgetc reads from a C stream. C streams are initially line-buffered for interactive devices (source).
The answer from #DeeDee is more portable and only Ada and the preferable way to go, so my answer is just if you are looking for a "windows" way to do it.
I think there is a linker option for it, but I couldn't find it. A more manual way is to bind the system() command from C and give it a "pause" command and place it at the end of your program:
with Ada.Text_IO; use Ada.Text_IO;
with Interfaces.C.Strings;
procedure Main is
function System(Str : Interfaces.c.strings.chars_ptr) return Interfaces.C.int
with Import,
Convention => C,
External_Name => "system";
procedure Pause is
Command : Interfaces.c.Strings.chars_ptr
:= Interfaces.C.Strings.New_String("pause");
Result : Interfaces.C.int
:= System(Command);
begin
Interfaces.C.Strings.Free(Command);
end Pause;
begin
Put_Line("Hello World");
Pause;
end Main;
I know you mentioned seeing about pause already, but just wanted to show an example.
The same way you could use Console.Readline(), you can use Get_Line from the package Ada.Text_IO.
In this case, you will have to put the result into a String that you won't use.

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

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.

Compiler messages in Julia

Consider the following code:
File C.jl
module C
export printLength
printLength = function(arr)
println(lentgh(arr))
end
end #module
File Main.jl
using C
main = function()
arr = Array(Int64, 4)
printLength(arr)
end
main()
Let's try to execute it.
$ julia Main.jl
ERROR: lentgh not defined
in include at /usr/bin/../lib64/julia/sys.so
in process_options at /usr/bin/../lib64/julia/sys.so
in _start at /usr/bin/../lib64/julia/sys.so
while loading /home/grzes/julia_sucks/Main.jl, in expression starting on line 8
Obviously, it doesn't compile, because lentgh is misspelled. The problem is the message I received. expression starting on line 8 is simply main(). Julia hopelessly fails to point the invalid code fragment -- it just points to the invocation of main, but the erroneous line is not even in that file! Now imagine a real project where an error hides really deep in the call stack. Julia still wouldn't tell anything more than that the problem started on the entry point of the execution. It is impossible to work like that...
Is there a way to force Julia to give a little more precise messages?
In this case it's almost certainly a consequence of inlining: your printLength function is so short, it's almost certainly inlined into the call site, which is why you get the line number 8.
Eventually, it is expected that inlining won't cause problems for backtraces. At the moment, your best bet---if you're running julia's pre-release 0.4 version---is to start julia as julia --inline=no and run your tests again.

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;

Resources