]>
Hello, World in Ada/LP Chip Richards This is a simple example of writing an Ada program using the XML LitProg DocBook-based LP (literate programming) tool by Norman Walsh. The source file that produced this document, and that also contains and produces the actual source code, is called helloworld.xweb. The Ada source code that appears in this document is the actual code that is compiled to produce the running program; thus, the documentation cannot disagree with the code, because it is the code. Another nice thing about this system is that the document you are reading can easily be produced in HTML, TeX, PDF, man-page format, GNU info format, WinHelp format, or any other format that DocBook supports.
The User Guide Running the program is simplicity itself. It has no arguments or options or anything else complicated like that, and its output is, or should be, completely predictable. On any current system with a command processor, you run it by typing this command, assuming you're sitting in the directory where you built it: ./helloworld And the output should look like this: Hello, world, from Ada/LP! This concludes the user guide portion of our show. Stay tuned for an explication of the miracle of modern technology that is the program itself!
The Code
Program Body Every Ada main program has roughly the same structure--a procedure with an environment and code. Larger Ada programs are usually broken up into packages, but all will have one procedure designated as the main procedure, the place where the program's execution starts. procedure &prog; is begin -- &prog; end &prog;;
Body Code Within the main program, the code is quite simple. This code was modeled after Kernighan and Ritchie's famous "Hello, world" example, and is designed to be as simple as possible while still providing visible results. The point is to get the program built and executed, without having to think at this point about the code. Once you have mastered the process of building software on the system(s) you have available, then you can focus on more complex code. Put ("Hello, world, from Ada/LP!");
The Program's Environment
The <literal>with</literal> clause Of course, to make that work, we need to import a bit of the standard Ada library. First, we name the package whose subroutines we wish to call, in a with clause: with Ada.Text_IO;
The <literal>use</literal> clause But if we left it at that, we'd have to put the specifier Ada.Text_IO in front of every procedure or function we called. In this small program, it's not a big deal, but in larger ones, you'd often like to omit the constant repetition of that specifier. So we can add a use clause to make the names within the package visible: use Ada.Text_IO;
External Environment Section When and whether to use use clauses are questions of deep import (no pun intended) to many Ada programmers. Some shops or projects outlaw them entirely. Many restrict their use. As you become comfortable with the concepts, and get your fingers burned a few times, you'll eventually develop your own personal guidelines for appropriate application of the use clause. Together, the with and use clauses make up what can be termed the program's external environment, or as the Ada reference manual calls it, the context clause :